如何在 Python 中仅通过平移和旋转无扭曲对齐图像

如何解决如何在 Python 中仅通过平移和旋转无扭曲对齐图像

我需要对齐两个稍微移动并旋转 180 度的图像。彼此相对。我尝试了几种使用 opencv(在 Python 中)的方法,但没有成功。

方法 1 使用 MOTION_AFFINE:

   im1 = cv2.imread(file1)    # Reference image. 
   im2 = cv2.imread(file2)  # Image to be aligned. 

   # Convert images to grayscale for computing the rotation via ECC method
   im1_gray = cv2.cvtColor(im1,cv2.COLOR_BGR2GRAY)
   im2_gray = cv2.cvtColor(im2,cv2.COLOR_BGR2GRAY)

   # Find size of image1
   sz = im1.shape

   # Define the motion model - euclidean is rigid (SRT)
   warp_mode = cv2.MOTION_AFFINE

   # Define 2x3 matrix and initialize the matrix to identity matrix I (eye)
   warp_matrix = np.eye(2,3,dtype=np.float32)

   # Specify the number of iterations.
   number_of_iterations = 5000;

   # Specify the threshold of the increment
   # in the correlation coefficient between two iterations
   termination_eps = 1e-3;

   # Define termination criteria
   criteria = (cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT,number_of_iterations,termination_eps)

   # Run the ECC algorithm. The results are stored in warp_matrix.
   (cc,warp_matrix) = cv2.findTransformECC (im1_gray,im2_gray,warp_matrix,warp_mode,criteria,None,1)

   # Warp im2 using affine
   im2_aligned = cv2.warpAffine(im2,(sz[1],sz[0]))#,flags=cv2.INTER_LINEAR + cv2.WARP_INVERSE_MAP);
   
   # Save the output. 
   cv2.imwrite(outfile,im2_aligned)

这甚至没有收敛。

方法 2 使用特征匹配,如下所示:

    im1 = cv2.imread(file1)    # Reference image. 
    im2 = cv2.imread(file2)  # Image to be aligned. 

    img1 = cv2.cvtColor(img1_color,cv2.COLOR_BGR2GRAY) 
    img2 = cv2.cvtColor(img2_color,cv2.COLOR_BGR2GRAY) 
 
    height,width = img2.shape 
  
    # Create ORB detector with 4000 features. 
    orb_detector = cv2.ORB_create(4000) 


    # The first arg is the image,second arg is the mask 
    #  (which is not reqiured in this case). 
    kp1,d1 = orb_detector.detectAndCompute(img1,None) 
    kp2,d2 = orb_detector.detectAndCompute(img2,None) 
  
    # Match features between the two images. 
    # We create a Brute Force matcher with  
    # Hamming distance as measurement mode. 
    matcher = cv2.BFMatcher(cv2.norM_HAMMING,crossCheck = True) 
      
    # Match the two sets of descriptors. 
    matches = matcher.match(d1,d2) 
      
    # Sort matches on the basis of their Hamming distance. 
    matches.sort(key = lambda x: x.distance) 
      
    # Take the top 90 % matches forward. 
    matches = matches[:int(len(matches)*90)] 
    no_of_matches = len(matches) 
  
    # Define empty matrices of shape no_of_matches * 2. 
    p1 = np.zeros((no_of_matches,2)) 
    p2 = np.zeros((no_of_matches,2)) 
  
    for i in range(len(matches)): 
        p1[i,:] = kp1[matches[i].queryIdx].pt 
        p2[i,:] = kp2[matches[i].trainIdx].pt 
  
    # Find the homography matrix. 
    homography,mask = cv2.findHomography(p1,p2,cv2.RANSAC) 
  
    # Use this matrix to transform the 
    # colored image wrt the reference image. 
    transformed_img = cv2.warpPerspective(img1_color,homography,(width,height)) 
    # Save the output. 
    cv2.imwrite(outfile,transformed_img)

这最终将第二张图像旋转到第一张图像的方向,但扭曲太多,所以看起来它甚至不在同一平面上。

有什么方法可以将两幅图像的基于特征的匹配与仅旋转和平移但不扭曲透视的变换相结合?

谢谢!

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?
Java在半透明框架/面板/组件上重新绘画。
Java“ Class.forName()”和“ Class.forName()。newInstance()”之间有什么区别?
在此环境中不提供编译器。也许是在JRE而不是JDK上运行?
Java用相同的方法在一个类中实现两个接口。哪种接口方法被覆盖?
Java 什么是Runtime.getRuntime()。totalMemory()和freeMemory()?
java.library.path中的java.lang.UnsatisfiedLinkError否*****。dll
JavaFX“位置是必需的。” 即使在同一包装中
Java 导入两个具有相同名称的类。怎么处理?
Java 是否应该在HttpServletResponse.getOutputStream()/。getWriter()上调用.close()?
Java RegEx元字符(。)和普通点?