OpenCV C++/Obj-C: 高级正方形检测

34

我之前提出了有关正方形检测的问题,而且 karlphillip 给出了一个不错的结果。

现在我想更进一步,找到边缘不完全可见的正方形。看看这个例子:

example

有什么好的建议吗?我正在使用 karlphillips 的代码:

void find_squares(Mat& image, vector<vector<Point> >& squares)
{
    // blur will enhance edge detection
    Mat blurred(image);
    medianBlur(image, blurred, 9);

    Mat gray0(blurred.size(), CV_8U), gray;
    vector<vector<Point> > contours;

    // find squares in every color plane of the image
    for (int c = 0; c < 3; c++)
    {
        int ch[] = {c, 0};
        mixChannels(&blurred, 1, &gray0, 1, ch, 1);

        // try several threshold levels
        const int threshold_level = 2;
        for (int l = 0; l < threshold_level; l++)
        {
            // Use Canny instead of zero threshold level!
            // Canny helps to catch squares with gradient shading
            if (l == 0)
            {
                Canny(gray0, gray, 10, 20, 3); // 

                // Dilate helps to remove potential holes between edge segments
                dilate(gray, gray, Mat(), Point(-1,-1));
            }
            else
            {
                    gray = gray0 >= (l+1) * 255 / threshold_level;
            }

            // Find contours and store them in a list
            findContours(gray, contours, CV_RETR_LIST, CV_CHAIN_APPROX_SIMPLE);

            // Test contours
            vector<Point> approx;
            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);

                    // 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 &&
                            fabs(contourArea(Mat(approx))) > 1000 &&
                            isContourConvex(Mat(approx)))
                    {
                            double maxCosine = 0;

                            for (int j = 2; j < 5; j++)
                            {
                                    double cosine = fabs(angle(approx[j%4], approx[j-2], approx[j-1]));
                                    maxCosine = MAX(maxCosine, cosine);
                            }

                            if (maxCosine < 0.3)
                                    squares.push_back(approx);
                    }
            }
        }
    }
}

3
你尝试了哪些修改?请至少告诉我们一些你认为值得探索的方向...另外,简要概括代码段中使用的程序也会很有帮助。 - penelope
4个回答

45
你可以尝试使用 HoughLines 来检测正方形的四边,接着定位四个结果线段交点以检测角落。Hough transform 对噪声和遮挡具有较强的鲁棒性,因此在此处可能很有用。此外,here提供了一个交互式演示,展示了Hough transform的工作原理(至少我认为它很酷:)。Here是我先前的答案之一,它检测出激光十字中心,并显示大部分相同的数学知识(除了它只找到单个角落)。
您可能会在每一侧有多条线,但是定位交点应该有助于确定内点和外点。一旦您找到候选角落,您还可以通过面积或多边形的“正方形程度”来过滤这些候选项。
编辑:所有这些带有代码和图像的答案让我觉得我的答案有点不够好:)所以,这里是一个如何实现此操作的示例:
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <iostream>
#include <vector>

using namespace cv;
using namespace std;

Point2f computeIntersect(Vec2f line1, Vec2f line2);
vector<Point2f> lineToPointPair(Vec2f line);
bool acceptLinePair(Vec2f line1, Vec2f line2, float minTheta);

int main(int argc, char* argv[])
{
    Mat occludedSquare = imread("Square.jpg");

    resize(occludedSquare, occludedSquare, Size(0, 0), 0.25, 0.25);

    Mat occludedSquare8u;
    cvtColor(occludedSquare, occludedSquare8u, CV_BGR2GRAY);

    Mat thresh;
    threshold(occludedSquare8u, thresh, 200.0, 255.0, THRESH_BINARY);

    GaussianBlur(thresh, thresh, Size(7, 7), 2.0, 2.0);

    Mat edges;
    Canny(thresh, edges, 66.0, 133.0, 3);

    vector<Vec2f> lines;
    HoughLines( edges, lines, 1, CV_PI/180, 50, 0, 0 );

    cout << "Detected " << lines.size() << " lines." << endl;

    // compute the intersection from the lines detected...
    vector<Point2f> intersections;
    for( size_t i = 0; i < lines.size(); i++ )
    {
        for(size_t j = 0; j < lines.size(); j++)
        {
            Vec2f line1 = lines[i];
            Vec2f line2 = lines[j];
            if(acceptLinePair(line1, line2, CV_PI / 32))
            {
                Point2f intersection = computeIntersect(line1, line2);
                intersections.push_back(intersection);
            }
        }

    }

    if(intersections.size() > 0)
    {
        vector<Point2f>::iterator i;
        for(i = intersections.begin(); i != intersections.end(); ++i)
        {
            cout << "Intersection is " << i->x << ", " << i->y << endl;
            circle(occludedSquare, *i, 1, Scalar(0, 255, 0), 3);
        }
    }

    imshow("intersect", occludedSquare);
    waitKey();

    return 0;
}

bool acceptLinePair(Vec2f line1, Vec2f line2, float minTheta)
{
    float theta1 = line1[1], theta2 = line2[1];

    if(theta1 < minTheta)
    {
        theta1 += CV_PI; // dealing with 0 and 180 ambiguities...
    }

    if(theta2 < minTheta)
    {
        theta2 += CV_PI; // dealing with 0 and 180 ambiguities...
    }

    return abs(theta1 - theta2) > minTheta;
}

// the long nasty wikipedia line-intersection equation...bleh...
Point2f computeIntersect(Vec2f line1, Vec2f line2)
{
    vector<Point2f> p1 = lineToPointPair(line1);
    vector<Point2f> p2 = lineToPointPair(line2);

    float denom = (p1[0].x - p1[1].x)*(p2[0].y - p2[1].y) - (p1[0].y - p1[1].y)*(p2[0].x - p2[1].x);
    Point2f intersect(((p1[0].x*p1[1].y - p1[0].y*p1[1].x)*(p2[0].x - p2[1].x) -
                       (p1[0].x - p1[1].x)*(p2[0].x*p2[1].y - p2[0].y*p2[1].x)) / denom,
                      ((p1[0].x*p1[1].y - p1[0].y*p1[1].x)*(p2[0].y - p2[1].y) -
                       (p1[0].y - p1[1].y)*(p2[0].x*p2[1].y - p2[0].y*p2[1].x)) / denom);

    return intersect;
}

vector<Point2f> lineToPointPair(Vec2f line)
{
    vector<Point2f> points;

    float r = line[0], t = line[1];
    double cos_t = cos(t), sin_t = sin(t);
    double x0 = r*cos_t, y0 = r*sin_t;
    double alpha = 1000;

    points.push_back(Point2f(x0 + alpha*(-sin_t), y0 + alpha*cos_t));
    points.push_back(Point2f(x0 - alpha*(-sin_t), y0 - alpha*cos_t));

    return points;
}

注意:我调整图片大小的主要原因是为了在我的屏幕上能够看到它,并加快处理速度。

Canny算法

这个算法使用Canny边缘检测来帮助大大减少阈值处理后检测到的线条数量。

enter image description here

Hough变换

然后使用Hough变换来检测正方形的边缘。 enter image description here

交点

最后,我们计算所有线段对的交点。 enter image description here

希望这有所帮助!


+1 坚实的方法,我明天会测试它。我需要一个强大的解决方案,能够抵抗噪音。 - dom
现在的代码依赖于阈值处理来隔离正方形。如果您更愿意在交点检测后隔离正方形,则需要开始测试交点,可能使用已经提到的cv::approxPoly函数。 - mevatron
嗨@mevatron,如何在iOS(Objective-C)中实现这个? - Kirti Nikam
1
谢谢你的想法。如果有多个矩形的情况怎么办?交叉点不会产生很多误报吗? - mehfoos yacoob
如何在设备坐标中绘制这些角点。目前我能找到的是不在设备坐标中的cv::point。 - Mukesh
acceptpair 函数中有一个 bug 吗?如果其中一个 theta 值小于 minTheta,而另一个值略大(小于 2*mintheta),那么“deal with…”这行代码会将一个点“移开”,而不去处理另一个点。 - rew

29

我尝试使用相当简单的凸包方法

这里您可以找到检测轮廓的凸包。它会消除纸张底部的凸性缺陷。

下面是OpenCV-Python中的代码:

import cv2
import numpy as np

img = cv2.imread('sof.jpg')
img = cv2.resize(img,(500,500))
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)

ret,thresh = cv2.threshold(gray,127,255,0)
contours,hier = cv2.findContours(thresh,cv2.RETR_LIST,cv2.CHAIN_APPROX_SIMPLE)

for cnt in contours:
    if cv2.contourArea(cnt)>5000:  # remove small areas like noise etc
        hull = cv2.convexHull(cnt)    # find the convex hull of contour
        hull = cv2.approxPolyDP(hull,0.1*cv2.arcLength(hull,True),True)
        if len(hull)==4:
            cv2.drawContours(img,[hull],0,(0,255,0),2)

cv2.imshow('img',img)
cv2.waitKey(0)
cv2.destroyAllWindows()

这里,我没有在所有平面中找到正方形,如果需要,请自己找一下。

以下是我得到的结果:

输入图像描述

希望这就是您需要的。


2
+1 我也必须点赞这个简洁的作品。做得很棒! - karlphillip
@abidrahmank 我试图将这段代码改写成C++,但它一直失败,你有什么想法是问题出在哪里吗?http://stackoverflow.com/questions/13599695/convert-opencv-python-to-c-objective-c - alandalusi
5
如果瓶子是白色的,那么轮廓会延伸到瓶子的轮廓上,因此凸包算法将无法奏效。 - user1914692
1
当有多个正方形时,此解决方案可能会失败。 - justin.yqyang

6

第一步:开始尝试使用阈值技术来将白色纸张与图像的其余部分隔离开。这是一个简单的方法:

Mat new_img = imread(argv[1]);

double thres = 200;
double color = 255;
threshold(new_img, new_img, thres, color, CV_THRESH_BINARY);

imwrite("thres.png", new_img);

但是还有其他替代方案可以提供更好的结果。其中之一是调查inRange(),另一个是通过将图像转换为HSV颜色空间检测颜色

这个线程也提供了一个有趣的讨论。

第二步:在执行其中一个过程后,您可以尝试直接将结果馈送到find_squares()中:

除了find_squares()之外的另一种方法是实现边界框技术,它具有提供更准确的矩形区域检测的潜力(前提是您具有阈值的完美结果)。我在这里这里使用了它。值得注意的是,OpenCV有自己的边界框教程

除了find_squares()之外,正如Abid在他的回答中指出的那样,另一种方法是使用convexHull方法。查看OpenCV关于此方法的C++教程以获取代码。


-1
  1. 转换为实验室空间
  2. 使用kmeans算法进行2个聚类
  3. 检测正方形中的一个内部聚类将在RGB空间中解决许多问题

那对此有何帮助? - Andrey Rubshtein
使用命令:cv::cvtColor(image, LABImage, CV_RGB2Lab); 在LAB颜色空间中,您可以获得更好的分离效果,并且对光照条件的敏感度较低。 - user3452134

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接