Is there an algorithm to find known size and shape object in an image?

233 Views Asked by At

Since image processing and computer vision aren't of my field of study I'm having difficulty finding an algorithm that can identify the positions of rectangles of known size and proportion in certain images to automate a process.

These are grayscale images containing some circles and only one or none white rectangle with rounded edges, as can be seen in the example figure below.

enter image description here 3 different imagens with the "same" retangle to be found

Thank you

1

There are 1 best solutions below

0
Mogarbobac On

Try OpenCV, it stands for Open Computer Vision. It’s free. This is written in Python.

import cv2
import numpy as np

img = cv2.imread("C:\\Users\\mogar\\Desktop\\temp.jpg")
grayImage = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
ret, thresh = cv2.threshold(grayImage, 160, 255, cv2.THRESH_BINARY)
kernel = np.ones((5,5),np.uint8)
thresh = cv2.erode(thresh,kernel,iterations = 1)

#thresh = np.invert(thresh)

cv2.imshow("Threholded Image", thresh)
cv2.waitKey(0) & 0xFF == ord('q')
cv2.destroyAllWindows()

_, contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)
for cnts in contours:
    rect = cv2.minAreaRect(cnts)
    box = cv2.boxPoints(rect)
    box = np.int0(box)
    (x,y),(w,h),angle = rect
    w = int(w)
    h = int(h)
    area = w*h
    if area > 10000 and area < 100000:
        print("Area Check", area)
        cv2.drawContours(img, [box], 0, (0,0,255), 5)
        
        small = cv2.resize(img, (0,0), fx=0.3, fy=0.3)
        cv2.imshow("Contours", small)
        cv2.waitKey(0) & 0xFF == ord('q')
        cv2.destroyAllWindows()

enter image description here

You may need to adjust some of the threshold values and area values so that you'll be enclosing only the rectangles. You'll notice the rectangles are not fully enclosed right now, that is literally because the text is getting in the way and cutting the rectangles in half. If you had a clean image; I'm sure this would work great. If you have any questions please don't hesitate the ask, but it may take some time before I can answer.