cv2.MOTION_HOMOGRAPHY使用ECC方法进行图像对齐

时间:2019-04-11 06:59:32

标签: python opencv image-processing

<图像>图像1和2是从一系列SAR视频帧获得的SAR图像。我想将图像2与图像1对齐。我将cv2.MOTION_HOMOGRAPHY用于扭曲模式。我得到以下输出图像。但这与图像2相同,并且与图像1不对齐。我尝试过,但无法弄清错误。

图片1 Image 1

图片2 Image 2

输出图像 Output - Image 2 aligned with image 1

import cv2
import numpy as np
from PIL import Image, ImageChops

im1 = cv2.imread("data2/frame200.jpg")
im2= cv2.imread("data2/frame300.jpg")

im1_gray = cv2.cvtColor(im1,cv2.COLOR_BGR2GRAY)
im2_gray = cv2.cvtColor(im2,cv2.COLOR_BGR2GRAY)

sz= im1.shape

warp_mode= cv2.MOTION_HOMOGRAPHY

if warp_mode == cv2.MOTION_HOMOGRAPHY :
    warp_matrix = np.eye(3, 3, dtype=np.float32)
else :
    warp_matrix = np.eye(2, 3, dtype=np.float32)

number_of_iterations = 5000

termination_eps = 1e-10

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

 (cc, warp_matrix) = cv2.findTransformECC (im1_gray,im2_gray,warp_matrix, 
 warp_mode, criteria)

if warp_mode == cv2.MOTION_HOMOGRAPHY :

    im2_aligned = cv2.warpPerspective (im2, warp_matrix, (sz[1],sz[0]), flags=cv2.INTER_LINEAR + cv2.WARP_INVERSE_MAP)
else :

    im2_aligned = cv2.warpAffine(im2, warp_matrix, (sz[1],sz[0]), flags=cv2.INTER_LINEAR + cv2.WARP_INVERSE_MAP);

im= Image.fromarray(im2_aligned)
im.save("im2_aligned.jpg")

diff_image= ImageChops.difference(Image.open("data2/frame300.jpg"),Image.open("im2_aligned.jpg"))

diff_image.save("difference1.png")
cv2.imshow("Image 1", im1)
cv2.imshow("Image 2", im2)
cv2.imshow("Aligned Image 2", im2_aligned)

cv2.waitKey(0)

code source

0 个答案:

没有答案