如何用鼠标从视频中拍照

发布于 2025-01-01 09:51:26 字数 160 浏览 0 评论 0原文

我有一段高速公路上汽车的视频,所以我想用鼠标一张一张地剪切。但我需要更自动地执行此操作,例如,一种更改框架的方法,按空格键并用鼠标剪切并将图像自动保存在文件夹中。

你在 Linux 上见过类似的东西吗?我正在尝试在 OpenCV 中执行此操作,尽管这不是必需的。

提前致谢。

I have a video with cars on a highway, so i would like to cut one by one with the mouse. But i need to do this more automatically, for example, one way to change the frame pressing the space bar and cuting with the mouse and saving the image automatically in a folder.

Have you ever seen something like this for Linux? I'm trying to do in OpenCV, although it isn't necessary.

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

岁月打碎记忆 2025-01-08 09:51:26

我在下面添加了代码。它对我来说效果很好。在要单独保存的对象周围绘制矩形。按“空格”进入下一帧。(有时您需要按更长的时间才能向前移动。我不知道为什么)。同样按“Esc”退出。

用法: python car.py 视频文件


# car.py
import cv,sys
print "usage: python car.py <videofile>"

box=[0,0,0,0]
i = 0
global image
#   creating mouse callback function
def my_mouse_callback(event,x,y,flags,param):
    global drawing_box,i
    if event==cv.CV_EVENT_LBUTTONDOWN:
        events = event
        #print event
        drawing_box=True
        [box[0],box[1],box[2],box[3]]=[x,y,0,0]
        print x,y
    if event==cv.CV_EVENT_LBUTTONUP:
        events = event
        drawing_box=False
        events= event
        if box[2]<0:
            box[0]+=box[2]
            box[2]*=-1
        if box[3]<0:
            box[1]+=box[3]
            box[3]*=-1
        rect = (box[0],box[1],box[2],box[3])
        #print rect
        roi = cv.GetSubRect(image,rect)
        filename = str(i)+'.jpg'
        #save(image,rect,filename)
        cv.SaveImage(filename,roi)
        i = i+1 

    if event==cv.CV_EVENT_MOUSEMOVE:
        if (drawing_box==True):
            box[2]=x-box[0]
            box[3]=y-box[1] 


#   function to draw the rectangle, added flag -1 to fill rectangle. If you don't want to fill, just delete it.     
def draw_box(img,box):
    cv.Rectangle(img,(box[0],box[1]),(box[0]+box[2],box[1]+box[3]),(255,0,0),2)

#   main program    
capture = cv.CaptureFromFile(sys.argv[1])
image2 = cv.QueryFrame(capture)
drawing_box=False

#   make a clone of image
temp=cv.CloneImage(image2)

cv.NamedWindow("Box Example")
cv.SetMouseCallback("Box Example",my_mouse_callback,temp)

while(1):
#   global image
    image = cv.QueryFrame(capture)
    while(1):
        roi = 0
        save = False
        events = 0
        boxsaver = 0
        cv.Copy(image,temp)

        if drawing_box==True:
            draw_box(temp,box)

            i = i+1

        cv.ShowImage("Box Example",temp)
        if cv.WaitKey(20)==27:
            sys.exit()
        elif cv.WaitKey(20)==32:
            break

I have added a code below. It works fine for me. Draw rectangle around object you want to save separately. Press 'Space' to go for next frame.( sometimes you need to press longer to move foreward. i don't know why). Similarly 'Esc' to exit.

usage: python car.py videofile


# car.py
import cv,sys
print "usage: python car.py <videofile>"

box=[0,0,0,0]
i = 0
global image
#   creating mouse callback function
def my_mouse_callback(event,x,y,flags,param):
    global drawing_box,i
    if event==cv.CV_EVENT_LBUTTONDOWN:
        events = event
        #print event
        drawing_box=True
        [box[0],box[1],box[2],box[3]]=[x,y,0,0]
        print x,y
    if event==cv.CV_EVENT_LBUTTONUP:
        events = event
        drawing_box=False
        events= event
        if box[2]<0:
            box[0]+=box[2]
            box[2]*=-1
        if box[3]<0:
            box[1]+=box[3]
            box[3]*=-1
        rect = (box[0],box[1],box[2],box[3])
        #print rect
        roi = cv.GetSubRect(image,rect)
        filename = str(i)+'.jpg'
        #save(image,rect,filename)
        cv.SaveImage(filename,roi)
        i = i+1 

    if event==cv.CV_EVENT_MOUSEMOVE:
        if (drawing_box==True):
            box[2]=x-box[0]
            box[3]=y-box[1] 


#   function to draw the rectangle, added flag -1 to fill rectangle. If you don't want to fill, just delete it.     
def draw_box(img,box):
    cv.Rectangle(img,(box[0],box[1]),(box[0]+box[2],box[1]+box[3]),(255,0,0),2)

#   main program    
capture = cv.CaptureFromFile(sys.argv[1])
image2 = cv.QueryFrame(capture)
drawing_box=False

#   make a clone of image
temp=cv.CloneImage(image2)

cv.NamedWindow("Box Example")
cv.SetMouseCallback("Box Example",my_mouse_callback,temp)

while(1):
#   global image
    image = cv.QueryFrame(capture)
    while(1):
        roi = 0
        save = False
        events = 0
        boxsaver = 0
        cv.Copy(image,temp)

        if drawing_box==True:
            draw_box(temp,box)

            i = i+1

        cv.ShowImage("Box Example",temp)
        if cv.WaitKey(20)==27:
            sys.exit()
        elif cv.WaitKey(20)==32:
            break
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文