Search code examples
pythonimageopencvmaskblending

Blending two images gradiently with a mask


I have Python code which blends two images gradiently using a mask. The code runs fine with opencv 2.4 but if I run it with opencv 4.0 I get an error:

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)

The image manipulation seems not to be the problem but opencv 4.0 cannot display the image afterwards while there is no problem with opencv 2.4. So what I can do to run the routine in both versions. This composite function does actually the same as the Image.composite from PIL/pillow library.

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


Solution

  • I finally found out how to fix this problem with 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)