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

created at 11-09-2021 views: 20

Error code in this article


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)

error message:

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

reasons

The reason for the error was found: the two coordinates entered into the cv2.rectangle() function: cannot be the type of floating-point numbers, and need to be converted to integers, so the int() coerced type conversion is used, and the code succeeds. This bug became another bug.

solution:

Add int() to the incoming parameter part to force the floating point number back to int type.

modified code

The modified code is as follows


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 weird bug

new 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'

I carefully looked at the error reporting part at the back and felt that it should be a boundary error, the feeling of going beyond the boundary

Then I tried to find the path in the bug message, but I couldn’t find it.

The re-analysis may be because the size in the parameters is incorrect, which causes the ROI area of the image to exceed the size of the image, that is

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 carefully looked at the size of the two pictures

  1. First, the size of src.jpg is 800 * 517
  2. Then the size of dst.jpg is 800 * 449

Then I saw the mask part in the code, I felt that the obj.shape used in this place was too big and wrong, and then replaced it with im.shape and found that the bug was solved and it can run normally.

Modified code


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)

Successful operation after modification

created at:11-09-2021
edited at: 11-09-2021: