Interpreting OpenCV cv2.matchShapes weird values using python -
i using matchshapes compare skewed triangle set of 3 shapes (a triangle, circle , square). problem similar seems either square or circle, hence "weird" in title.
i read documentation don't know hu invariants , think might of problem.
my scenario:
i have image1: [ triangle square circle ]
another image2: [ skewed_triangle ]
after computations getting how similiar contour in image 2 each contour in image 1: [0.189, 6.194, 0.159]
now, have bunch of questions:
a) how should read "match" metric? (i assuming bigger better)
b) should clip values range? maybe values in [0, 1] relevant?
c) doing wrong in code? (i tried 3 different methods , different thresholds canny edges)
in end, looking way know shape similar template. expecting find triangle had either biggest value or (maybe?) smallest value. not in between.
below code using.
import numpy np import cv2 import matplotlib.pyplot plt ## creating image 1 - triangle, square , circle, next each other. im_1 = np.zeros((200,500,3), np.uint8) #data = helper_dummychannels() pts = np.array([ [100,50], [50, 150], [150, 150], ], np.int32) pts = pts.reshape((-1,1,2)) cv2.fillpoly(im_1, [pts], (0,255,0)) cv2.rectangle(im_1, (200,50), (300, 150), (0,255,0), -1) cv2.circle(im_1, (400, 100), 50, (0,255,0), -1) plt.imshow(im_1) ## data has triangle, rectangle , circle next 1 plt.show() ## creating image 2 - skewed triangle im_2 = np.zeros((200,200,3), np.uint8) pts = np.array([ [130,50], [50, 150], [140, 150], ], np.int32) pts = pts.reshape((-1,1,2)) cv2.fillpoly(im_2, [pts], (0,255,0)) plt.imshow(im_2) plt.show() # grayscaling use in canny filter gray_1 = cv2.cvtcolor(im_1, cv2.color_bgr2gray) gray_2 = cv2.cvtcolor(im_2, cv2.color_bgr2gray) #plt.imshow(gray_1) #plt.show() #plt.imshow(gray_2) #plt.show() # canny edges , contours edged_1 = cv2.canny(gray_1, 30, 200) _, contours_1, hierarchy_1 = cv2.findcontours(edged_1, cv2.retr_external, cv2.chain_approx_none) edged_2 = cv2.canny(gray_2, 30, 200) _, contours_2, hierarchy_2 = cv2.findcontours(edged_2, cv2.retr_external, cv2.chain_approx_none) ## making sure contours ok plt.imshow(cv2.drawcontours(im_1.copy(), contours_1, -1, (255,0,0), 5)) plt.show() plt.imshow(cv2.drawcontours(im_2.copy(), contours_2, -1, (255,0,0), 5)) plt.show() ## computing cv2.matchshapes - of 3 shapes similar skewed triangle? #matches = [ cv2.matchshapes(contours_2[0], x, cv2.contours_match_i1, 0.0) x in contours_1 ] #matches = [ cv2.matchshapes(contours_2[0], x, cv2.contours_match_i2, 0.0) x in contours_1 ] matches = [ cv2.matchshapes(contours_2[0], x, cv2.contours_match_i3, 0.0) x in contours_1 ] print(matches)
Comments
Post a Comment