OpenCV+Python 截取图像中选中区域ROI)

import numpy as np

import cv2 



 

#使用鼠标获取ROI区域

src=cv2.imread(r"C:\Users\ljt\Desktop\pic\all.bmp",0)

cv2.namedWindow('roi',cv2.WINDOW_NORMAL)

cv2.imshow('roi',src)

roi=cv2.selectROI(windowName="roi",img=src,showCrosshair=True,fromCenter=False)

x,y,w,h=roi

cv2.rectangle(img=src,pt1=(x,y),pt2=(x+w,y+h),color=(0,0,255),thickness=2)

img=src[y:y+h,x:x+w]

cv2.imshow('img',img)

cv2.waitKey(0)

cv2.destroyAllWindows()

你可能感兴趣的:(OpenCV)