I want to find custom shape in OpenCV. Shape is previously defined. I want to detect if this shape is equal or not equal to the current shape using WebCam in real time.
How can I do that? How to compare pre-defined custom shape and current shape?
Python - OpenCV & PyQT5 together To detect a triangle in an image, we first detect all the contours in the image. Then we loop over all the contours. Find the approximate contour for each of the contours. If the number of vertex points in the approximate contour is 3, then draw the contour and set it as a triangle.
Compute the aspect ratio of the contour cnt. Set a range of aspect ratios to detect the square. We set it [0.9, 1.1]. If the ratio is between 0.9 and 1.1, the detected contour is a square else it is a rectangle.
The first facility OpenCV offers to calculate the approximate bounding polygon of a shape is cv2. approxPolyDP. This function takes three parameters: A contour.
As the commenters have eluded, you can use template matching to detect custom shapes.
So What Is Template Matching?
Template matching is a technique in digital image processing for finding small parts of an image which match a template image. It can be used in manufacturing as a part of quality control, a way to navigate a mobile robot, or as a way to detect edges in images.
Template Matching In OpenCV/Python
Template Matching is a method for searching and finding the location of a template image in a larger image. OpenCV comes with a function cv2.matchTemplate() for this purpose. It simply slides the template image over the input image (as in 2D convolution) and compares the template and patch of input image under the template image.
I have taken this text from here which is a tutorial on how to perform template matching in OpenCV. You can search for any "custom shape" that you define. It includes code samples for python.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With