I’m trying to extract pictures from an big image in python
Environment: Python 3.9.12 with CV2 4.6.0 on Ubuntu 22.04.1 LTS
I faced cv2.error :‘pointSetBoundingRect’
import cv2
image = cv2.imread('/home/joy/桌面/test_11_2/split_img/original.png', cv2.IMREAD_UNCHANGED)
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (5,5))
gradient = cv2.morphologyEx(gray, cv2.MORPH_GRADIENT, kernel)
contours = cv2.findContours(gradient, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)[1]
for cnt in contours:
(x,y,w,h) = cv2.boundingRect(cnt)
cv2.rectangle(image, (x,y), (x+w,y+h), (0,0,255))
cv2.imwrite("/home/joy/桌面/test_11_2/split_img/original.png", contours)
cv2.imshow("output", contours)
Traceback (most recent call last):
File "/home/joy/桌面/test_11_2/split_img/split_image.py", line 14, in <module>
(x,y,w,h) = cv2.boundingRect(cnt)
cv2.error: OpenCV(4.6.0) /io/opencv/modules/imgproc/src/shapedescr.cpp:874: error: (-215:Assertion failed) npoints >= 0 && (depth == CV_32F || depth == CV_32S) in function 'pointSetBoundingRect'
I tried to solved by this link
python - OpenCV Assertion failed: (-215:Assertion failed) npoints >= 0 && (depth == CV_32F || depth == CV_32S) - Stack Overflow
using _, contours, _ = cv2.findContours(img,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_NONE)
and other way still not work
(base) joy@joy-System-Product-Name:~/桌面/test_11_2/split_img$ python split_image.py
Traceback (most recent call last):
File "/home/joy/桌面/test_11_2/split_img/split_image.py", line 14, in <module>
(x,y,w,h) = cv2.boundingRect(cnt)
cv2.error: OpenCV(4.6.0) /io/opencv/modules/imgproc/src/shapedescr.cpp:874: error: (-215:Assertion failed) npoints >= 0 && (depth == CV_32F || depth == CV_32S) in function 'pointSetBoundingRect'
(base) joy@joy-System-Product-Name:~/桌面/test_11_2/split_img$ python split_image.py
Traceback (most recent call last):
File "/home/joy/桌面/test_11_2/split_img/split_image.py", line 11, in <module>
_ , contours = cv2.findContours(gradient, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)[1]
ValueError: not enough values to unpack (expected 2, got 1)
Therefor, I try as far as I could, pls give me a hand