四边形顶点寻找

jjhua0885 2018-04-23 10:30:03

描述
如图的东西的意思是,用相机拍了一张图片,里面的四边形就是所拍物体 ,
问题
寻找四边形的四个顶点,之前我使用的方法是寻找距离ABCD四个顶点最近的点作为各自的顶点,但是图形一旦歪的厉害点就不行了,有什么好办法?
...全文
2321 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
jiht594 2018-04-24
  • 打赏
  • 举报
回复
引用 4 楼 jiht594 的回复:
“之前我使用的方法是寻找距离ABCD四个顶点最近的点作为各自的顶点” 看不懂说的啥,看着像是已经知道ABCD四个顶点了,还找什么顶点?
看懂了ABCD是图片顶点、和四边形没关系啊。 找顶点的算拐角最尖的地方吧。所有构成点、相邻2线段求夹角、夹角最大的四个角就是顶点吧。
jiht594 2018-04-24
  • 打赏
  • 举报
回复
“之前我使用的方法是寻找距离ABCD四个顶点最近的点作为各自的顶点” 看不懂说的啥,看着像是已经知道ABCD四个顶点了,还找什么顶点?
jiht594 2018-04-24
  • 打赏
  • 举报
回复
引用 5 楼 jiht594 的回复:
[quote=引用 4 楼 jiht594 的回复:] “之前我使用的方法是寻找距离ABCD四个顶点最近的点作为各自的顶点” 看不懂说的啥,看着像是已经知道ABCD四个顶点了,还找什么顶点?
看懂了ABCD是图片顶点、和四边形没关系啊。 找顶点的算拐角最尖的地方吧。所有构成点、相邻2线段求夹角、夹角最的四个角就是顶点吧。[/quote] 小
jjhua0885 2018-04-24
  • 打赏
  • 举报
回复
引用 5 楼 jiht594 的回复:
[quote=引用 4 楼 jiht594 的回复:] “之前我使用的方法是寻找距离ABCD四个顶点最近的点作为各自的顶点” 看不懂说的啥,看着像是已经知道ABCD四个顶点了,还找什么顶点?
看懂了ABCD是图片顶点、和四边形没关系啊。 找顶点的算拐角最尖的地方吧。所有构成点、相邻2线段求夹角、夹角最大的四个角就是顶点吧。[/quote] ABCD识别区域顶点,识别区域内的蓝色物体才是是别的主体,蓝色物体是由点阵(非规则的圆形点阵)构成的四边形,这里说的顶点是四边形点阵四个顶点,不是你想象的那么简单
赵4老师 2018-04-24
  • 打赏
  • 举报
回复
千句说明讨论; 比不上一行实现代码。
赵4老师 2018-04-23
  • 打赏
  • 举报
回复
仅供参考:
double angle( Point pt1, Point pt2, Point pt0 ) {// finds a cosine of angle between vectors from pt0->pt1 and from pt0->pt2
    double dx1 = pt1.x - pt0.x;
    double dy1 = pt1.y - pt0.y;
    double dx2 = pt2.x - pt0.x;
    double dy2 = pt2.y - pt0.y;
    double ratio;//边长平方的比
    ratio=(dx1*dx1+dy1*dy1)/(dx2*dx2+dy2*dy2);
    if (ratio<0.8 || 1.2<ratio) {//根据边长平方的比过小或过大提前淘汰这个四边形,如果淘汰过多,调整此比例数字
//      Log("ratio\n");
        return 1.0;//根据边长平方的比过小或过大提前淘汰这个四边形
    }
    return (dx1*dx2 + dy1*dy2)/sqrt((dx1*dx1 + dy1*dy1)*(dx2*dx2 + dy2*dy2) + 1e-10);
}
void findSquares( const Mat& gray0, vector<vector<Point> >& squares ) {// returns sequence of squares detected on the gray0. the sequence is stored in the specified memory storage
    squares.clear();

    Mat pyr,gray1,timg;

    // down-scale and upscale the gray0 to filter out the noise
    pyrDown(gray0, pyr, Size(gray0.cols/2, gray0.rows/2));
    pyrUp(pyr, timg, gray0.size());
    vector<vector<Point> > contours;

    // try several threshold levels
    for (int l = 0; l < N; l++ ) {
        // hack: use Canny instead of zero threshold level.
        // Canny helps to catch squares with gradient shading
//      if (l == 0 ) {//可试试不对l==0做特殊处理是否能在不影响判断正方形的前提下加速判断过程
//          // apply Canny. Take the upper threshold from slider
//          // and set the lower to 0 (which forces edges merging)
//          Canny(timg, gray1, 0, thresh, 5);
//          // dilate canny output to remove potential
//          // holes between edge segments
//          dilate(gray1, gray1, Mat(), Point(-1,-1));
//      } else {
            // apply threshold if l!=0:
            //     tgray(x,y) = gray1(x,y) < (l+1)*255/N ? 255 : 0
            gray1 = timg >= (l+1)*255/N;
//      }

        // find contours and store them all as a list
        findContours(gray1, contours, CV_RETR_LIST, CV_CHAIN_APPROX_SIMPLE);

        vector<Point> approx;

        // test each contour
        for (size_t i = 0; i < contours.size(); i++ ) {
            // approximate contour with accuracy proportional
            // to the contour perimeter
            approxPolyDP(Mat(contours[i]), approx, arcLength(Mat(contours[i]), true)*0.02, true);//0.02为将毛边拉直的系数,如果对毛边正方形漏检,可试试调大

            // square contours should have 4 vertices after approximation
            // relatively large area (to filter out noisy contours)
            // and be convex.
            // Note: absolute value of an area is used because
            // area may be positive or negative - in accordance with the
            // contour orientation
            if (approx.size() == 4 && isContourConvex(Mat(approx))) {
                double area;
                area=fabs(contourArea(Mat(approx)));
                if (4000.0<area && area<30000.0) {//当正方形面积在此范围内……,如果有因面积过大或过小漏检正方形问题,调整此范围。
//                  printf("area=%lg\n",area);
                    double maxCosine = 0.0;

                    for (int j = 2; j < 5; j++ ) {
                        // find the maximum cosine of the angle between joint edges
                        double cosine = fabs(angle(approx[j%4], approx[j-2], approx[j-1]));
                        maxCosine = MAX(maxCosine, cosine);
                        if (maxCosine==1.0) break;// //边长比超过设定范围
                    }

                    // if cosines of all angles are small
                    // (all angles are ~90 degree) then write quandrange
                    // vertices to resultant sequence
                    if (maxCosine < 0.1 ) {//四个角和直角相比的最大误差,可根据实际情况略作调整,越小越严格
                        squares.push_back(approx);
                        return;//检测到一个合格的正方形就返回
//                  } else {
//                      Log("Cosine>=0.1\n");
                    }
                }
            }
        }
    }
}
jjhua0885 2018-04-23
  • 打赏
  • 举报
回复
引用 2 楼 qq_38204686 的回复:
距离四条边最近的?....
之前使用的方法是距离对应顶点最近的点作为顶点,但是图歪的时候就不行了
大米粥哥哥 2018-04-23
  • 打赏
  • 举报
回复
距离四条边最近的?....

3,882

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 其它技术问题
社区管理员
  • 其它技术问题社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧