是否有任何opencv函数,如" cvHoughCircles()"用于方检?

时间:2012-06-13 12:42:50

标签: c++ image-processing opencv

是否有任何类似“cvHoughCircles()”的opencv函数可用于方检测编程 对于 CvSeq * circles = cvHoughCircles()的圆检测程序,但我找不到方形检测。

2 个答案:

答案 0 :(得分:8)

您不需要任何单独的功能。 OpenCV带有方形检测样本(实际上检测矩形,你可以添加约束,使所有边长度相等以获得正方形)。

点击此链接:squares.cpp

关于此代码在此SOF中的工作方式,有一个很好的解释:How to identify square or rectangle with variable lengths and width by using javacv?

以下是您应用该代码时获得的结果。

enter image description here

答案 1 :(得分:5)

没有opencv函数可以直接找到正方形。

但是你可以使用houghLines函数来检测线条,并找到90度角的线条之间的交叉点。

为了测量线之间的角度,我可以为您提供Java代码片段:

// returns cosine of angle between line segments 0 to 1, and 0 to 2.
// pt0 is the vertex / intersection
// angle of 90 degrees will have a cosine == 0

public static final double angleCosine(Point pt1, Point pt0, Point 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;
    return (dx1 * dx2 + dy1 * dy2) / Math.sqrt((dx1 * dx1 + dy1 * dy1) * (dx2 * dx2 + dy2 * dy2) + 1e-10);
}

关于houghLines的文档:

http://docs.opencv.org/modules/imgproc/doc/feature_detection.html?highlight=houghlines#houghlines