点击鼠标左键开始画roi,右键或者回车代表画框完毕
并且做了封装。
import cv2
import numpy as np
class ROIDrawer:
def __init__(self, image_path):
self.drawing = False
self.ix, self.iy = -1, -1
self.roi = None
self.image_o = cv2.imread(image_path)
self.image = self.image_o.copy()
self.temp_image = self.image.copy()
self.ok=False
def 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