import cv2 from matplotlib import pyplot as plt from numpy import floor method = cv2.TM_SQDIFF_NORMED # Read the images from the file energy_image = cv2.imread('glyphs-a-energy.png', cv2.IMREAD_GRAYSCALE) large_image = cv2.imread('radofdispear.png', cv2.IMREAD_GRAYSCALE) orb = cv2.ORB_create() # screenshot = cv2.resize(large_image, down_points, interpolation= cv2.INTER_LINEAR) for i in range(64): mod8 = i % 8 x1 = (int(floor(i / 8))) * 256 y1 = (int(mod8)) * 192 x2 = x1 + 256 y2 = y1 + 192 # print(x1, y1, x2, y2) small_image = energy_image[y1:y2, x1:x2] # print(small_image) plt.imshow(small_image) plt.show() kp1, des1 = orb.detectAndCompute(small_image, None) kp2, des2 = orb.detectAndCompute(large_image, None) img1 = small_image img2 = large_image # result = cv2.matchTemplate(small_image, large_image, method) # print(result) bf = cv2.BFMatcher(cv2.NORM_HAMMING, crossCheck=True) # Match descriptors. matches = bf.match(des1, des2) # Sort them in the order of their distance. matches = sorted(matches, key=lambda x: x.distance) # Draw first 10 matches. img3 = cv2.drawMatches(img1, kp1, img2, kp2, matches[:10], None, flags=cv2.DrawMatchesFlags_NOT_DRAW_SINGLE_POINTS) img3 = cv2.resize(img3, dsize=(int(3840 / 2), int(2180 / 2))) cv2.imshow('output', img3) cv2.waitKey(0) break # break # # We want the minimum squared difference # mn, _, mnLoc, _ = cv2.minMaxLoc(result) # # # # Draw the rectangle: # # Extract the coordinates of our best match # MPx, MPy = mnLoc # # print(MPx, MPy) # # # # # Step 2: Get the size of the template. This is the same size as the match. # # trows, tcols = small_image.shape[:2] # # # # # Step 3: Draw the rectangle on large_image # # cv2.rectangle(large_image, (MPx, MPy), (MPx + tcols, MPy + trows), (0, 0, 255), 2) # cv2.imshow('output', small_image) # cv2.waitKey(0) # break # # Display the original image with the rectangle around the match. # cv2.imshow('output', large_image) # # # The image is only displayed if we call this #