Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are there any opencv function like "cvHoughCircles()" for square detection?

Are there any opencv function like "cvHoughCircles()" that can use for square detection programming for circle detection program that is CvSeq* circles = cvHoughCircles() but i couldn't find for square detection.

like image 521
Thar1988 Avatar asked Jun 13 '12 12:06

Thar1988


2 Answers

You don't need any separate function for that. OpenCV comes with square detection sample( which actually detects rectangles, you can add constraint that all sides should be equal in length to get square).

Check this link : squares.cpp

There is a good explanation on how this code works in this SOF : How to identify square or rectangle with variable lengths and width by using javacv?

Below is the result you get when apply that code.

enter image description here

like image 56
Abid Rahman K Avatar answered Nov 02 '22 01:11

Abid Rahman K


There is no opencv function to directly find squares.

But you can use houghLines function, which detects lines, and find intersections between lines with 90 degree angles.

To measure angles between lines I can provide you a Java code snippet:

// 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);
}

Docs about houghLines:

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

like image 5
Rui Marques Avatar answered Nov 02 '22 00:11

Rui Marques