python + OpenCV实现鼠标和图像的交互显示ROI(感兴趣区域)

1.首先选取一张图片

python + OpenCV实现鼠标和图像的交互显示ROI(感兴趣区域)_第1张图片

import numpy as np
import cv2

img = cv2.imread('D:/OpencvVideo/1.jpg')

# create a window
cv2.namedWindow('image', flags= cv2.WINDOW_NORMAL | cv2.WINDOW_FREERATIO)# 定义窗口1
cv2.namedWindow('image_roi', flags= cv2.WINDOW_NORMAL | cv2.WINDOW_FREERATIO)# 定义感兴趣区域窗口

cv2.imshow('image', img)

# whether to show crosschair
showCrosshair = True # 是否显示交叉线
# if true, then from the center
# if false, then from the left-top
fromCenter = False  #是否从中心开始选择
# then let use to choose the ROI
rect = cv2.selectROI('image', img, showCrosshair, fromCenter)
#也可以是 rect = cv2.selectROI('image', img, False, False)#记得改掉上面的语句不要设置为
# rect = cv2.selectROI('image', img, showCrosshair=False, fromCenter=False)
# get the ROI
(x, y, w, h) = rect#(起始x,起始y,终点x,终点y)

# Crop image
imCrop = img[int(y) : int(y+h), int(x):int(x+w)]

# Display cropped image
cv2.imshow('image_roi', imCrop)

# write image to local disk
cv2.imwrite('image_roi.jpg', imCrop)
cv2.waitKey(0)

python + OpenCV实现鼠标和图像的交互显示ROI(感兴趣区域)_第2张图片

点击enter之后可以有

python + OpenCV实现鼠标和图像的交互显示ROI(感兴趣区域)_第3张图片

 

请忽略拙劣的瓶盖,一点小东西,大家有什么想法一起交流吧

 

 

你可能感兴趣的:(CV,计算机视觉)