当前位置:网站首页>Opencv鼠标事件+界面交互之绘制矩形多边形选取感兴趣区域ROI
Opencv鼠标事件+界面交互之绘制矩形多边形选取感兴趣区域ROI
2022-06-26 07:41:00 【明月醉窗台】
1.Opencv鼠标事件
import cv2
d=[i for i in dir(cv2) if 'EVENT' in i]
print(d)
'EVENT_FLAG_ALTKEY':代表拖拽事件。按住alt键不放
'EVENT_FLAG_CTRLKEY'按住ctrl键不放
'EVENT_FLAG_LBUTTON'按住左键拖拽
'EVENT_FLAG_MBUTTON'中键拖拽
'EVENT_FLAG_RBUTTON'右键拖拽
'EVENT_FLAG_SHIFTKEY'按住shift不放
'EVENT_LBUTTONDBLCLK'event鼠标事件。左键双击
'EVENT_LBUTTONDOWN'按下左键
'EVENT_LBUTTONUP'释放左键
'EVENT_MBUTTONDBLCLK' 中键双击
'EVENT_MBUTTONDOWN'中键点击
'EVENT_MBUTTONUP'中键放开
'EVENT_MOUSEHWHEEL'
'EVENT_MOUSEMOVE'滑动
'EVENT_MOUSEWHEEL',
'EVENT_RBUTTONDBLCLK',右键双击
'EVENT_RBUTTONDOWN',右键点击
'EVENT_RBUTTONUP'右键释放
回调函数使用:
cv2.setMousecallback(const string& winname, MouseCallback onMouse, void* userdata=0)
winname:窗口的名字
onMouse:鼠标响应函数,回调函数。指定窗口里每次鼠标时间发生的时候,被调用的函数指针。 这个函数的原型应该为void on_Mouse(int event, int x, int y, int flags, void* param);
userdate:传给回调函数的参数
响应函数使用:
void on_Mouse(int event, int x, int y, int flags, void* param);
event是 CV_EVENT_*变量之一
x和y是鼠标指针在图像坐标系的坐标(不是窗口坐标系)
flags是CV_EVENT_FLAG的组合, param是用户定义的传递到setMouseCallback函数调用的参数。
2.绘制矩形ROI
2.1方法一
import cv2
global img
global point1, point2
#鼠标响应函数
def Rectangular_box(event, x, y, flags, param):
global img, point1, point2
img2 = img.copy()
if event == cv2.EVENT_LBUTTONDOWN: # 左键点击
point1 = (x, y)
cv2.circle(img2, point1, 10, (0, 255, 0), 5)
cv2.imshow('img', img2)
elif event == cv2.EVENT_MOUSEMOVE and (flags & cv2.EVENT_FLAG_LBUTTON): # 按住左键拖曳
cv2.rectangle(img2, point1, (x, y), (255, 0, 0), 5)
cv2.imshow('img', img2)
elif event == cv2.EVENT_LBUTTONUP: # 左键释放
point2 = (x, y)
cv2.rectangle(img2, point1, point2, (0, 255, 255), 4)
cv2.imshow('img', img2)
min_x = min(point1[0], point2[0])
min_y = min(point1[1], point2[1])
width = abs(point1[0] - point2[0])
height = abs(point1[1] - point2[1])
cut_img = img[min_y:min_y + height, min_x:min_x + width]
#cv2.imwrite('baocun.jpg', cut_img)
cv2.imshow('result',cut_img
def main():
global img
img = cv2.imread('yangmi.jpg')
img=cv2.resize(img,None,fx=0.4,fy=0.4)
cv2.namedWindow('image')
cv2.setMouseCallback('image', Rectangular_box)
cv2.imshow('image', img)
cv2.waitKey(0)
if __name__ == '__main__':
main()

2.2方法二
import cv2
import imutils
img = cv2.imread("./test_image.jpg")
img = imutils.resize(img, width=500)
roi = cv2.selectROI(windowName="roi", img=img, showCrosshair=True, fromCenter=False)
x, y, w, h = roi
cv2.rectangle(img=img, pt1=(x, y), pt2=(x + w, y + h), color=(0, 0, 255), thickness=2)
cv2.imshow("roi", img)
cv2.waitKey(0)
cv2.destroyAllWindows()

3.绘制多边形ROI
多边形ROI,主要利用鼠标交互进行绘制:
- 单击左键,选择多边形的点;
- 单击右键,删除最近一次选择的点;
- 单击中键,确定ROI区域并可视化。
- 按”S“键,将多边形ROI区域的点保存到本地”config.pkl"文件中。
import cv2
import imutils
import numpy as np
import joblib
pts = [] # 用于存放点
# 统一的:mouse callback function
def draw_roi(event, x, y, flags, param):
img2 = img.copy()
if event == cv2.EVENT_LBUTTONDOWN: # 左键点击,选择点
pts.append((x, y))
if event == cv2.EVENT_RBUTTONDOWN: # 右键点击,取消最近一次选择的点
pts.pop()
if event == cv2.EVENT_MBUTTONDOWN: # 中键绘制轮廓
mask = np.zeros(img.shape, np.uint8)
points = np.array(pts, np.int32)
points = points.reshape((-1, 1, 2))
# 画多边形
mask = cv2.polylines(mask, [points], True, (255, 255, 255), 2)
mask2 = cv2.fillPoly(mask.copy(), [points], (255, 255, 255)) # 用于求 ROI
mask3 = cv2.fillPoly(mask.copy(), [points], (0, 255, 0)) # 用于 显示在桌面的图像
show_image = cv2.addWeighted(src1=img, alpha=0.8, src2=mask3, beta=0.2, gamma=0)
cv2.imshow("mask", mask2)
cv2.imshow("show_img", show_image)
ROI = cv2.bitwise_and(mask2, img)
cv2.imshow("ROI", ROI)
cv2.waitKey(0)
if len(pts) > 0:
# 将pts中的最后一点画出来
cv2.circle(img2, pts[-1], 3, (0, 0, 255), -1)
if len(pts) > 1:
# 画线
for i in range(len(pts) - 1):
cv2.circle(img2, pts[i], 5, (0, 0, 255), -1) # x ,y 为鼠标点击地方的坐标
cv2.line(img=img2, pt1=pts[i], pt2=pts[i + 1], color=(255, 0, 0), thickness=2)
cv2.imshow('image', img2)
# 创建图像与窗口并将窗口与回调函数绑定
img = cv2.imread("./test_image.jpg")
img = imutils.resize(img, width=500)
cv2.namedWindow('image')
cv2.setMouseCallback('image', draw_roi)
print("[INFO] 单击左键:选择点,单击右键:删除上一次选择的点,单击中键:确定ROI区域")
print("[INFO] 按‘S’确定选择区域并保存")
print("[INFO] 按 ESC 退出")
while True:
key = cv2.waitKey(1) & 0xFF
if key == 27:
break
if key == ord("s"):
saved_data = {
"ROI": pts
}
joblib.dump(value=saved_data, filename="config.pkl")
print("[INFO] ROI坐标已保存到本地.")
break
cv2.destroyAllWindows()
边栏推荐
- [UVM practice] Chapter 2: a simple UVM verification platform (4) the ultimate masterpiece of UVM: sequence
- ECE 9203/9023 analysis
- Which of the top ten securities companies has the lowest commission fee and is the most safe and reliable?
- Getdrawingcache of view is empty. Solution: interview questions for Android audio and video development
- Google Earth Engine(GEE) 02-基本了解和学习资源
- Yyds dry inventory executor package (executor function)
- D do not assign references to non domain parameters
- How to convert Unicode into Chinese characters in Excel
- Iron / zinc / copper / platinum metal complexes such as 5,10,15,20-tetra (4-hydroxyphenyl) porphyrin (THPP) / (thppfe) / (thppzn) / (thppcu) / (thpppt) - Qiyue R & D
- Okhttp3 source code explanation (IV) cache strategy, disadvantages of Android mixed development
猜你喜欢

Two models of OSPF planning: double tower Raider and dog tooth crisscross

Children play games (greed, prefix and) - Niuke winter vacation training camp

职场“大冤种”,不仅身累,心也被掏空……
![5,10,15,20-tetra (4-bromophenyl) porphyrin (h2tppbr4) /5.2.15,10,15,20-tetra [4-[(3-aminophenyl) ethynyl] phenyl] porphyrin (tapepp) Qiyue porphyrin reagent](/img/2b/837dfa0f80c31c2903927085c491df.jpg)
5,10,15,20-tetra (4-bromophenyl) porphyrin (h2tppbr4) /5.2.15,10,15,20-tetra [4-[(3-aminophenyl) ethynyl] phenyl] porphyrin (tapepp) Qiyue porphyrin reagent

Gavin teacher's insight on transformer live class - multi state transition of financial BOT and rasa interactive behavior analysis of Rasa project (52)

Machine learning - Iris Flower classification

Quickly find five channels for high-quality objects, quickly collect and avoid detours

Attention mechanism yyds, AI editor finally bid farewell to P and destroyed the whole picture

Jemter 压力测试 -可视化工具支持-【安装篇】

Request&Response
随机推荐
3,3 '- di (3,4-dicarboxyphenoxy) -4,4' - diphenylethynylbiphenyldianhydride (bpebpda) / porphyrin 2dcofs (H2P COF, ZNP COF and cup COF) supplied by Qiyue
Is it legal to open an account for compass stock trading software? Is it safe?
How to quickly merge multiple PDF files?
Iron / zinc / copper / platinum metal complexes such as 5,10,15,20-tetra (4-hydroxyphenyl) porphyrin (THPP) / (thppfe) / (thppzn) / (thppcu) / (thpppt) - Qiyue R & D
Item2 installation configuration and environment failure solution
A bold sounding and awesome operation - remake a Netflix
Web technology sharing | webrtc recording video stream
Crosslinked porphyrin based polyimide ppbpi-2, ppbpi-1-cr and ppbpi-2-cr; Porous porphyrin based hyperbranched polyimide (ppbpi-1, ppbpi-2) supplied by Qiyue
QPS
记一次开发 pgadmin 时执行 Building the Web Assets 遇到的依赖安装问题
Yyds dry inventory executor package (executor function)
3D porphyrin MOF (mof-p5) / 3D porphyrin MOF (mof-p4) / 2D cobalt porphyrin MOF (ppf-1-co) / 2D porphyrin COF (POR COF) / supplied by Qiyue
Okhttp3 source code explanation (IV) cache strategy, disadvantages of Android mixed development
卡尔曼滤波器_Recursive Processing
What are the key points of turnover box management in warehouse management
Getdrawingcache of view is empty. Solution: interview questions for Android audio and video development
职场“大冤种”,不仅身累,心也被掏空……
QT basics tutorial: qstring
Young man, do you know the original appearance of kotlin association process?
Oracle creates stored procedures with return values and executes SQL calls
