我有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 4.0无法显示图像,而opencv 2.4没有问题。那么我可以做什么来在两个版本中运行例程。此复合函数实际上与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)
我终于找到了如何使用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)