我在互联网上获得了一段代码'Label image regions',并尝试在视频上运行它,但我得到的只是第一帧而不是关闭第一帧窗口后的错误“max()arg是我的代码的“从行”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()
答案 0 :(得分:0)
瓦沃里亚!
解决方案是:
1。)错误纠正:&#34; max()arg是一个空序列&#34; ,可以使用< plt.tight_layout()
删除<强> fig.tight_layout
而不是 plt.tight_layout
。因为在我关闭视频的第一帧之后(那不是更新,以及我仍在思考的另一个问题!!)这个数字是空的,它引发了一个例外,因为 {{1}试图在一个空虚的身影上运行。
2。)如果替换行
,则可以运行Label image regions视频代码tight.layout
带
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()
基本上以简单的Capture Video from Camera Python程序显示视频。