cv2.error: OpenCV(4.6.0) :-1: error: (-5:Bad argument) in function ‘seamlessClone‘

2023-05-16

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

# 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 "<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)  # 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

即:
在这里插入图片描述

那这个时候又该怎么办呢?
在这里插入图片描述

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)

floatint即可:

center = (int(height/2), int(width/2))

4.结果预览🤔

正常运行,可以成功融合图片了:

在这里插入图片描述


渣男!都看到这里了,还不赶紧点赞评论收藏走一波?

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

cv2.error: OpenCV(4.6.0) :-1: error: (-5:Bad argument) in function ‘seamlessClone‘ 的相关文章

随机推荐