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.
-
2Given that detecting squares is substantially easier than detecting circles, I wouldn't be surprised if squarefinding was left as an exercise to the coder. You basically have to find long collinear connected segments which intersect at approximately right angles, and you can generate those segments using a bit of filtering and an edge detector. Most of the tools you need to do that should already be in OpenCV. – Rook Jun 13 '12 at 12:48
-
And to do that you will use Hough transform for lines, which is also implemented in OpenCV ;) – CTZStef Jun 13 '12 at 13:33
-
1Refer to this: http://stackoverflow.com/questions/10533233/opencv-c-obj-c-advanced-square-detection There are three or four approaches you could try under that link. Enjoy :) – ChathuraSam Apr 13 '14 at 20:12
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.

- 1
- 1

- 51,886
- 31
- 146
- 157
-
bt when i'm trying to run program, that shows 2 errors because of **#include "opencv2/core/core.hpp"** and **#include "opencv2/imgproc/imgproc.hpp" ** ///// Error- fatal error C1083: Cannot open include file: 'opencv2/core/core.hpp': No such file or directory.could you please tell me the way to solve this error – Thar1988 Jun 13 '12 at 15:47
-
I am sorry i am not using C++. May be ask it as a separate question, saying your new question is continuation of this question. – Abid Rahman K Jun 13 '12 at 17:31
-
You should change those includes so that their path point to the place you installed those files. It should be a relative path, not a full one. – Rui Marques Jun 13 '12 at 18:25
-
Can you please explain about how to calculating width and height of those identified polygons ? – SL_User Jun 26 '12 at 09:43
-
if you check, you can see finding angle from 3 points. You can find distance between two consecutive points. Or find bounding rect or rotated rect for the square. It directly gives you width and height. But may be wrong if it is not an exact rectangle – Abid Rahman K Jun 26 '12 at 14:20
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

- 8,567
- 3
- 60
- 91