Ejemplo n.º 1
0
def compareAndShow(img1: ImageWithPoints, img2: ImageWithPoints):
    pts_match_1, pts_match_2, match_good = ho.matchKeypoints(img1.kp, img1.des, img2.kp, img2.des)
    H_tf, mask_tf = ho.findHomography(pts_match_1, pts_match_2, 4)
    H_cv, mask_cv = ho.findHomographyCV(pts_match_1, pts_match_2, 4)

    image = ho.polyline(img2.points, img2.image, color=(0, 0, 255))

    image_tf = ho.polyline(ho.project(H_tf, img1.points), image, color=(255, 0, 0))
    image_cv = ho.polyline(ho.project(H_cv, img1.points), image, color=(0, 255, 0))

    while True:
        cv2.imshow('image', image)
        cv2.imshow('tf', image_tf)
        cv2.imshow('cv', image_cv)
        k = cv2.waitKey(10) & 0xFF
        if k == 27:
            break
Ejemplo n.º 2
0
def normalizationBox(img1, img2):
    pts_match_1, pts_match_2, match_good = ho.matchKeypoints(img1.kp, img1.des, img2.kp, img2.des)

    pts = np.asarray([[1, 1], [-1, 1], [-1, -1], [1, -1]])
    H, mask = ho.ransac(pts_match_1, pts_match_2, 4)
    N_1 = ho.findNormalizationMatrix(pts_match_1, mask)
    N_1_inv = np.linalg.inv(N_1)

    N_2 = ho.findNormalizationMatrix(pts_match_2, mask)
    N_2 = np.matmul(N_1, np.linalg.inv(H))
    N_2_inv = np.linalg.inv(N_2)

    image1 = ho.polyline(ho.project(N_1_inv, pts), img1.image)
    image2 = ho.polyline(ho.project(N_2_inv, pts), img2.image)

    mask = (mask * 1).tolist()
    both = ho.drawMatches(img1.kp, img2.kp, match_good, image1, image2, mask)

    cv2.imshow("both", both)
    cv2.waitKey(0)
Ejemplo n.º 3
0
def video(img1: ImageWithPoints, img2: ImageWithPoints):
    pts_match_1, pts_match_2, match_good = ho.matchKeypoints(img1.kp, img1.des, img2.kp, img2.des)
    H, mask = ho.findHomography(pts_match_1, pts_match_2, 2, epochs=0, learning_rate=0.3, method=2)
    H = np.asarray([[1, 0, 0], [0, 1, 0], [0, 0, 1]])
    with ho.Graph() as graph:
        for i in range(0, 100):
            if i < 50:
                H, mask = ho.findHomography(pts_match_1, pts_match_2, 2, epochs=1, learning_rate=0.15, H=H, mask=mask, method=0, graph=graph, normalization=1)
                image = ho.polyline(ho.project(H, img1.points), img2.image, color=(0, 0, 255))
            else:
                H, mask = ho.findHomography(pts_match_1, pts_match_2, 2, epochs=5, learning_rate=0.3, H=H, mask=mask, method=0, graph=graph, normalization=1)
                image = ho.polyline(ho.project(H, img1.points), img2.image, color=(0, 255, 0))

            image2 = cv2.warpPerspective(img1.image, H, (image.shape[1], image.shape[0]))
            image3 = image2/256*0.25 + image/256*0.75
            cv2.imshow('frame4', image3)

            cv2.imshow('image', image)
            k = cv2.waitKey(10) & 0xFF
            if k == 27:
                break
Ejemplo n.º 4
0
H = np.identity(3)
_, mask = ho.findHomography(pts_match_1, pts_match_2)

# wait
cv2.imshow('frame', image_test)
k = cv2.waitKey(1000) & 0xFF

with ho.Graph() as graph:
    iter = 0
    for i in range(0, 140):
        if i < 50:
            iter += 1
            # 50 times 1 epoch
            H, mask = ho.findHomography(pts_match_1, pts_match_2, 2, epochs=1, learning_rate=0.3, H=H, mask=mask, method=0, graph=graph, normalization=1)
            error = ho.distanceError(H, pts_match_1, pts_match_2, mask).mean()
            image = ho.polyline(ho.project(H, corners), image_test, color=(0, 0, 255)) #red
            image = cv2.putText(image,"iteration "+str(iter)+",  error: "+str(error),(10,50),cv2.FONT_HERSHEY_SIMPLEX,1,(0,0,0),2,cv2.LINE_AA)
            
        else:
            iter += 5
            # 90 times 5 epochs
            H, mask = ho.findHomography(pts_match_1, pts_match_2, 2, epochs=5, learning_rate=0.3, H=H, mask=mask, method=0, graph=graph, normalization=1)
            error = ho.distanceError(H, pts_match_1, pts_match_2, mask).mean()
            image = ho.polyline(ho.project(H, corners), image_test, color=(0, 255, 0)) #green
            image = cv2.putText(image,"iteration "+str(iter)+", error: "+str(error),(10,50),cv2.FONT_HERSHEY_SIMPLEX,1,(0,0,0),2,cv2.LINE_AA)

        image2 = cv2.warpPerspective(image_reference, H, (image_test.shape[1], image_test.shape[0]))
        image3 = image2/256*0.5 + image/256*0.5
        cv2.imshow('frame', image3)
        k = cv2.waitKey(10) & 0xFF
        if k == 27:
Ejemplo n.º 5
0
"""Finds keypoints between two images and calculates a homography.
Must be run from the parent directory, e.g.: .../Mehrkamera/src> python -m examples.example1
"""
import numpy as np
import cv2
import modules.homography as ho

image_test = cv2.imread("res/graffiti/1.png")
image_reference = cv2.imread("res/graffiti/ref.png")
corners = np.float32([[54, 26], [2672, 54], [2679, 1905], [21, 1900]])

# resize the reference image
image_reference = cv2.resize(image_reference, (0, 0), fx=0.25, fy=0.25)
corners /= 4

points_reference, points_test, _ = ho.match(image_reference, image_test)

matrix, _ = ho.findHomography(points_reference, points_test)

corners_projected = ho.project(matrix, corners)

test_with_corners = ho.polyline(corners_projected, image_test)

cv2.imshow('test', test_with_corners)
cv2.waitKey(0)
cv2.destroyAllWindows()