简体   繁体   中英

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

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.

在此处输入图片说明 3 different imagens with the "same" retangle to be found

Thank you

Try OpenCV, it stands for Open Computer Vision. Its 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()

在此处输入图片说明

Hello, 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 arrant 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM