裁剪图像
import cv2.cv2
img = cv2.imread('roi.jpg') #输入所显示的图片的路径
#windowName:选择的区域被命名
roi = cv2.selectROI(windowName='roi', img=img, showCrosshair=True, fromCenter=False)
x, y, w, h = roi
print("x=", x)
print("y=", y)
print("w=", w)
print("h=", h)
img_roi = img[int(roi[1]):int(roi[1]+roi[3]),int(roi[0]):int(roi[0]+roi[2])] #截取ROI
cv2.rectangle(img=img, pt1=(x, y), pt2=(x + w, y + h), color=(0, 0, 255), thickness=2)
cv2.imshow('roi', img)
cv2.imshow('imageHSV',img_roi)
cv2.waitKey(0)
cv2.destroyAllWindows()
网友评论