【问题标题】:Possible to reorder an array?可以重新排序数组吗?
【发布时间】:2018-11-09 11:16:35
【问题描述】:

所以我使用的是 Google Vision TEXT_DETECTION,它的基础是 - 它读取一个车牌,然后使用 PHPGD 用一个多边形覆盖它。现在这一切都很好,但似乎坐标数组的顺序错误,我把头撞在墙上我希望你能帮忙:)

在上图中,您可以看到车牌和围绕它的多边形。您可以看到它应该是 2 个正方形,但它是一个正方形和一个十字

这是我获取坐标并使用它们放置多边形的代码

$response = json_decode($json_response, true);

//dd($response);
$red =0;
$green =0;
$blue = 0;
$i =0;
foreach($response['responses'][0]['fullTextAnnotation']['pages'] as $box) {
    $points = array();
    foreach ($box['blocks'] as $block) {
        foreach ($block['paragraphs'] as $paragraph) {
            foreach ($paragraph['words'] as $word) {
                foreach ($word['boundingBox']['vertices'] as $vertex) {
                    array_push($points, $vertex['x'], $vertex['y']);
                }
                $count_points = count($points) / 2;
                $color = imagecolorallocate($im, round(0), round(0), round(0));
                imagefilledpolygon($im, $points, $count_points, $color);
                var_dump($points);
            }

        }

    }

}

这是一个 $points(坐标) 的 var_dump

    array(8) { 
[0]=> int(424) 
[1]=> int(224) 
[2]=> int(446) 
[3]=> int(218) 
[4]=> int(451) 
[5]=> int(235) 
[6]=> int(429) 
[7]=> int(241) }
 array(16) { 
[0]=> int(424)
 [1]=> int(224)
 [2]=> int(446)
 [3]=> int(218)
 [4]=> int(451) 
[5]=> int(235) 
[6]=> int(429) 
[7]=> int(241) 
[8]=> int(454) 
[9]=> int(216)
 [10]=> int(472)
 [11]=> int(211)
 [12]=> int(477)
 [13]=> int(228)
 [14]=> int(459)
 [15]=> int(233)
 }

【问题讨论】:

    标签: php arrays google-vision


    【解决方案1】:

    我不是谷歌视觉专家,但我的直觉是你只是误用了视觉 API。主要问题是你以某种方式将两个多边形的顶点数据合并为一个 - 这就是你得到array(16)的原因。您需要将该数组分成两部分以获得 2 个array(8) 数组。示例:

    $points = 
    [
        424,224,
        446,218,
        451,235,
        429,241,
        // cut vertexes in half here to form two polygons
        454,216,
        472,211,
        477,228,
        459,233
    ];
    
    $image = imagecreatetruecolor(210, 300);
    
    // Colors
    $col_1 = imagecolorallocate($image, 0, 0, 255);
    $col_2 = imagecolorallocate($image, 255, 0, 0);
    $textcolor = imagecolorallocate($image, 0, 255, 0);
    
    // Extract two polygons
    $poly_1 = array_slice($points, 0, 8);
    $poly_2 = array_slice($points, 8, 8);
    
    // Translate & Scale for better visual affect
    translateAndScale($poly_1, -800, -200, 2);
    translateAndScale($poly_2, -800, -200, 2);
    translateAndScale($points, -820, -350, 2);
    
    // Drawing merged polygon
    imagestring($image, 4, 20, 30, 'Two polygons as one :', $textcolor);
    imagepolygon($image, $points, 8, $col_2);
    
    // Drawing separated polygons
    imagestring($image, 4, 20, 180, 'Separated polygons :', $textcolor);
    imagepolygon($image, $poly_1, 4, $col_1);
    imagepolygon($image, $poly_2, 4, $col_1);
    
    // output data
    header('Content-type: image/png');
    imagepng($image);
    imagedestroy($image);
    
    function translateAndScale(&$points, $tran_x = 0, $tran_y = 0, $scale = 1) {
        $i = 1;
        foreach ($points as $k => $coord) {
            $translate = $i & 1 ? $tran_x : $tran_y;
            $points[$k] = ((int)($coord * $scale) + $translate) ;
            $i++;
        }
    }
    

    生成的图像:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-11-12
      • 1970-01-01
      • 2012-02-14
      • 2011-02-24
      • 1970-01-01
      • 2019-10-11
      • 2011-01-27
      相关资源
      最近更新 更多