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
Read More:
- [Solved] cv2.error: OpenCV(4.5.3) :-1: error: (-5:Bad argument) in function ‘circle‘
- [Solved] error: OpenCV(4.5.2) :-1: error: (-5:Bad argument) in function ‘copyMakeBorder‘
- [Solved] error: (-215:Assertion failed) !_src.empty() in function ‘cv::cvtColor‘
- [Solved] cv2.error: OpenCV(4.5.3) :-1: error: (-5:Bad argument) in function “pointPolygonTest“
- [Solved] error: (-215:Assertion failed) size.width>0 && size.height>0 in function ‘imshow‘
- [Solved] cv2.error: (-215:Assertion failed) encoder->isFormatSupported(CV_8U) in function ‘imwrite_‘
- v2.error: OpenCV(4.5.5) error: (-215:Assertion failed) npoints > 0 in function ‘cv::drawContours‘
- [Solved] cv2.error: OpenCV(4.5.1) XXX\shapedescr.cpp:315: error: (-215:Assertion failed) npoints >= 0 &&……
- Opencv Python realizes the paint filling function in PS, one click filling color and the possible reasons for opencv’s frequent errors
- [Solved] Python opencv imshow Error: Assertion failed (size.width>0 && size.height>0) in cv::imshow
- [Solved] Django backend error: Assertion failed: (NSViewIsCurrentlyBuildingLayerTreeForDisplay()
- [Solved] Python Error: positional argument follows keyword argument
- [Solved] ERROR: No matching distribution found for torch-cluster==x.x.x
- [Solved] PyTorch Caught RuntimeError in DataLoader worker process 0和invalid argument 0: Sizes of tensors mus
- [ncclUnhandledCudaError] unhandled cuda error, NCCL version xx.x.x
- How to Fix “HTTP error 403: forbidden” in Python 3. X
- [Solved] Pdfplumber Read PDF Sheet Error: AttributeError: function/symbol ‘ARC4_stream_init‘ not found in library
- Python Run Error: TypeError: hog() got an unexpected keyword argument ‘visualise‘”
- Python environment error, bad interpreter: there is no file or directory