I'm trying to crop an image to the boundaries of a contour. I've found a code from this answer
mask = np.zeros_like(image)
cv2.drawContours(mask, [c], -1, 255, -1)
out = np.zeros_like(image)
out[mask == 255] = image[mask == 255]
(y, x) = np.where(mask == 255)
(topy, topx) = (np.min(y), np.min(x))
(bottomy, bottomx) = (np.max(y), np.max(x))
out = out[topy: bottomy + 1, topx:bottomx + 1]
crop_img = image[topy: bottomy + 1, topx:bottomx + 1]
cv2.imshow("croppedd", crop_img)
where c
is a contour.
I'm getting error like :
Traceback (most recent call last):
File "detect_shapes.py", line 66, in <module>
(y, x) = np.where(mask == 255)
ValueError: too many values to unpack (expected 2)
How can I solve my issue?
- Python version 3.7
- OpenCV version 3.4.4
I don't think this is related to my image but, here my image;