Python 3.x Python中视频的Regionprops(skimage.measure)

Python 3.x Python中视频的Regionprops(skimage.measure),python-3.x,video-processing,labels,region,Python 3.x,Video Processing,Labels,Region,我在internet上得到一段代码,它是“”,并试图在视频上运行它,但我得到的只是第一帧,而不是关闭第一帧窗口后的一个错误“max()arg是一个空序列”from line“plt.tight_layout()我的代码。我正在尝试获取视频中所有帧的标签,而不是上面给定示例(链接)中所示的单个图像示例。基本上,代码应该显示/打印带有标签的所有帧 任何帮助都会非常有用。请在下面找到我的代码 import cv2 import numpy as np from matplotlib import py

我在internet上得到一段代码,它是“”,并试图在视频上运行它,但我得到的只是第一帧,而不是关闭第一帧窗口后的一个错误“max()arg是一个空序列”from line“
plt.tight_layout()
我的代码。我正在尝试获取视频中所有帧的标签,而不是上面给定示例(链接)中所示的单个图像示例。基本上,代码应该显示/打印带有标签的所有帧

任何帮助都会非常有用。请在下面找到我的代码

import cv2
import numpy as np
from matplotlib import pyplot as plt
import time
import matplotlib.patches as mpatches

from skimage import data
from skimage.filters import threshold_otsu
from skimage.segmentation import clear_border
from skimage.measure import label, regionprops
from skimage.morphology import closing, square
from skimage.color import label2rgb


cap =  cv2.VideoCapture('test3.mp4')
fig, ax = plt.subplots(figsize=(10, 6))

while(1):
    t = time.time()
    ret, frame2 = cap.read()
    image = cv2.cvtColor(frame2, cv2.COLOR_BGR2GRAY)
    thresh = threshold_otsu(image)
    bw = closing(image > thresh, square(3))

    # remove artifacts connected to image border
    cleared = clear_border(bw)

    # label image regions
    label_image = label(cleared)
    image_label_overlay = label2rgb(label_image, image=frame2)
    x = regionprops(label_image)
    area2 = [r.area for r in x]
    print(area2)
    ax.imshow(image_label_overlay)

    for region in regionprops(label_image):
        # take regions with large enough areas
        if region.area >= 100:
           # draw rectangle around segmented coins
            minr, minc, maxr, maxc = region.bbox
            rect = mpatches.Rectangle((minc, minr), maxc - minc, maxr -minr,
                                   fill=False, edgecolor='red', linewidth=2)
            ax.add_patch(rect)

    ax.set_axis_off()
    plt.tight_layout()
    plt.show()

    if cv2.waitKey(1) & 0xFF == ord('q'):
        break
 cap.release()
 cv2.destroyAllWindows()
沃拉

解决办法是:

1.)错误纠正:“max()arg是一个空序列”来自
plt行。紧密布局()
可以使用
图移除。紧密布局
而不是
plt。紧密布局
。因为在我关闭视频的第一帧之后(那不是更新,这是我还在思考的另一个问题!!)图是空的,它引发了一个异常,因为
太紧了。布局
试图在空图上运行

2.)如果更换线路,则可以运行视频代码

    rect = mpatches.Rectangle((minc, minr), maxc - minc+50, maxr - minr+50,fill=False, edgecolor='red', linewidth=2)
    ax.add_patch(rect)
    ax.set_axis_off()
    plt.tight_layout()
    plt.show()

基本上以Python简单程序的方式显示视频

   cv2.rectangle(frame2, (minc, minr), (minc +maxc - minc , minr + maxr - minr), (0, 255, 0), 2)
cv2.imshow('ObjectTrack', frame2) # this line outside the if loop