Python 在视频opencv的最后一帧上保存形状

Python 在视频opencv的最后一帧上保存形状,python,opencv,Python,Opencv,对于视频的每一帧,我都在检测的对象周围画一个矩形和一个圆形。我想保存并在视频的最后一帧上绘制整个循环中绘制的所有形状。有点像保存一张图像,在整个视频中都有物体的轨迹。我不知道如何做到这一点。如有任何建议,将不胜感激 导入cv2 将numpy作为np导入 #打开视频 cap=cv2.VideoCapture('v.mp4') 而(一): 成功,frame=cap.read() #每帧 如果成功: 裁剪图像=帧[200:2000,400:2500]。复制() 灰色=cv2.CVT颜色(裁剪图像,cv

对于视频的每一帧,我都在检测的对象周围画一个矩形和一个圆形。我想保存并在视频的最后一帧上绘制整个循环中绘制的所有形状。有点像保存一张图像,在整个视频中都有物体的轨迹。我不知道如何做到这一点。如有任何建议,将不胜感激

导入cv2
将numpy作为np导入
#打开视频
cap=cv2.VideoCapture('v.mp4')
而(一):
成功,frame=cap.read()
#每帧
如果成功:
裁剪图像=帧[200:2000,400:2500]。复制()
灰色=cv2.CVT颜色(裁剪图像,cv2.COLOR\U BGR2GRAY)
圆=cv2。霍夫圆(灰色,cv2.HOUGH_梯度,dp=1.5,Minist=505,param1=75,param2=30,minRadius=8,maxRadius=30)
#确保至少找到一些圆圈
如果圆不是无:
#将圆的(x,y)坐标和半径转换为整数
圆=np.round(圆[0,:]).aType(“int”)
#在圆的(x,y)坐标和半径上循环
对于圆中的(x,y,r):
打印(x,y)
#在输出图像中绘制圆,然后绘制矩形
#对应于圆的中心
cv2.圆(切向(x,y),r,(0,255,0),4)
cv2.矩形(裁剪图(x-5,y-5),
(x+5,y+5),(0128255),-1)
cv2.imshow(“hi”,收割机)
cv2.等待键(0)
其他:
打破
cv2.destroyAllWindows()
我添加了
circles\u all
列表来存储您检测到的所有圆圈

while True:
    success, new_frame = cap.read()
    # Take each frame
    if success:
        frame = new_frame
        crop_img = frame[200:2000, 400:2500].copy()
        gray = cv2.cvtColor(crop_img, cv2.COLOR_BGR2GRAY)
        circles = cv2.HoughCircles(gray, cv2.HOUGH_GRADIENT, dp=1.5, minDist=505, param1=75, param2=30, minRadius=8,
                                   maxRadius=30)

        # ensure at least some circles were found
        if circles is not None:
            # convert the (x, y) coordinates and radius of the circles to integers
            circles = np.round(circles[0, :]).astype("int")
            # loop over the (x, y) coordinates and radius of the circles
            for x, y, r in circles:
                print(x, y)
                circles_all.append([x, y, r])
                # draw the circle in the output image, then draw a rectangle
                # corresponding to the center of the circle
                cv2.circle(crop_img, (x, y), r, (0, 255, 0), 4)
                cv2.rectangle(crop_img, (x - 5, y - 5),
                              (x + 5, y + 5), (0, 128, 255), -1)

        cv2.imshow('hi', crop_img)
        cv2.waitKey(0)
然后添加
圆。追加
以追加坐标和半径

之后,当没有更多的帧时,您迭代trought
循环所有
并保存图像

    else:
        for (x, y, r) in circles_all:
            # draw the circle in the output image, then draw a rectangle
            # corresponding to the center of the circle
            cv2.circle(frame, (x, y), r, (0, 255, 0), 4)
            cv2.rectangle(frame, (x - 5, y - 5),
                          (x + 5, y + 5), (0, 128, 255), -1)
        cv2.imwrite('lastframe.jpg', frame)
        break
cv2.destroyAllWindows()
最后的效果是这样的十帧。

定义一个
列表
,添加圆圈。对于最后一帧,在
列表上迭代
,并绘制所有帧。或者,定义一个numpy
,将
添加到numpy数组,最后添加帧和数组。您好,非常感谢您的解决方案。尽管如此,我还是得到了这个错误:Traceback(最近一次调用last):File“/Users/mingaioh/Desktop/t/s.py”,第42行,在cv2.imwrite('./lastframe.jpg',frame)cv2.error:OpenCV(4.5.1)/private/var/folders/nz/vvv4_9tw56nv9k3tkkkvyszvg8000gn/t/pip-req-build-yaf6rry6/OpenCV/modules/imgcodecs/src/loadsave.cpp:753:error:(-215:Assertion失败)_函数'imwrite'中的img.empty()尝试打印最后一个“帧”,它输出了“无”。对不起,我忘记了当视频结束时没有帧可读取,我修改了答案,检查了
的开始,而为True
是。成功了!但我想知道,如果I>10:continue,那么I+=1是为了什么?我添加这个是为了不等待整个视频结束,而忘记删除这个,这是不必要的。
    else:
        for (x, y, r) in circles_all:
            # draw the circle in the output image, then draw a rectangle
            # corresponding to the center of the circle
            cv2.circle(frame, (x, y), r, (0, 255, 0), 4)
            cv2.rectangle(frame, (x - 5, y - 5),
                          (x + 5, y + 5), (0, 128, 255), -1)
        cv2.imwrite('lastframe.jpg', frame)
        break
cv2.destroyAllWindows()