如何解决如何在 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 举报,一经查实,本站将立刻删除。