本文报错代码

# 注意修改路径!
import cv2
import numpy as np

# Read images : src image will be cloned into dst
obj= cv2.imread("src.jpg")
im = cv2.imread("dst.jpg")

# Create an all white mask
mask = 255 * np.ones(obj.shape, im.dtype)

# The location of the center of the src in the dst
width, height, channels = im.shape
center = (height/2, width/2)

# Seamlessly clone src into dst and put the results in output
normal_clone = cv2.seamlessClone(obj, im, mask, center, cv2.NORMAL_CLONE)
mixed_clone = cv2.seamlessClone(obj, im, mask, center, cv2.MIXED_CLONE)

# Write results
cv2.imwrite("normal.jpg", normal_clone)
cv2.imwrite("mixed.jpg", mixed_clone)

其中  dst.jpg  图片如下

src.jpg  图片如下

 

报错信息:

cv2.error: OpenCV(4.5.3) :-1: error: (-5:Bad argument) in function 'seamlessClone'

截图如下:

 分析原因:

发现错误的原因是:输入到cv2.rectangle()这个函数中两个的坐标:不能是浮点数的类型, 要转换成整数才行,于是就使用int()强制类型转换,之后代码成功的有这个bug变成了另外一个bug。

解决方法:

给传入参数部分加上  int()  将浮点数强转回  int型

 修改后的代码如下

# 注意修改路径!
import cv2
import numpy as np

# Read images : src image will be cloned into dst
obj= cv2.imread("src.jpg")
im = cv2.imread("dst.jpg")

# Create an all white mask
mask = 255 * np.ones(obj.shape, im.dtype)

# The location of the center of the src in the dst
width, height, channels = im.shape
center = (int(height/2), int(width/2))

# Seamlessly clone src into dst and put the results in output
normal_clone = cv2.seamlessClone(obj, im, mask, center, cv2.NORMAL_CLONE)
mixed_clone = cv2.seamlessClone(obj, im, mask, center, cv2.MIXED_CLONE)

# Write results
cv2.imwrite("normal.jpg", normal_clone)
cv2.imwrite("mixed.jpg", mixed_clone)

然后运行

发现了另外一个更奇葩的bug

cv2.error: OpenCV(4.5.3) C:\Users\runneradmin\AppData\Local\Temp\pip-req-build-q3d_8t8e\opencv\modules\core\src\matrix.cpp:811: error: (-215:Assertion failed) 0 <= roi.x && 0 <= roi.width && roi.x + roi.width <= m.cols && 0 <= roi.y && 0 <= roi.height && roi.y + roi.height <= m.rows in function 'cv::Mat::Mat'

报错截图如下

D:\Anaconda3\File\python.exe C:/Users/0moyi0/Desktop/DeepLearningExamples-master/hh.py
Traceback (most recent call last):
  File "C:/Users/0moyi0/Desktop/DeepLearningExamples-master/hh.py", line 17, in <module>
    normal_clone = cv2.seamlessClone(obj, im, mask, center, cv2.NORMAL_CLONE)
cv2.error: OpenCV(4.5.3) C:\Users\runneradmin\AppData\Local\Temp\pip-req-build-q3d_8t8e\opencv\modules\core\src\matrix.cpp:811: error: (-215:Assertion failed) 0 <= roi.x && 0 <= roi.width && roi.x + roi.width <= m.cols && 0 <= roi.y && 0 <= roi.height && roi.y + roi.height <= m.rows in function 'cv::Mat::Mat'

我仔细看了下后面的报错部分   感觉应该是个边界错误  超出边界的感觉

然后我尝试去找了 bug  提示信息里面的路径 发现找不到

再分析可能是因为参数里面的尺寸不对 导致图像的ROI区域超过了图像的尺寸,即

0 <= roi.x && 0 <= roi.width && roi.x + roi.width <= m.cols && 0 <= roi.y && 0 <= roi.height && roi.y + roi.height <= m.rows

然后仔细看了两张图片的大小

首先是  src.jpg  的尺寸大小是  800 * 517

然后是  dst.jpg  的尺寸大小   是 800 * 449

 然后看到了代码中的  mask 部分,感觉应该是这个地方用的  obj.shape  过大错了,然后换成  im.shape  发现bug解决了,可以正常运行了

修改后的代码

# 注意修改路径!
import cv2
import numpy as np

# Read images : src image will be cloned into dst
obj= cv2.imread("src.jpg")
im = cv2.imread("dst.jpg")

# Create an all white mask
mask = 255 * np.ones(im.shape, im.dtype)

# The location of the center of the src in the dst
width, height, channels = im.shape
center = (int(height/2), int(width/2))

# Seamlessly clone src into dst and put the results in output
normal_clone = cv2.seamlessClone(obj, im, mask, center, cv2.NORMAL_CLONE)
mixed_clone = cv2.seamlessClone(obj, im, mask, center, cv2.MIXED_CLONE)

# Write results
cv2.imwrite("normal.jpg", normal_clone)
cv2.imwrite("mixed.jpg", mixed_clone)

 修改后的运行成功截图

 成功生成 normal.jpg

和  mixed.jpg

 这个实验的参数没设置好,后续还要修改,希望能可同样出 bug 的你一些帮助,谢谢

Logo

鸿蒙生态一站式服务平台。

更多推荐