gpt4 book ai didi

opencv - 用画笔样式的绘图覆盖实时视频?

转载 作者:行者123 更新时间:2023-12-02 17:42:14 24 4
gpt4 key购买 nike

我需要一些最佳编程方法的指导,以便用画笔覆盖网络摄像头的实时视频(或预加载的视频)。即绘制线条,圆圈等以标记视频播放时的区域。

现在使用OpenCV + Tkinter,有没有更好的方法来解决这个问题?

谢谢

编辑:尝试直接在视频窗口上绘制,但无法使其正常工作..这是我的代码:

import cv2
import numpy as np
def interactive_drawing(event,x,y,flags,param):
global ix,iy,drawing, mode

if event==cv2.EVENT_LBUTTONDOWN:
drawing=True
ix,iy=x,y

elif event==cv2.EVENT_MOUSEMOVE:
if drawing==True:
if mode==True:
cv2.line(frame,(ix,iy),(x,y),(0,0,255),10)
ix=x
iy=y
print x,y
elif event==cv2.EVENT_LBUTTONUP:
drawing=False
if mode==True:
cv2.line(frame,(ix,iy),(x,y),(0,0,255),10)
ix=x
iy=y
return x,y

drawing=False # true if mouse is pressed
mode=True # if True, draw rectangle. Press 'm' to toggle to curve
cap = cv2.VideoCapture('track.avi')


while(cap.isOpened()):
ret, frame = cap.read()
if frame is None:
break
# cv2.namedWindow("frame", cv2.WND_PROP_FULLSCREEN)
# cv2.setWindowProperty("frame",cv2.WND_PROP_FULLSCREEN,cv2.WINDOW_FULLSCREEN)
cv2.setMouseCallback('frame',interactive_drawing)
cv2.imshow('frame',frame)
if cv2.waitKey(15) & 0xFF == ord('q'):
break

cap.release()
cv2.destroyAllWindows()



# mouse callback function






##img = np.zeros((512,512,3), np.uint8)
##
##cv2.namedWindow('begueradj')
##cv2.setMouseCallback('begueradj',interactive_drawing)
##while(1):
## cv2.imshow('begueradje',img)
## k=cv2.waitKey(1)&0xFF
## if k==27:
## break
##cv2.destroyAllWindows()

最佳答案

我只会为此使用 OpenCV ,因为它已经提供了drawing functions,例如:

  • 矩形
  • 圈子
  • 椭圆
  • 多边形

  • 编辑:此代码段应帮助您入门。这里的重点是您必须将绘图元素保存在变量中(在我的情况下为 curve_points),并在每个新框架上绘制它们:
    import cv2
    import numpy as np
    def interactive_drawing(event,x,y,flags,param):
    global drawing, mode

    if event==cv2.EVENT_LBUTTONDOWN:
    drawing=True

    elif event==cv2.EVENT_MOUSEMOVE:
    if drawing==True:
    if mode==True:
    curves[len(curves)-1].append((x,y)) #append new points to the last list of curves

    elif event==cv2.EVENT_LBUTTONUP:
    drawing=False
    if mode==True:
    curves.append([]) #adding a new list to curves

    return x,y

    def draw_curves(myArray):

    for j in range(0, len(myArray)):
    for i in range(1, len(myArray[j])):
    cv2.line(frame,myArray[j][i-1],myArray[j][i],(0,0,255),10)

    drawing=False # true if mouse is pressed
    mode=True # if True, draw rectangle. Press 'm' to toggle to curve
    cap = cv2.VideoCapture(0) #cap = cv2.VideoCapture('track.avi')
    curves = [[]] # initializing curves list with an empty list
    cv2.namedWindow('frame')
    cv2.setMouseCallback('frame',interactive_drawing)

    while(cap.isOpened()):
    ret, frame = cap.read()
    if frame is None:
    break
    draw_curves(curves)
    cv2.imshow('frame',frame)
    if cv2.waitKey(15) & 0xFF == ord('q'):
    break

    cap.release()
    cv2.destroyAllWindows()

    关于opencv - 用画笔样式的绘图覆盖实时视频?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43204427/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com