If you use the code below to detect objects, multiple detection windows will be displayed for one object.(I think it's because the object was recognized as multiple because it was slightly separated on the image.) One detection window is enough, so please let me know if you know the code that can extract one detection window from multiple detection windows.Cv2.contourArea(contour) did not work even if I narrowed down the area that could be displayed.
img=cv2.imread(image)
im2,contours,hierarchy=cv2.findContours(img,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE
# draw tour
for tour in contents:
if cv2.contourArea(contour)<1000:
continue
# rectangle area
x,y,w,h=cv2.boundingRect(contour)
# draw tour
img5 = cv2.rectangle(img,(x,y),(x+w,y+h),(0,255,0),3)
return img5
To choose one of the multiple contours found, you must have a criterion (a method to compare the two extracted contours and choose between them).
For example:
·Select the one with the larger contour area
·Select the one with the shape of the outline closer to the circle
·Select a contour whose aspect ratio is close to 1 (a contour close to a square)
Once the criteria have been determined, you can narrow it down to one by repeating the task of comparing the detected contours and excluding one.
© 2024 OneMinuteCode. All rights reserved.