使用蒙版渐变融合两个图像

时间:2019-03-10 22:32:33

标签: python image opencv mask blending

我有Python代码,可以使用遮罩渐变地混合两个图像。该代码在opencv 2.4上运行良好,但是如果在opencv 4.0下运行,则会收到错误消息:

File "C:\Python27\Programme\OpenCV2\Tutorial\10c_composite_alpha.py", line 44, in <module>
    cv2.imshow("result", result)
cv2.error: OpenCV(4.0.0) C:\projects\opencv-python\opencv\modules\highgui\src\window_w32.cpp:1230: error: (-215:Assertion failed) dst.data == (uchar*)dst_ptr in function 'cvShowImage'

Line 44 is: cv2.imshow("result", result)

图像处理似乎不是问题,但是当opencv 2.4没有问题时,opencv 4.0之后无法显示图像。因此,我可以在两个版本中运行例程。该复合功能实际上与PIL /枕头库中的Image.composite相同。

from __future__ import print_function
import numpy as np 
import cv2, sys

def composite(background, foreground, alphamask):
    "pastes the foreground image into the background image using the mask"
    # Convert uint8 to float
    print(foreground[:10])
    foreground = foreground.astype(float)

    background = background.astype(float)
    # Normalize the alpha mask to keep intensity between 0 and 1
    alphamask = alphamask.astype(float)/255
    # Multiply the foreground with the alpha matte
    foreground = cv2.multiply(alphamask, foreground)
    # Multiply the background with ( 1 - alpha )
    background = cv2.multiply(1.0 - alphamask, background)
    # Add the masked foreground and background
    outImage = cv2.add(foreground, background)
    outImage = outImage/255
    return outImage

img2 = cv2.imread('cat.jpg')
cv2.imshow("Foreground", img2)
img1 = cv2.imread("landscape.jpg")
print(img1.shape, img2.shape)
img1 = cv2.resize(img1, (img2.shape[1], img2.shape[0]))
cv2.imshow("Background", img1)
print(img1.shape, img2.shape)

for img_file in ["mask1.jpg", "mask2.jpg", "mask3.jpg"]:
    mask = cv2.imread(img_file)
    mask = cv2.resize(mask, (img2.shape[1], img2.shape[0]))
    cv2.imshow("mask", mask)
    result = composite(img1, img2, mask)
    print(type(result))
    cv2.imshow("result", result)
    cv2.waitKey(0)

cat.jpg

landscape.jpg

mask1.jpg

mask2.jg

mask3.jpg

1 个答案:

答案 0 :(得分:0)

我终于找到了如何使用opencv-python == 4.0.0.21解决此问题的方法:

from __future__ import print_function
import numpy as np 
import cv2, sys

print(cv2.__version__)

def composite(background, foreground, alphamask):
    "pastes the foreground image into the background image using the mask"
    # remember the old datatype
    old_type = background.dtype
    # Convert uint8 to float
    foreground = foreground.astype(float)
    background = background.astype(float)
    # Normalize the alpha mask to keep intensity between 0 and 1
    alphamask = alphamask.astype(float)/255
    # Multiply the foreground with the alpha matte
    foreground = cv2.multiply(alphamask, foreground)
    # Multiply the background with ( 1 - alpha )
    background = cv2.multiply(1.0 - alphamask, background)
    # Add the masked foreground and background
    outImage = cv2.add(foreground, background)
    outImage = outImage/255
    # convert it back to old format
    outImage = outImage*255
    outImage = outImage.astype(old_type)
    return outImage

img2 = cv2.imread('cat.jpg')
cv2.imshow("Foreground", img2)
img1 = cv2.imread("landscape.jpg")
print(img1.shape, img2.shape)
img1 = cv2.resize(img1, (img2.shape[1], img2.shape[0]))
cv2.imshow("Background", img1)
print(img1.shape, img2.shape)

for img_file in ["mask1.jpg", "mask2.jpg", "mask3.jpg"]:
    mask = cv2.imread(img_file)
    mask = cv2.resize(mask, (img2.shape[1], img2.shape[0]))
    cv2.imshow("mask", mask)
    result = composite(img1, img2, mask)
    print(type(result))
    cv2.imshow("result", result)
    cv2.waitKey(0)