Thar1988
Thar1988

Reputation: 511

How to detect square shape object in a video using opencv?

I want to build an program that can detect square shape object in a video. i used SURF algorithm for that. but that only detect key points.

Upvotes: 0

Views: 5695

Answers (2)

Abid Rahman K
Abid Rahman K

Reputation: 52646

The code for square detection ( specifically rectangle, you can modify a little bit to work it for squares only) comes directly with opencv samples and you get it when you download the OpenCV library.

You didn't specify the language you work. But the code comes in Python and C++.

Python , C++

How it works:

  • Split image in to R,G,B plane
  • For each plane, threshold image for a range values in 0 to 255
  • Find the contours, approximate, and select the ones with only 4 points
  • Find cosine of angle between all the lines of the contour and check if close to 90
  • If so, it is rectangle
  • If you want square, check if its all sides are almost equal.

It works pretty well. And if you have seen this, and this is not what you want, update your question with more specific details, including some test images.

Hope it helps!!!

Upvotes: 0

Martin Beckett
Martin Beckett

Reputation: 96109

The normal way would be to detect edges with a canny filter then a hough transform to find lines and then find pairs of lines with slopes that are 90deg different

Upvotes: 4

Related Questions