角点检测

时间:2015-09-15 14:37:35

标签: opencv image-processing

我是Open-CV的新手。我试图在相当简单的图像中检测90度角。我需要检测围绕物体的那个矩形的角落。我正在使用shi-Thomasi功能。以下是我的代码:

for x in range(0, 50):
    ret, frame = cap.read()

# make image gray scale
im = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) 

#finding corners 
corners = cv2.goodFeaturesToTrack(im, 1, 0.01, 10)
corners = np.int0(corners)
for i in corners:
    x, y = i.ravel()
    cv2.circle(frame, (x, y), 3, 255,-1)
cv2.imwrite("DetectedCorners.png", frame)

问题:始终检测到该对象中的某些角落。我需要一种方法,完全删除该对象,然后检测角落。enter image description here

我不知道如何删除该对象。 有什么建议 ?照片显示了我的结果。有些时候检测到周围矩形的角,有时是该复杂对象中的一些随机点。 在检测角落之前我也使用了Canny,但结果差了10倍。

1 个答案:

答案 0 :(得分:4)

嗯,快速而肮脏的C ++解决方案,只是为了证明使用Hough变换来检测线条,然后计算它们的交集。

如果需要,您最终可以将代码移植到Python。

#include <opencv2\opencv.hpp>
#include <iostream>
using namespace cv;
using namespace std;

int main()
{
    Mat3b img = imread("path_to_image");

    // Convert to grayscale
    Mat1b gray;
    cvtColor(img, gray, COLOR_BGR2GRAY);

    // Compute edges
    Mat1b edges;
    Canny(gray, edges, 400, 100);

    // Create the output result image
    Mat3b res;
    cvtColor(edges, res, COLOR_GRAY2BGR);

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

    vector<pair<Point,Point>> pts;
    vector<Point> intersections;

    for (size_t i = 0; i < lines.size(); i++)
    {
        float rho = lines[i][0], theta = lines[i][1];

        // Get 2 points on each line
        Point pt1, pt2;
        double a = cos(theta), b = sin(theta);
        double x0 = a*rho, y0 = b*rho;

        pt1.x = cvRound(x0 + 1000 * (-b));
        pt1.y = cvRound(y0 + 1000 * (a));
        pt2.x = cvRound(x0 - 1000 * (-b));
        pt2.y = cvRound(y0 - 1000 * (a));

        // Save the pair of points 
        pts.push_back(make_pair(pt1, pt2));

        // Draw lines
        line(res, pt1, pt2, Scalar(0, 0, 255), 3, CV_AA);
    }

    // for each couple of lines
    for (int i = 0; i < pts.size() - 1; ++i)
    {
        // get the two points of the first line
        const Point&  p1 = pts[i].first;
        const Point&  p2 = pts[i].second;

        for (int j = i + 1; j < pts.size(); ++j)
        {
            // Get the two points of the second line
            const Point&  p3 = pts[j].first;
            const Point&  p4 = pts[j].second;

            // Compute intersection

            Point p;
            float den = (p1.x - p2.x) * (p3.y - p4.y) - (p1.y - p2.y) * (p3.x - p4.x);
            if (den != 0) // if not parallel lines
            {
                p.x = ((p1.x*p2.y - p1.y*p2.x)*(p3.x - p4.x) - (p1.x - p2.x)*(p3.x*p4.y - p3.y*p4.x)) / den;
                p.y = ((p1.x*p2.y - p1.y*p2.x)*(p3.y - p4.y) - (p1.y - p2.y)*(p3.x*p4.y - p3.y*p4.x)) / den;

                // Draw intersection
                circle(res, p, 7, Scalar(0, 255, 0), 2);
            }

            // Save intersections
            intersections.push_back(p);
        }
    }

    return 0;
}

结果:

enter image description here