Can't parse 'p'. Sequence item with index 0 has a wrong type
- 1.软件环境⚙️
- 2.问题描述🔍
- 3.解决方法🐡
- 4.结果预览🤔
1.软件环境⚙️
Windows10
教育版64位
Python
3.6.3
Opencv-python
4.6.0.66
2.问题描述🔍
今天在使用opencv
进行泊松图像编辑
时:
'''
===========================================
@author: jayce
@file: 泊松图像编辑.py
@time: 2023/1/31/031 19:59
===========================================
'''
import cv2
import numpy as np
dst = cv2.imread("background1.jpg")
obj= cv2.imread("foreground1.jpg")
mask = cv2.imread("mask1.jpg")
width, height, channels = dst.shape
center = (height/2, width/2)
normal_clone = cv2.seamlessClone(obj, dst, mask, center, cv2.NORMAL_CLONE)
mixed_clone = cv2.seamlessClone(obj, dst, mask, center, cv2.MIXED_CLONE)
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 "<ipython-input-2-06464fbcb61d>", 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)
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
即:
那这个时候又该怎么办呢?
3.解决方法🐡
仔细看报错:
- 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))
4.结果预览🤔
正常运行,可以成功融合图片了:
渣男!都看到这里了,还不赶紧点赞
,评论
,收藏
走一波?
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)