Tag Archives: error: (-5:Bad argument) in function ‘seamlessClone‘

error: (-5:Bad argument) in function ‘seamlessClone‘ and error: (-215:Assertion failed) 0 <= roi.x && 0 [How to Solve]

Error code in this document

# Pay attention to modify the path!
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)

The picture is as follows among dst.jpg:

The picture (src.jpg) is as follows

Error message:

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

The screenshot is as follows:

Analysis reason:

The reason for finding the error is that the coordinates of the two input into the CV2. Rectangle () function cannot be the type of floating point number, but must be converted to an integer. Therefore, int () is used to force type conversion. After that, the code successfully has this bug and becomes another bug.

Solution:

Add int() Strong return of floating point numbers  Int type

The modified code is as follows

# Pay attention to modify the path!
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)

Then run

Found another more exotic 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'

The screenshot of error reporting is as follows

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'

I read the error report carefully   It feels like a boundary error   Feeling beyond the boundary

Then I tried to find the bug   The path found in the prompt message cannot be found

Re analysis may be due to the wrong size in the parameters, resulting in the ROI area of the image exceeding the size of the image, i.e

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

Then I looked at the size of the two pictures carefully

First   src.jpg   What is the size of the   800 * 517

And then the size of dst.jpg is 800 * 449

Then you see the the mask part feels like it should be used in this place   obj.shape   Too big a mistake, and then replace it with im.shape, The bug has been found and solved. It can run normally

Modified code

# Pay attention to modify the path!
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)

Screenshot of successful operation after modification

Normal.jpg generated successfully

and mixed.jpg

The parameters of this experiment are not set well, and will be modified later. I hope you can help with the same bug. Thank you