在图像识别中,需要地图像中某个特征进行标注,或者是显著出来。则可以使用:
方法rectangle(目标图像,边框左上角坐标,边框右下角坐标,color=BGR三基色像素值,thickness=矩阵边框的厚度)
实例代码:
import cv2
img = cv2.imread(“C:\lena.jpg”)
def on_mouse(event,x,y,flags,param):
rect_start = (0,0)
rect_end = (0,0)
#鼠标左键按下/抬起/双击
if event == cv2.EVENT_LBUTTONDOWN:
rect_start = (x,y)
elif event == cv2.EVENT_LBUTTONUP:
rect_end = (x,y)
cv2.rectangle(img,(2,2),(90,90),(0,255,0),1)
cv2.namedWindow(“test”)
cv2.setMouseCallback(“test”,on_mouse)
while(1):
cv2.imshow("",img)
if cv2.waitKey(1) & 0xff ==ord(“q”):
break
cv2.destroyAllWindows()
显示结果: