没有合适的资源?快使用搜索试试~ 我知道了~
首页Python OpenCV实现鼠标画框效果
资源详情
资源评论
资源推荐

Python OpenCV实现鼠标画框效果实现鼠标画框效果
主要为大家详细介绍了Python OpenCV实现鼠标画框效果,具有一定的参考价值,感兴趣的小伙伴们可以参考
一下
使用Python+OpenCV实现鼠标画框的代码,供大家参考,具体内容如下
# -*-coding: utf-8 -*-
"""
@Project: IntelligentManufacture
@File : user_interaction.py
@Author : panjq
@E-mail : pan_jinquan@163.com
@Date : 2019-02-21 15:03:18
"""
# -*- coding: utf-8 -*-
import cv2
from utils import image_processing
import numpy as np
global img
global point1, point2
global g_rect
def on_mouse(event, x, y, flags, param):
global img, point1, point2,g_rect
img2 = img.copy()
if event == cv2.EVENT_LBUTTONDOWN: # 左键点击,则在原图打点
print("1-EVENT_LBUTTONDOWN")
point1 = (x, y)
cv2.circle(img2, point1, 10, (0, 255, 0), 5)
cv2.imshow('image', img2)
elif event == cv2.EVENT_MOUSEMOVE and (flags & cv2.EVENT_FLAG_LBUTTON): # 按住左键拖曳,画框
print("2-EVENT_FLAG_LBUTTON")
cv2.rectangle(img2, point1, (x, y), (255, 0, 0), thickness=2)
cv2.imshow('image', img2)
elif event == cv2.EVENT_LBUTTONUP: # 左键释放,显示
print("3-EVENT_LBUTTONUP")
point2 = (x, y)
cv2.rectangle(img2, point1, point2, (0, 0, 255), thickness=2)
cv2.imshow('image', img2)
if point1!=point2:
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])
g_rect=[min_x,min_y,width,height]

















安全验证
文档复制为VIP权益,开通VIP直接复制

评论0