使用具有降低的不透明度的Homography进行图像对齐

时间:2017-05-31 09:24:29

标签: python opencv image-processing

我有一个使用单应性对齐两个图像的程序。我在python中使用cv2。这是我正在使用的代码:

import cv2
import numpy as np
im_src = cv2.imread('src.jpg')
pts_src = np.array([[141, 131], [480, 159], [493, 630],[64, 601]])
im_dst = cv2.imread('dst.jpg')
pts_dst = np.array([[318, 256],[534, 372],[316, 670],[73, 473]])
h, status = cv2.findHomography(pts_src, pts_dst)
im_out = cv2.warpPerspective(im_src, h, (im_dst.shape[1],im_dst.shape[0]))
cv2.imshow("Warped Image", im_out)
cv2.waitKey(0)

上面的代码工作正常。现在我想减少im_out中的im_dst的不透明度(比如opacity = 0.5),这样我就可以在im_out中看到im_src和im_dst。 怎么做?

我在网上查了很多,大多数都使用了cv2.addWeighted,但我希望在cv2.warpPerspective中减少im_dst的不透明度,而不仅仅是简单的覆盖。

我是openCV的新手,所以我可能会遗漏一些简单的东西。谢谢你的帮助!

编辑:

我尝试了以下方法来降低im_dst的不透明度,但我得到的只是一个黑色的包裹图像

import cv2
import numpy as np
im_src = cv2.imread('src.jpg')
pts_src = np.array([[141, 131], [480, 159], [493, 630],[64, 601]])
im_dst = cv2.imread('dst.jpg')
pts_dst = np.array([[318, 256],[534, 372],[316, 670],[73, 473]])
h, status = cv2.findHomography(pts_src, pts_dst)
img1 = np.array(im_dst , dtype=np.float)
img2 = np.array(im_src , dtype=np.float)
img1 /= 255.0
# pre-multiplication
a_channel = np.ones(img1.shape, dtype=np.float)/2.0
im_dst = img1*a_channel
im_src = img2*(1-a_channel)
im_out = cv2.warpPerspective(im_src, h, (im_dst.shape[1],im_dst.shape[0]))
cv2.imshow("Warped Image", im_out)
cv2.waitKey(0)

1 个答案:

答案 0 :(得分:1)

终于得到了解决方案!为了得到我想要的东西,我们应该将褪色和扭曲的图像添加到目标图像并输出。此外,当我们有四个匹配点时,最好使用->get();,当我们有四个匹配点时,最好使用getPerspectiveTransform()

findHomography()