角点检测 [英] Corner Detection

查看:263
本文介绍了角点检测的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am new in Open-CV.I am trying to detect 90 degree corner in fairly simple image.I need to detect corners of that rectangle which surround the object. I am using shi-Thomasi feature. following is my code :

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)

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

Problem: Always some corners in that object is detected. I need a method, to totally remove that object, and then detects the corners.

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

I don't know how to remove that object. Any suggestions ? Photo shows my result.some times corners of surrounding rectangle are detected, some times some random points in that complex object. I also used Canny before detecting corners, but result was 10 times worse.

推荐答案

嗯,快速而肮脏的C ++解决方案,仅用于使用Hough变换检测线条的概念证明,然后计算他们的交集。

Well, quick and dirty C++ solution, just for a proof of concept regarding using Hough transform to detect lines, and then compute their intersection.

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

You can eventually port the code to Python if needed.

#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;
}

结果:

这篇关于角点检测的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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