点击鼠标左键开始画roi,右键或者回车代表画框完毕
并且做了封装。
import cv2
import numpy as npclass ROIDrawer:def __init__(self, image_o):self.drawing = Falseself.ix, self.iy = -1, -1self.roi = Noneself.image_o =image_oself.image = self.image_o.copy()self.temp_image = self.image.copy()self.ok = Falsedef draw_crosshair(self, event, x, y, flags, param):self.temp_image = self.image.copy()if self.roi is None and not self.drawing:cv2.line(self.temp_image, (x, 0), (x, self.temp_image.shape[0]), (0, 255, 0), 1)cv2.line(self.temp_image, (0, y), (self.temp_image.shape[1], y), (0, 255, 0), 1)if event == cv2.EVENT_LBUTTONDOWN:self.drawing = True