如何检查四个点是否形成矩形 [英] How to check if four points form a rectangle

查看:548
本文介绍了如何检查四个点是否形成矩形的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个形状识别应用程序。此时,一组点(x,y)由角点检测器确定(红点,img。来检测二进制图像上的线条。或者找到图像中的所有轮廓。

  • 最后一步是检测矩形。

  • 更新:



    这是另一个也适用于灰色图片的解决方案。


    1. 执行将图像转换为1位的阈值(我使用 200 从255作为阈值)。

    2. 查找新图像中的所有轮廓区域大于某个常数(我用 1000 )。

    3. 找到每个轮廓的边界矩形并进行检查:




    ContourArea / BoundingReactangleArea>常量


    I把这个常数作为 0.9



    这个算法给了我下一个结果:



    这是OpenCV代码:

      Mat src = imread(input.jpg),灰色,结果; 
    vector< vector< Point> >轮廓;
    vector< Vec4i>层次结构;

    result = Mat(src.size(),CV_8UC1);

    cvtColor(src,src,CV_BGR2GRAY);
    threshold(src,gray,200,255,THRESH_BINARY_INV);
    findContours(灰色,轮廓,层次结构,CV_RETR_TREE,CV_CHAIN_APPROX_SIMPLE,Point(0,0));

    result = Scalar :: all(0);
    for(size_t i = 0; i< contours.size(); i ++)
    {
    Rect rect = boundingRect(contours [i]);
    if(rect.area()> 1000)
    {
    double area = contourArea(contours [i]);
    if(area / rect.area()> 0.9)
    {
    drawContours(result,contours,i,Scalar(255), - 1);
    }
    }
    }


    I am working on a shape recognition app. At this moment a set of points (x,y) is determined by corner detector (red points, img. 2.). Four of these points (in red frames, img. 2.) are vertices of a rectangle (sometimes a little deformed rectangle). What would be the best way to find them among others?

    Here is an example of an input image:

    And it looks like this after corner detection:

    解决方案

    This is not an answer to your question - it's just suggestion.

    In my opinion corner detector is a bad way to detect rectangles - it will take much time to calculate all point distances as mathematician1975 suggested. You have to use another technique in this situation:

    1. That stamp is violet color, so first thing you should do is color segmentation.
    2. After you've done with step 1 you can use Houhg transform to detect lines on binary image. Or find all contours in image.
    3. And the final step is to detect rectangle.

    Update:

    Here's another solution that should also work in gray images.

    1. Do a threshold to convert image to 1bit (I used 200 from 255 as threshold).
    2. Find all contours in new image which have area bigger than some constant (I took 1000).
    3. Find bounding rectangle for each contour and do a check:

    ContourArea / BoundingReactangleArea > constant

    I take this constant as 0.9.

    And this algorithm gave me next result:

    Here's OpenCV code:

    Mat src = imread("input.jpg"), gray, result;
    vector<vector<Point> > contours;
    vector<Vec4i> hierarchy;
    
    result = Mat(src.size(), CV_8UC1);
    
    cvtColor(src, src, CV_BGR2GRAY);
    threshold(src, gray, 200, 255, THRESH_BINARY_INV);
    findContours(gray, contours, hierarchy, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE, Point(0, 0));
    
    result = Scalar::all(0);
    for (size_t i=0; i<contours.size(); i++)
    {
        Rect rect = boundingRect(contours[i]);
        if (rect.area() > 1000)
        {
            double area = contourArea(contours[i]);
            if (area/rect.area() > 0.9)
            {
                drawContours(result, contours, i, Scalar(255), -1);
            }
        }
    }
    

    这篇关于如何检查四个点是否形成矩形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆