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)
cv2.error: OpenCV(4.5.3) :-1: error: (-5:Bad argument) in function 'seamlessClone'
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.
Add int()
to the incoming parameter part to force the floating point number back to int type.
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
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
800 * 517
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