How to correctly use cv2.imwrite to save an image in openCV with cv2.selectROI
cv2.selectROI
returns the (x,y,w,h)
values of a rectangle similar to cv2.boundingRect()
. My guess is that the saved black rectangle is due to rounding issues when converting the bounding box coordinates to an int
type. So just unpack the (x,y,w,h)
coordinates directly and use Numpy slicing to extract the ROI. Here's a minimum working example to extract and save a ROI:
Input image ->
Program to extract ROI ->
Saved ROI
Code
import cv2image = cv2.imread('1.jpg')(x,y,w,h) = cv2.selectROI(image)ROI = image[y:y+h, x:x+w]cv2.imshow("ROI", ROI)cv2.imwrite("ROI.png", ROI)cv2.waitKey()