opencv 矩形标记
import cv2 from PIL import Image import pytesseract import pyautogui import numpy as np import time #图片路径 img = cv2.imread('Images/CAD2.png') xstart, ystart = 0, 0 #起始点x,y坐标 xend, yend = 0, 0#鼠标松开后 x,y坐标 px, py = 0, 0#移动时的x,y坐标 def on_EVENT_LBUTTONDOWN(event, x, y,flags, param): global xstart, ystart, px, py, xend, yend img2 = img.copy() #复制一个图片 if event == cv2.EVENT_LBUTTONDOWN: #鼠标点击时的操作 xstart = x; ystart = y; cv2.imshow('image', img2) elif event == cv2.EVENT_MOUSEMOVE and (flags & cv2.EVENT_FLAG_LBUTTON): #鼠标移动时的操作 px, py = x, y #在复制图片上进行鼠标移动时的画矩形框的操作 cv2.rectangle(img2, (xstart, ystart), (x, y), (0, 0, 255), 2) cv2.imshow('image', img2) elif event==cv2.EVENT_LBUTTONUP: #鼠标抬起时的操作 xend = x; yend = y; if (xstart != xend and ystart != yend): #矩形框 cv2.rectangle(img, (xstart, ystart), (xend, yend), (0, 0, 255), 2) ## 矩形截图 # t = time.time() # mst = int(round(t * 1000));# 生成毫秒时间戳 13位 ## opencv区域截图标记参数 ## xstart: 区块左上角位置的像素点离图片左边界的距离 ## ystart:区块左上角位置的像素点离图片上边界的距离 ## xend:区块右下角位置的像素点离图片左边界的距离 ## yend:区块右下角位置的像素点离图片上边界的距离 #cropped = img[ystart:yend, xstart:xend] #imgstr = "Screenshot_Image/" + str(mst) + ".png"; # 根据时间戳生成一个图片名称 ## 保存截图 #cv2.imwrite(imgstr, cropped) # 显示图片 cv2.imshow('image', img) cv2.namedWindow("image") cv2.setMouseCallback("image", on_EVENT_LBUTTONDOWN) cv2.imshow("image", img) cv2.waitKey(0)