Esempio n. 1
0
def example_04_find_homography_blend_multi_band(detector='SIFT', matchtype='knn'):

    folder_input = 'images/ex_keypoints/'
    img_left = cv2.imread(folder_input + 'left.jpg')
    img_right = cv2.imread(folder_input + 'rght.jpg')

    folder_output = 'images/output/'
    output_left = folder_output + 'left_out.jpg'
    output_rght = folder_output + 'rght_out.jpg'

    if not os.path.exists(folder_output):
        os.makedirs(folder_output)
    else:
        tools_IO.remove_files(folder_output)

    points_left, des_left = tools_alg_match.get_keypoints_desc(img_left,detector)
    points_rght, des_rght = tools_alg_match.get_keypoints_desc(img_right,detector)

    homography = tools_calibrate.get_homography_by_keypoints_desc(points_left, des_left, points_rght, des_rght)
    result_left, result_right= tools_calibrate.get_stitched_images_using_homography(img_left, img_right, homography)
    result_image = tools_image.blend_multi_band(result_left,result_right)
    cv2.imwrite(output_left, result_image)

    homography = tools_calibrate.get_homography_by_keypoints_desc(points_rght, des_rght, points_left, des_left)
    result_right, result_left= tools_calibrate.get_stitched_images_using_homography(img_right, img_left, homography)
    result_image = tools_image.blend_multi_band(result_left,result_right)
    cv2.imwrite(output_rght, result_image)
    return
Esempio n. 2
0
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)
Esempio n. 3
0
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
Esempio n. 4
0
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
Esempio n. 5
0
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
Esempio n. 6
0
def demo_stereo_04_keypoints_limited():
    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_desc_limit_by_disp(
        points1, des1, points2, des2, disp_v1, disp_v2, disp_h1, disp_h2,
        'ccc')

    R = 4

    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], R, color)
        imgR_gray_rgb = tools_draw_numpy.draw_circle(imgR_gray_rgb, match2[i,
                                                                           1],
                                                     match2[i, 0], R, color)

    cv2.imwrite(folder_output + 'matches03_L.png', imgL_gray_rgb)
    cv2.imwrite(folder_output + 'matches03_R.png', imgR_gray_rgb)

    return
Esempio n. 7
0
def example_ORB(filename_in, filename_out, R=2):
	img = cv2.imread(filename_in)
	gray_rgb = tools_image.desaturate(img)

	points, desc = tools_alg_match.get_keypoints_desc(img, detector='ORB')

	for each in points:
		gray_rgb = tools_draw_numpy.draw_circle(gray_rgb, each[1], each[0], R, [0, 0, 255])

	cv2.imwrite(filename_out, gray_rgb)
	return
Esempio n. 8
0
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
Esempio n. 9
0
def example_03_find_homography_live():
    USE_CAMERA = False
    USE_TRANSFORM = True

    filename_out = './images/output/frame.jpg'
    image_deck = cv2.imread('./images/ex_homography_live/frame2.jpg')
    image_card = cv2.imread('./images/ex_homography_live/card.jpg')
    image_sbst = cv2.imread('./images/ex_homography_live/card2.jpg')

    image_sbst = cv2.resize(image_sbst,
                            (image_card.shape[1], image_card.shape[0]))

    points1, des1 = tools_alg_match.get_keypoints_desc(image_card, 'SURF')
    if USE_CAMERA:
        capture = cv2.VideoCapture(0)

    while (True):
        if USE_CAMERA:
            ret, image_deck = capture.read()

        points2, des2 = tools_alg_match.get_keypoints_desc(image_deck, 'SURF')

        if USE_TRANSFORM:
            H = tools_calibrate.get_transform_by_keypoints_desc(
                points1, des1, points2, des2, 'knn')
        else:
            H = tools_calibrate.get_homography_by_keypoints_desc(
                points1, des1, points2, des2, 'knn')

        if (H is not None):
            if USE_TRANSFORM:
                aligned1, aligned2 = tools_calibrate.get_stitched_images_using_translation(
                    image_sbst,
                    image_deck,
                    H,
                    background_color=(0, 0, 0),
                    keep_shape=True)
            else:
                aligned1, aligned2 = tools_calibrate.get_stitched_images_using_homography(
                    image_sbst, image_deck, H, background_color=(0, 0, 0))
            #im_result = tools_image.put_layer_on_image(aligned2,aligned1,background_color=(0,0,0))
            im_result = tools_image.blend_multi_band_large_small(
                aligned2,
                aligned1,
                background_color=(0, 0, 0),
                filter_size=10,
                leveln_default=1)
            cv2.imshow('frame', im_result)
        else:
            cv2.imshow('frame', image_deck)

        key = cv2.waitKey(1)
        if key & 0xFF == 27: break
        if (key & 0xFF == 13) or (key & 0xFF == 32):
            cv2.imwrite(filename_out, image_deck)

    if USE_CAMERA:
        capture.release()

    cv2.destroyAllWindows()
    return
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