用画笔样式图纸叠加实时视频?

时间:2017-04-04 10:19:22

标签: opencv video tkinter overlay

我需要一些方向,了解使用画笔覆盖来自网络摄像头(或预装视频)的实时视频的最佳编程方法。即绘制线条,圆圈等,以便在播放时标记视频中的区域。

现在使用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()

1 个答案:

答案 0 :(得分:1)

我只会将 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()