5

是否有像“cvHoughCircles()”这样的opencv函数可以用于圆形检测程序的正方形检测编程,即CvSeq* circles = cvHoughCircles(),但我找不到正方形检测。

4

2 回答 2

8

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

检查此链接:squares.cpp

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

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

在此处输入图像描述

于 2012-06-13T14:11:23.040 回答
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

于 2012-06-13T13:33:02.830 回答