''' Number from 1 to 3 expected as an argument to determine which MatchShapes version to use ''' import cv2 import numpy as np import os, os.path, time, sys def main(): if len(sys.argv) > 1: matching_method = str(sys.argv[1]) else: matching_method = 1 final_result = {} for file in sorted(os.listdir("routes")): # iterate through all the routes rotation = 0 result_list = {} smallest = 100 for i in range (0,1): # determine rotations (default 0) route_name = str(file) route = cv2.imread('routes/' + route_name) route = cv2.cvtColor(route, cv2.COLOR_BGR2GRAY) blank_route = np.zeros((140,140,3), np.uint8) # blank image to draw contours on rows,cols = route.shape M = cv2.getRotationMatrix2D((cols/2,rows/2),rotation,1) dst = cv2.warpAffine(route,M,(cols,rows)) route = cv2.resize(dst, (140,140)) kernel = np.ones((2,25),np.uint8) route = cv2.dilate(route,kernel,iterations = 1) # apply dilation to make routes a bit simpler route_ret, route_thresh = cv2.threshold(route, 127, 255,0) route_contours,route_hierarchy = cv2.findContours(route_thresh,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_NONE) # find contours route_cnt1 = route_contours[0] # take first contour cv2.drawContours(blank_route, route_cnt1, -1, (0,255,0), 3) result_list = comparison(matching_method, route_name, blank_route, route_cnt1, result_list, rotation) #rotation = rotation + 90 smallest_distance = min(result_list, key=result_list.get) value_of_smallest = result_list.get(smallest_distance, "none") if value_of_smallest <= smallest: smallest = value_of_smallest final_result[route_name] = smallest_distance, value_of_smallest for k,v in sorted(final_result.items()): print(k, v) def comparison(matching_method, route_name, blank_route, route_cnt1, result_list, rotation): combined_image = np.zeros((140,140,3), np.uint8) sample_images = np.zeros((140,140,3), np.uint8) for file in os.listdir("samples"): sample_name = str(file) blank_sample = np.zeros((140,140,3), np.uint8) # blank image to draw contours on sample = cv2.imread('samples/' + sample_name) sample = cv2.cvtColor(sample, cv2.COLOR_BGR2GRAY) sample = cv2.resize(sample, (140,140)) sample = cv2.bitwise_not(sample) # invert black and white sample_ret, sample_thresh = cv2.threshold(sample, 127, 255,0) # thresholding sample_contours,sample_hierarchy = cv2.findContours(sample_thresh,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_NONE) sample_cnt1 = sample_contours[0] # take first contour approx2 = cv2.approxPolyDP(sample_cnt1, 0.01*cv2.arcLength(sample_cnt1, True), True) # approximation can be used for contours cv2.drawContours(blank_sample, sample_cnt1, -1, (0,255,0), 3) sample_images = np.concatenate((sample_images, blank_sample), axis=1) # show all the sample images #cv2.imshow("sample", sample_images) if(int(matching_method) == 1): #result = cv2.matchShapes(route_cnt1, sample_cnt1,cv2.CONTOURS_MATCH_I1,0.0) result = cv2.matchShapes(route_cnt1, sample_cnt1,1,0.0) result_list[str(file)] = result elif(int(matching_method) == 2): result = cv2.matchShapes(route_cnt1,sample_cnt1,cv2.CONTOURS_MATCH_I2,0.0) result_list[str(file)] = result elif(int(matching_method) == 3): result = cv2.matchShapes(route_cnt1,sample_cnt1,cv2.CONTOURS_MATCH_I3,0.0) result_list[str(file)] = result print("Difference between ", sample_name, "and ", route_name, result) combined_image = np.concatenate(( blank_route,combined_image), axis=0) #cv2.imshow("Comparison", combined_image) #cv2.waitKey(0) return result_list main()