def get_homography_by_keypoints_desc(points_source, des_source, points_destin, des_destin, matchtype='knn'): M = None if points_source is None or des_source is None or points_destin is None or des_destin is None: return M src, dst, distance = tools_alg_match.get_matches_from_keypoints_desc( points_source, des_source, points_destin, des_destin, matchtype=matchtype) if src is not None: M = get_homography_by_keypoints(src, dst) if M is None: return M #if not is_homography_good(src, dst,M): # M = None return M
def align_two_images_translation(img1, img2, detector='SIFT', matchtype='knn', borderMode=cv2.BORDER_REPLICATE, background_color=(0, 255, 255)): points1, des1 = tools_alg_match.get_keypoints_desc(img1, detector) points2, des2 = tools_alg_match.get_keypoints_desc(img2, detector) coord1, coord2, distance = tools_alg_match.get_matches_from_keypoints_desc( points1, des1, points2, des2, matchtype) translation = None if coord1 is not None and coord1.size >= 8: translation = get_transform_by_keypoints(coord1, coord2) if translation is None or math.isnan(translation[0, 0]): return img1, img2, 0 #T = numpy.eye(3,dtype=numpy.float32) #T[0:2,0:2] = translation[0:2,0:2] #angle = math.fabs(rotationMatrixToEulerAngles(T)[2]) #if translation[0, 2] >= 0.10*img1.shape[0] or translation[1, 2] >= 0.10*img1.shape[1]:# or angle>0.05: # return img1, img2, 0 else: return img1, img2, 0 if borderMode == cv2.BORDER_REPLICATE: result_image1, result_image2 = get_stitched_images_using_translation( img1, img2, translation, borderMode=cv2.BORDER_REPLICATE, keep_shape=True) else: result_image1a, result_image2 = get_stitched_images_using_translation( img1, img2, translation, borderMode=cv2.BORDER_CONSTANT, background_color=(0, 255, 0), keep_shape=True) result_image1b, result_image2 = get_stitched_images_using_translation( img1, img2, translation, borderMode=cv2.BORDER_CONSTANT, background_color=(255, 0, 255), keep_shape=True) result_image1 = result_image1b.copy() idx2 = numpy.all(result_image1a != result_image1b, axis=-1) result_image1[idx2] = background_color q = ((cv2.matchTemplate( result_image1, result_image2, method=cv2.TM_CCOEFF_NORMED)[0, 0]) + 1) * 128 return result_image1, result_image2, int(q)
def example_find_matches_for_homography(detector='SIFT', matchtype='knn'): folder_input = './images/ex_keypoints/' img1 = cv2.imread(folder_input + 'left.jpg') img2 = cv2.imread(folder_input + 'rght.jpg') img1_gray_rgb = tools_image.desaturate(img1) img2_gray_rgb = tools_image.desaturate(img2) folder_output = './images/output/' if not os.path.exists(folder_output): os.makedirs(folder_output) #else: # tools_IO.remove_files(folder_output) points1, des1 = tools_alg_match.get_keypoints_desc(img1, detector) points2, des2 = tools_alg_match.get_keypoints_desc(img2, detector) match1, match2, distance = tools_alg_match.get_matches_from_keypoints_desc(points1, des1, points2, des2, matchtype) homography = tools_calibrate.get_homography_by_keypoints_desc(points1, des1, points2, des2, matchtype) if homography is None: return match2_cand = cv2.perspectiveTransform(match1.reshape(-1, 1, 2).astype(numpy.float32), homography) hit = numpy.zeros(match1.shape[0]) for i in range(0, match1.shape[0]): if numpy.linalg.norm(match2_cand[i] - match2[i]) <= 5: hit[i] = 1 img1_gray_rgb = tools_draw_numpy.draw_circle(img1_gray_rgb, int(match1[i][1]), int(match1[i][0]), 3,[32, 192, 0]) img2_gray_rgb = tools_draw_numpy.draw_circle(img2_gray_rgb, int(match2[i][1]), int(match2[i][0]), 3,[32, 192, 0]) else: img1_gray_rgb = tools_draw_numpy.draw_circle(img1_gray_rgb, int(match1[i][1]), int(match1[i][0]), 3,[0, 64, 255]) img2_gray_rgb = tools_draw_numpy.draw_circle(img2_gray_rgb, int(match2[i][1]), int(match2[i][0]), 3,[0, 64, 255]) img2_gray_rgb, img1_gray_rgb = tools_calibrate.get_stitched_images_using_homography(img2_gray_rgb, img1_gray_rgb,homography) fig = plt.figure(figsize=(12, 6)) fig.subplots_adjust(hspace=0.01) roc_auc = 0 if hit.size >= 2: fpr, tpr, thresholds = metrics.roc_curve(hit, -distance) roc_auc = auc(fpr, tpr) filename_out = folder_output + ('_ROC_%s_%s_auc_%1.3f.png' % (detector, matchtype, roc_auc)) tools_plot.plot_tp_fp(plt.subplot(1, 1, 1), fig, tpr, fpr, roc_auc) plt.savefig(filename_out) cv2.imwrite(folder_output + ('%s_%s_auc_%1.3f_left_matches.png' % (detector, matchtype, roc_auc)), img1_gray_rgb) cv2.imwrite(folder_output + ('%s_%s_auc_%1.3f_rght_matches.png' % (detector, matchtype, roc_auc)), img2_gray_rgb) return
def example_03_find_homography_by_keypoints(detector='SIFT', matchtype='knn'): folder_input = 'images/ex_keypoints/' img1 = cv2.imread(folder_input + 'left.jpg') img2 = cv2.imread(folder_input + 'rght.jpg') img1_gray_rgb = tools_image.desaturate(img1) img2_gray_rgb = tools_image.desaturate(img2) folder_output = 'images/output/' output_filename1 = folder_output + 'left_transformed_homography.png' output_filename2 = folder_output + 'rght_transformed_homography.png' output_filename = folder_output + 'blended_homography.png' if not os.path.exists(folder_output): os.makedirs(folder_output) else: tools_IO.remove_files(folder_output) points1, des1 = tools_alg_match.get_keypoints_desc(img1, detector) points2, des2 = tools_alg_match.get_keypoints_desc(img2, detector) homography = tools_calibrate.get_homography_by_keypoints_desc( points1, des1, points2, des2, matchtype) match1, match2, distance = tools_alg_match.get_matches_from_keypoints_desc( points1, des1, points2, des2, matchtype) for each in match1: img1_gray_rgb = tools_draw_numpy.draw_circle(img1_gray_rgb, int(each[1]), int(each[0]), 3, [0, 0, 255]) for each in match2: img2_gray_rgb = tools_draw_numpy.draw_circle(img2_gray_rgb, int(each[1]), int(each[0]), 3, [255, 255, 0]) result_image1, result_image2 = tools_calibrate.get_stitched_images_using_homography( img1_gray_rgb, img2_gray_rgb, homography, background_color=(255, 255, 255)) result_image = tools_image.blend_avg(result_image1, result_image2, background_color=(255, 255, 255)) # result_image = tools_calibrate.blend_multi_band(result_image1, result_image2) cv2.imwrite(output_filename1, result_image1) cv2.imwrite(output_filename2, result_image2) cv2.imwrite(output_filename, result_image) return
def demo_stereo_03_keypoints(): folder_input = 'images/ex_stereo/' folder_output = 'images/output/' filenameL = '1L.png' filenameR = '1R.png' disp_v1, disp_v2, disp_h1, disp_h2 = 0, 1, -70, -10 if not os.path.exists(folder_output): os.makedirs(folder_output) else: tools_IO.remove_files(folder_output) imgL = cv2.imread(folder_input + filenameL) imgR = cv2.imread(folder_input + filenameR) imgL_gray_rgb = tools_image.desaturate(imgL) imgR_gray_rgb = tools_image.desaturate(imgR) points1, des1 = tools_alg_match.get_keypoints_desc(imgL) points2, des2 = tools_alg_match.get_keypoints_desc(imgR) match1, match2, distance = tools_alg_match.get_matches_from_keypoints_desc( points1, des1, points2, des2) idx = [] for i in range(0, match1.shape[0]): row1, col1 = match1[i, 1], match1[i, 0] row2, col2 = match2[i, 1], match2[i, 0] if (col2 - col1 >= disp_h1) and (col2 - col1 < disp_h2) and ( row2 - row1 >= disp_v1) and (row2 - row1 < disp_v2): idx.append(i) match1, match2, distance = match1[idx], match2[idx], distance[idx] for i in range(0, match1.shape[0]): r = int(255 * numpy.random.rand()) color = cv2.cvtColor( numpy.array([r, 255, 225], dtype=numpy.uint8).reshape(1, 1, 3), cv2.COLOR_HSV2BGR) imgL_gray_rgb = tools_draw_numpy.draw_circle(imgL_gray_rgb, match1[i, 1], match1[i, 0], 4, color) imgR_gray_rgb = tools_draw_numpy.draw_circle(imgR_gray_rgb, match2[i, 1], match2[i, 0], 4, color) cv2.imwrite(folder_output + 'matches03_L.png', imgL_gray_rgb) cv2.imwrite(folder_output + 'matches03_R.png', imgR_gray_rgb) return
def get_homography_by_keypoints_desc(points_source, des_source, points_destin, des_destin, matchtype='knn'): M = None if points_source==[] or des_source==[] or points_destin==[] or des_destin==[]: return M src, dst, distance = tools_alg_match.get_matches_from_keypoints_desc( points_source, des_source, points_destin, des_destin, matchtype=matchtype) if src.size != 0: M = get_homography_by_keypoints(src, dst) return M
def align_two_images_homography(img1, img2, detector='SIFT', matchtype='knn'): img1_gray_rgb = tools_image.desaturate(img1) img2_gray_rgb = tools_image.desaturate(img2) points1, des1 = tools_alg_match.get_keypoints_desc(img1, detector) points2, des2 = tools_alg_match.get_keypoints_desc(img2, detector) match1, match2, distance = tools_alg_match.get_matches_from_keypoints_desc( points1, des1, points2, des2, matchtype) homography = None if match1.size != 0: homography = get_homography_by_keypoints_desc(points1, des1, points2, des2, matchtype) if homography is None: return img1, img2 else: return img1, img2 for each in match1: cv2.circle(img1_gray_rgb, (int(each[0]), int(each[1])), 3, [0, 0, 255], thickness=-1) for each in match2: cv2.circle(img2_gray_rgb, (int(each[0]), int(each[1])), 3, [255, 255, 0], thickness=-1) result_image1, result_image2 = get_stitched_images_using_homography( img2_gray_rgb, img1_gray_rgb, homography, borderMode=cv2.BORDER_REPLICATE, background_color=(255, 255, 255)) q = cv2.matchTemplate(result_image1, result_image2, method=cv2.TM_CCOEFF_NORMED)[0, 0] q = int((1 + q) * 128) return result_image1, result_image2, q
def example_find_matches_for_frames(filename1_in, filename2_in, folder_output, detector='SIFT', matchtype='knn'): img1 = cv2.imread(filename1_in) img2 = cv2.imread(filename2_in) if not os.path.exists(folder_output): os.makedirs(folder_output) else: tools_IO.remove_files(folder_output) points1, des1 = tools_alg_match.get_keypoints_desc(img1, detector) points2, des2 = tools_alg_match.get_keypoints_desc(img2, detector) match1, match2, distance = tools_alg_match.get_matches_from_keypoints_desc( points1, des1, points2, des2, matchtype) img1_gray_rgb = tools_image.desaturate(img1) img2_gray_rgb = tools_image.desaturate(img2) for m1, m2 in zip(match1, match2): if math.sqrt((m1[1] - m2[1]) * (m1[1] - m2[1]) + (m1[0] - m2[0]) * (m1[0] - m2[0])) > 1 and math.sqrt((m1[1] - m2[1]) * (m1[1] - m2[1]) + (m1[0] - m2[0]) * (m1[0] - m2[0])) < 55: img1_gray_rgb = tools_draw_numpy.draw_line(img1_gray_rgb, int(m1[1]), int(m1[0]), int(m2[1]), int(m2[0]), [0, 0, 255]) img2_gray_rgb = tools_draw_numpy.draw_line(img2_gray_rgb, int(m1[1]), int(m1[0]), int(m2[1]), int(m2[0]), [0, 0, 255]) cv2.imwrite(folder_output + 'out1.png', img1_gray_rgb) cv2.imwrite(folder_output + 'out2.png', img2_gray_rgb) img1_gray_rgb = tools_image.desaturate(img1) img2_gray_rgb = tools_image.desaturate(img2) for m1, m2 in zip(match1, match2): if math.sqrt((m1[1] - m2[1]) * (m1[1] - m2[1]) + (m1[0] - m2[0]) * (m1[0] - m2[0])) > 5 and math.sqrt((m1[1] - m2[1]) * (m1[1] - m2[1]) + (m1[0] - m2[0]) * (m1[0] - m2[0])) < 55: r = int(255 * numpy.random.rand()) color = cv2.cvtColor( numpy.array([r, 255, 225], dtype=numpy.uint8).reshape(1, 1, 3), cv2.COLOR_HSV2BGR) img1_gray_rgb = tools_draw_numpy.draw_circle(img1_gray_rgb, int(m1[1]), int(m1[0]), 4, color, alpha_transp=0.3) img2_gray_rgb = tools_draw_numpy.draw_circle(img2_gray_rgb, int(m2[1]), int(m2[0]), 4, color, alpha_transp=0.3) cv2.imwrite(folder_output + 'out3.png', img1_gray_rgb) cv2.imwrite(folder_output + 'out4.png', img2_gray_rgb) return