We are conducting shape detection using Python openCV. We find approx_poly_dp and then count number of vertices. However, the process does not work when object has many intersections (seen in second picture below). Opencv cannot detect individual objects, and just finds one large shape. In dealing with intersections, what is best way to find shapes, given general picture inputs. Or is this not a functionality of opencv, maybe better suited with machine learning?
image_original = cv2.imread(file_name)
image_gray = cv2.cvtColor(image_original, cv2.COLOR_BGR2GRAY)
image_blur = cv2.GaussianBlur(image_gray, (5, 5), 0)
image_morph = cv2.morphologyEx(image_blur, cv2.MORPH_OPEN, kernel)
image_canny = cv2.Canny(image_morph, 50, 50)
image_dilate = cv2.dilate(image_canny, kernel, iterations=2)
image_final = cv2.erode(image_dilate, kernel, iterations=1)
contours, hierarchy = cv2.findContours(image_final, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
peri = cv2.arcLength(contours, True)
approx = cv2.approxPolyDP(contours, 0.04 * peri, True)
if len(approx) == 3:
shape = "triangle"
elif len(approx) == 4:
(x, y, w, h) = cv2.boundingRect(approx)
ar = w / float(h)
shape = "square" if ar >= 0.95 and ar <= 1.05 else "rectangle"
elif len(approx) == 5:
shape = "pentagon"
else:
shape = "circle"
Intersection Picture:
from Python opencv detect shapes with intersections


No comments:
Post a Comment