Python 中视频的 Regionprops (skimage.measure)

Regionprops (skimage.measure) for Video in Python

我在互联网上得到一段代码“Label image regions”并试图通过视频 运行 它,但我得到的只是第一帧,而不是关闭第一帧后的错误window“max() arg 是一个空序列”来自我代码的行“plt.tight_layout()”。我正在尝试为我的视频中的所有帧获取标签,而不是给定的单个图像示例上面的示例 (link)。基本上代码应该 display/plot 所有带标签的帧。

任何帮助都是真的useful.Please在下面找到我的代码

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()

Wola!

解决方案是:

1.) 错误修正:"max() arg is an empty sequence"plt.tight_layout()可以使用[=30删除=]fig.tight_layout 而不是 plt.tight_layout。因为在我关闭视频的第一帧之后(那不是更新,这是我仍在思考的另一个问题!!)这个数字是空的,它引发了一个异常 tight.layout 试图 运行 空图。

2.) 运行 Label image regions 如果您替换第

行,则视频代码成为可能
    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()

   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

基本上以 Python 的简单 Capture Video from Camera 程序的方式显示视频。