Windows10
教育版64位
Python
3.6.3
Opencv-python
4.6.0.66
今天在使用opencv
进行泊松图像编辑
时:
'''
===========================================
@author: jayce
@file: 泊松图像编辑.py
@time: 2023/1/31/031 19:59
===========================================
'''
import cv2
import numpy as np
# Read images : src image will be cloned into dst
dst = cv2.imread("background1.jpg")
obj= cv2.imread("foreground1.jpg")
# 创建mask
# 方法1:直接读入mask图片
mask = cv2.imread("mask1.jpg")
# 方法2:Create an all white mask
# mask = 255 * np.ones(obj.shape, obj.dtype)
# The location of the center of the src in the dst
width, height, channels = dst.shape
center = (height/2, width/2)
# Seamlessly clone src into dst and put the results in output
normal_clone = cv2.seamlessClone(obj, dst, mask, center, cv2.NORMAL_CLONE)
mixed_clone = cv2.seamlessClone(obj, dst, mask, center, cv2.MIXED_CLONE)
# Write results
cv2.imwrite("opencv-normal-clone-example.jpg", normal_clone)
cv2.imwrite("opencv-mixed-clone-example.jpg", mixed_clone)
突然发生报错:
cv2.error: OpenCV(4.6.0) :-1: error: (-5:Bad argument) in function 'seamlessClone'
> Overload resolution failed:
> - Can't parse 'p'. Sequence item with index 0 has a wrong type
> - Can't parse 'p'. Sequence item with index 0 has a wrong type
详细报错如下:
Traceback (most recent call last):
File "C:\Users\Jayce\Anaconda3\lib\site-packages\IPython\core\interactiveshell.py", line 2862, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File "" , line 1, in <module>
runfile('E:/Code/Python/demo代码/测试泊松图像编辑/泊松图像编辑.py', wdir='E:/Code/Python/demo代码/测试泊松图像编辑')
File "C:\Program Files\JetBrains\PyCharm 2022.1.4\plugins\python\helpers\pydev\_pydev_bundle\pydev_umd.py", line 198, in runfile
pydev_imports.execfile(filename, global_vars, local_vars) # execute the script
File "C:\Program Files\JetBrains\PyCharm 2022.1.4\plugins\python\helpers\pydev\_pydev_imps\_pydev_execfile.py", line 18, in execfile
exec(compile(contents+"\n", file, 'exec'), glob, loc)
File "E:/Code/Python/demo代码/测试泊松图像编辑/泊松图像编辑.py", line 26, in <module>
normal_clone = cv2.seamlessClone(obj, dst, mask, center, cv2.NORMAL_CLONE)
cv2.error: OpenCV(4.6.0) :-1: error: (-5:Bad argument) in function 'seamlessClone'
> Overload resolution failed:
> - Can't parse 'p'. Sequence item with index 0 has a wrong type
> - Can't parse 'p'. Sequence item with index 0 has a wrong type
那这个时候又该怎么办呢?
仔细看报错:
- Can't parse 'p'. Sequence item with index 0 has a wrong type
发现是变量出现了wrong type
,于是检查了代码,发现在23
行在计算center
时:
center = (height/2, width/2)
会导致计算后的center
是一个由float
组成的tuple
:
而我们知道图片上的像素是int
组成的,因此需要保证center
中的元素由int
构成!
所以只需要将除法(/
)改为地板除(//
):
center = (height//2, width//2)
或float
转int
即可:
center = (int(height/2), int(width/2))
正常运行,可以成功融合图片了:
渣男!都看到这里了,还不赶紧点赞
,评论
,收藏
走一波?