前言:同上。
1.代码:
import cv2
refPt=[]
cropping=False
image=cv2.imread('E:\\Desktop\\01.jpg')
def click_and_crop(event,x,y,flags,param):
global refPt,cropping
if event == cv2.EVENT_LBUTTONDOWN:
refPt=[(x,y)]
print(x,y)
cropping=True
elif event == cv2.EVENT_LBUTTONUP:
refPt.append((x,y))
print(x,y)
cropping=False
cv2.rectangle(image,refPt[0],refPt[1],(0,255,0),2)
cv2.imshow("image",image)
cv2.namedWindow('test')
cv2.setMouseCallback('test',click_and_crop)
while (1):
cv2.imshow('test',image)
if cv2.waitKey(0):
break
cv2.destroyAllWindows()
2.spyder运行如下: