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

Blending two images gradiently with a mask

我有 Python 代码,它使用遮罩渐变地混合两个图像。代码 运行 适用于 opencv 2.4,但如果我 运行 使用 opencv 4.0,我会收到错误消息:

File "C:\Python27\Programme\OpenCV2\Tutorialc_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 4.0 之后无法显示图像,而 opencv 2.4 则没有问题。那么我可以对 运行 两个版本中的例程做些什么。这个复合函数实际上与 PIL/pillow 库中的 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)

我终于找到了如何用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)