-1

I am trying to perform image registration and my registration output is completely bad .

Following is my code , the images that i posses are models of wounds acquired at different camera angle, links for the acquired images and output image is provided below for your review

import numpy as np
import cv2
from matplotlib import pyplot as plt

im1 = cv2.imread('/home/Documents/image_registration/1.jpg')          # Image that needs to be registered.
im2 = cv2.imread('/home/Documents/image_registration/3.jpg') # trainImage

img1 = cv2.cvtColor(im1, cv2.COLOR_BGR2GRAY)
img2 = cv2.cvtColor(im2, cv2.COLOR_BGR2GRAY)

# Initiate ORB detector
orb = cv2.ORB_create(5000)  #Registration works with at least 50 points

# find the keypoints and descriptors with orb
kp1, des1 = orb.detectAndCompute(img1, None)  #kp1 --> list of keypoints
kp2, des2 = orb.detectAndCompute(img2, None)

#Brute-Force matcher takes the descriptor of one feature in first set and is 
 # create Matcher object

matcher = cv2.DescriptorMatcher_create(cv2.DESCRIPTOR_MATCHER_BRUTEFORCE_HAMMING)

# Match descriptors.
matches = matcher.match(des1, des2, None)  #Creates a list of all matches, just like keypoints

# Sort them in the order of their distance.
matches = sorted(matches, key = lambda x:x.distance)

#Like we used cv2.drawKeypoints() to draw keypoints, 
#cv2.drawMatches() helps us to draw the matches. 
 
img3 = cv2.drawMatches(im1,kp1, im2, kp2, matches[:500], None)

#cv2.imshow("Matches image", img3)
#cv2.waitKey(0)

#Now let us use these key points to register two images. 
#Can be used for distortion correction or alignment
 
#Second set to #trainIdx. 

points1 = np.zeros((len(matches), 2), dtype=np.float32)  #Prints empty array of size equal to (matches, 2)
points2 = np.zeros((len(matches), 2), dtype=np.float32)

for i, match in enumerate(matches):
   points1[i, :] = kp1[match.queryIdx].pt    #gives index of the descriptor in the list of query descriptors
   points2[i, :] = kp2[match.trainIdx].pt    #gives index of the descriptor in the list of train descriptors

 
  
h, mask = cv2.findHomography(points1, points2, cv2.RANSAC)
 
  # Use homography
height, width, channels = im2.shape
im1Reg = cv2.warpPerspective(im1, h, (width, height))  #Applies a perspective transformation to an image.
   
print("Estimated homography : \n",  h)

#cv2.imshow("Registered image", im1Reg)
#cv2.waitKey()

cv2.imwrite ( '/home/Documents/image_registration/output.jpg' , im1Reg) 

image 1 enter image description here

image 3

enter image description here

Output registration enter image description here

Kindly suggest me the problem with my approach

DevanDev
  • 161
  • 1
  • 2
  • 17
  • 1
    draw the matches. there's a function for that. and don't use ORB. use akaze or sift. – Christoph Rackwitz Feb 11 '22 at 12:28
  • @ChristophRackwitz , sift is not working any more with openCV new version , could you please provide me some example how to approach this problem – DevanDev Feb 11 '22 at 12:35
  • of course it works. why would you say otherwise? if you need an example, use `samples/python/find_obj.py` to be found in OpenCV source – Christoph Rackwitz Feb 11 '22 at 12:37
  • @ChristophRackwitz sift is deprecated : sift = cv2.xfeatures2d.SIFT_create() [ WARN:0@6.010] global /io/opencv_contrib/modules/xfeatures2d/misc/python/shadow_sift.hpp (13) SIFT_create DEPRECATED: cv.xfeatures2d.SIFT_create() is deprecated due SIFT tranfer to the main repository. https://github.com/opencv/opencv/issues/16736 – DevanDev Feb 11 '22 at 12:57
  • well yes, it was moved from contrib to main repository several years ago. it's no longer in `xfeatures2d`. the message says as much, even if it's not very clear about what you should do. – Christoph Rackwitz Feb 11 '22 at 13:05

1 Answers1

0

in python tuples are immutable and sort function (in python list) is sorting in place and that's why it doesn't exist on tuples. to sort the tuples you should use the sorted function which returns a new list

in your case -

sorted(matches, key = lambda x: x.distance)
gil
  • 2,388
  • 1
  • 21
  • 29
  • I unable to register the images using the procedure, i am editing my answer, please check my question – DevanDev Feb 10 '22 at 14:14
  • Hi, I have updated my question, could you please check it and let me know how i possibly could resolve registration issue – DevanDev Feb 10 '22 at 16:43