python打开相机,用鼠标左键框选矩形区域,支持一次框选多个矩形区域,通过鼠标右标清除上一次画的矩形。

方案一

python 复制代码
import cv2

# Global variables
rectangles = []
current_rectangle = []
drawing = False

# Mouse callback function
def mouse_callback(event, x, y, flags, param):
    global rectangles, current_rectangle, drawing

    if event == cv2.EVENT_LBUTTONDOWN:
        drawing = True
        current_rectangle = [(x, y)]

    elif event == cv2.EVENT_LBUTTONUP:
        drawing = False
        current_rectangle.append((x, y))
        rectangles.append(tuple(current_rectangle))
        current_rectangle = []

    elif event == cv2.EVENT_RBUTTONDOWN:
        rectangles.pop()

# Open camera
cap = cv2.VideoCapture(0)

# Create a window and set mouse callback
cv2.namedWindow('Camera')
cv2.setMouseCallback('Camera', mouse_callback)

while True:
    # Read frame from camera
    ret, frame = cap.read()

    # Draw rectangles on the frame
    for rect in rectangles:
        cv2.rectangle(frame, rect[0], rect[1], (0, 255, 0), 2)

    # Display the frame
    cv2.imshow('Camera', frame)

    # Check for key press
    key = cv2.waitKey(1)
    if key == 27:  # Press 'Esc' to exit
        break

# Release the camera and destroy all windows
cap.release()
cv2.destroyAllWindows()

方案二

python 复制代码
import cv2

# Global variables
rectangles = []
current_rectangle = []
drawing = False
rect_x = 0
rect_y = 0

# Mouse callback function
def mouse_callback(event, x, y, flags, param):
    global rectangles, current_rectangle, drawing

    if event == cv2.EVENT_LBUTTONDOWN:
        drawing = True
        current_rectangle = [(x, y)]
        global rect_x, rect_y
        rect_x = x
        rect_y = y

    elif event == cv2.EVENT_LBUTTONUP:
        drawing = False
        current_rectangle.append((x, y))
        rectangles.append(tuple(current_rectangle))
        current_rectangle = []


    elif event == cv2.EVENT_RBUTTONDOWN:
        rectangles.pop()

# Open camera
cap = cv2.VideoCapture(0)

# Create a window and set mouse callback
cv2.namedWindow('Camera')
cv2.setMouseCallback('Camera', mouse_callback)

while True:
    # Read frame from camera
    ret, frame = cap.read()

    # Draw rectangles on the frame
    for rect in rectangles:
        cv2.rectangle(frame, rect[0], rect[1], (0, 255, 0), 2)

    # Draw current rectangle in real-time
    if drawing:
        cv2.rectangle(frame, current_rectangle[0], (rect_x, rect_y), (0, 255, 0), 2)

    # Display the frame
    cv2.imshow('Camera', frame)

    # Check for key press
    key = cv2.waitKey(1)
    if key == 27:  # Press 'Esc' to exit
        break

# Release the camera and destroy all windows
cap.release()
cv2.destroyAllWindows()

下一步修改优化

a 在图像区域内实时显示鼠标所在位置的十字线

b 实时显示绘制框的过程

相关推荐
Birdy_x1 小时前
接口自动化项目实战(1):requests请求封装
开发语言·前端·python
我爱学习好爱好爱1 小时前
Ansible 常用模块详解:lineinfile、replace、get_url实战
linux·python·ansible
一轮弯弯的明月3 小时前
Python基础-速通秘籍(下)
开发语言·笔记·python·学习
千寻girling4 小时前
面试官 : “ 说一下 Python 中的常用的 字符串和数组 的 方法有哪些 ? ”
人工智能·后端·python
第一程序员4 小时前
Python基础学习路径:非科班转码者的入门指南
python·github
u0136863824 小时前
将Python Web应用部署到服务器(Docker + Nginx)
jvm·数据库·python
smchaopiao5 小时前
Python中字典与列表合并的问题与解决方法
开发语言·python
卡尔特斯5 小时前
Ultralytics YOLO26 自动对指定标注文件夹区分标注素材脚本与训练脚本
python·openai
2501_921649495 小时前
期货 Tick 级数据与基金净值历史数据 API 接口详解
开发语言·后端·python·websocket·金融·区块链
njidf6 小时前
实战:用Python开发一个简单的区块链
jvm·数据库·python