话不多说,注释详细写在代码当中
import cv2
src = cv2.imread('pic.jpg')
src = cv2.resize(src, (0,0), fx=1.2, fy=1.2)#水平轴与垂直轴上的比例因子,我给它拉大了1.2倍
print("--------- HL截取ROI区域的测试 ---------")
print("鼠标选择ROI,然后点击 enter键")
r = cv2.selectROI('org', src, False) # ,返回 (x_min, y_min, w, h)
# roi区域
roi = src[int(r[1]):int(r[1]+r[3]), int(r[0]):int(r[0]+r[2])]
cv2.destroyAllWindows()
cv2.imshow('ROI',roi)#显示ROI区域
k = cv2.waitKey(0) & 0xFF
if k == 27: # 按Esc 键即可退出
cv2.destroyAllWindows()
import numpy as np
import cv2
src=cv2.imread('pic.jpg')
cv2.namedWindow('roi',cv2.WINDOW_AUTOSIZE)
print("--------- HL截取ROI区域的测试 ---------")
print("鼠标选择ROI,然后点击 enter键")
roi=cv2.selectROI(windowName="roi",img=src,showCrosshair=True,fromCenter=False)
x,y,w,h=roi
cv2.rectangle(img=src,pt1=(x,y),pt2=(x+w,y+h),color=(0,0,255),thickness=2)
img=src[y:y+h,x:x+w]
cv2.destroyAllWindows()
cv2.imshow('ROI',img)
k = cv2.waitKey(0) & 0xFF
if k == 27: # 按esc 键即可退出
cv2.destroyAllWindows()
被组织发现我第二种方法是投机取巧 滥竽充数了
经过再次了解,修正方法如下:
import cv2
def on_mouse(event, x, y, flags, param):
global img, click, slip #定义局部变量全局可用
img2 = img.copy() #将原图复制copy一份给我处理
if event == cv2.EVENT_LBUTTONDOWN: #库函数————左键点击
click = (x,y) #定义一个参数存放点击的坐标
cv2.circle(img2, click, 5, (255,255,0), 5) #点的时候显示一个小点即圆点供用户交互
cv2.imshow('pic', img2)
elif event == cv2.EVENT_MOUSEMOVE and (flags & cv2.EVENT_FLAG_LBUTTON): #鼠标按住左键拖曳
cv2.rectangle(img2, click, (x,y), (0,125,255), 5)#上一步记录的坐标点作为起点,用户拖拽轨迹点为终点画矩形框
cv2.imshow('pic', img2)
elif event == cv2.EVENT_LBUTTONUP: #当鼠标左键释放时
slip = (x,y)
cv2.rectangle(img2, click, slip, (0,0,255), 5)
cv2.imshow('pic', img2)
min_x = min(click[0],slip[0])
min_y = min(click[1],slip[1])
width = abs(click[0] - slip[0])
height = abs(click[1] -slip[1])
roi = img[min_y:min_y+height, min_x:min_x+width]#提取此时的ROI区域
cv2.destroyAllWindows()
cv2.imshow('ROI', roi)
img = cv2.imread('pic.jpg')
print("--------- HL截取ROI区域的测试 ---------")
cv2.namedWindow('pic')
cv2.setMouseCallback('pic', on_mouse)
cv2.imshow('pic', img)
cv2.waitKey(0)
cv2.destroyAllWindows()