php 边缘检测从图像中裁剪子图像

第一步:安装,

composer require lizhichao/one-img

第二步:调用测试,返回黑白图像即可:

$img_path = './test.png';
$img = new \OneImg\Border();
$dd = $img->setImg($img_path)
    ->scale(300, 300)
    ->outline()
    ->save();

第三步:创建数据表:img,字段:x(int) | y(int) | color(int)

第四步:修改 Border类:outline(),函数如:

$color = imageColorAllocate($this->img, $c, $c, $c);
imagesetpixel($new_img, $x, $y, $color);
//写入数据至数据表当中
if($c>0){
    DB::table('img')->insert(
        ['x' => $x, 'y' =>$y,'color'=>$c]
    );
}

第五步:getParseData 【解析图片数据,计算坐标点,返回响应的4点坐标】

public function getParseData(){
        #### 处理x,y 轴方向所有的坐标基点 ###
        //确定x轴分布的点坐标
        $list_x = DB::table('img')->groupBy('x')->get()->toArray();
        $x1 = 0;
        $arr_x = [];
        $testx = [];
        $pool_x = [];
        $list_x = json_decode(json_encode($list_x,true),true);
        foreach ($list_x as $item1){
            if($x1 >= (count($list_x)-1) ){
                break;
            }
            //处理是否具有连续性
            $cc = $list_x[$x1+1]['x'];
            $dd = $item1['x'];
            $pl = $cc - $dd;
            if($pl>1){
                $arr_x[] = $item1['x'].'-'.$list_x[$x1+1]['x'];
                //获取四方形当中,a到b点的坐标值
                $testx[] = $item1['x'];
                $testx[] = $list_x[$x1+1]['x'];
            }
            $x1++;
        }
        //计算坐标点x轴方向的差值,基本就可以推算出,所有图形的x轴方向的坐标点
        //$pool_x[] = $test[$x1+1]['x'] - $item1['x'];


        //确定y轴方向的坐标点值
        $list_y = DB::table('img')->groupBy('y')->get()->toArray();
        $y1 = 0;
        $arr_y = [];
        $testy = [];
        $pool_y = [];
        $list_y = json_decode(json_encode($list_y,true),true);
        foreach ($list_y as $item1){
            if($y1 >= (count($list_y)-1) ){
                break;
            }
            //处理是否具有连续性
            $cc = $list_y[$y1+1]['y'];
            $dd = $item1['y'];
            $pl = $cc - $dd;
            if($pl>1){
                $arr_y[] = $item1['y'].'-'.$list_y[$y1+1]['y'];
                //获取四方形当中,a到b点的坐标值
                $testy[] = $item1['y'];
                $testy[] = $list_y[$y1+1]['y'];
            }
            $y1++;
        }
        //计算坐标点x轴方向的差值,基本就可以推算出,所有图形的x轴方向的坐标点
        //$pool_x[] = $test[$x1+1]['x'] - $item1['x'];

        ####计算,两个方向的,相同的误差值####
        $count_x = 0;
        $count_y = 0;
        $plx = 0;
        foreach ($testx as $key=>$itemx2){
            $sumx = $testx[$plx+1]-$testx[$plx];
            $count_x += $sumx;
        }


        $ply = 0;
        foreach ($testy as $key=> $itemy2){
            $sumy = $testy[$ply+1]-$testy[$ply];
            $count_y += $sumy;
        }
        //获得两个方向公共基点参数
        $plx = ceil(($count_x/(count($testx)-1)));
        $ply = ceil(($count_y/(count($testy)-1)));

        #####获取x,y轴,首尾节点####
        $coor_firstx = $list_x[0]['x'];
        $coor_lastx  = $testx[count($testx)-1]+$plx;

        $coor_firsty = $list_y[0]['y'];
        $coor_lasty  = $testy[count($testy)-1]+$ply;

        ###补全两个方向,坐标基点数据###
        array_push($testx,$coor_firstx,$coor_lastx);
        array_push($testy,$coor_firsty,$coor_lasty);
        sort($testx);
        sort($testy);
        //嵌套for循环数据,计算出所有的节点坐标值

        ###计算所有的基点的坐标数据###
        //形成坐标点数据(交叉数据结果集)
        $arr_end = [];
        //这里只能获取全部的坐标节点值,并不能确定,4点准确的坐标值
        foreach ($testx as $item_end1){
            foreach ($testy as $item_end2){
                $arr_end[] = $item_end1.','.$item_end2;
            }
        }

//        return $arr_end;

        $sum_node = count($testy);
        $mm = $sum_node/2;

        //换算精准坐标值
        //开始分组
        $end_i = 1;
        $arr_end_group = [];
        foreach ($arr_end as $item_end){
            if($end_i%$sum_node==0 ){
                $arr_end_group[] = [$arr_end[$end_i-1],$arr_end[$end_i-2],$arr_end[$end_i-3],$arr_end[$end_i-4],$arr_end[$end_i-5],$arr_end[$end_i-6]];
                //$arr_end_group[] = [$arr_end[$end_i-1],$item_end];
                //$arr_end_group[] = $arr_end[$end_i-1];
            }
            $end_i++;
        }

        //二次合并数组
        $end_va = 1;
        $arr_end_va = [];
        foreach ($arr_end_group as $va){
            if($end_va%2==0){
                $arr_end_va[] = [$arr_end_group[$end_va-2],$va];
            }
            $end_va++;
        }

        //合并精准坐标
        $arr = [];
        $arr_i = 0;
        //无法获取数据
        foreach ($arr_end_va as $v){
            if(isset($v)){
                $for_num = count($v[0])/2;
                for ($i=0;$i<$for_num;$i++){
                    $arr[] = [$v[0][$i*2],$v[0][$i*2+1],$v[1][$i*2],$v[1][$i*2+1]];
                }
            }
            $arr_i++;
        }
        ###验证当前数据点是否真实存在###
        $finally_arr = [];
        foreach ($arr as $arr_v){
            foreach ($arr_v as $arr_y){
                $for_arr = explode(',',$arr_y);
                //$for_arr[0],$for_arr[1]
                $for_color = DB::table('img')->where(['x'=>$for_arr[0],'y'=>$for_arr[1]])->value('color');
                if($for_color>0){
                    $finally_arr[] = $arr_v;
                }
            }
        }
        return $finally_arr;
    }

结束,返回坐标

裁剪图片,自行完成

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值