我在绘制numpy.ndarray值时遇到麻烦。我没有太多经验,所以我可能会误称其中一些值。我相信这与它是什么类型的课程有关。
基本上,我有一个while循环,它将拍摄一个视频帧,对像素average_int
进行平均,然后绘制平均像素的图,从而创建一个不错的峰值频率。然后,平均像素值将附加到result_array
并通过峰值检测函数signal.find_peaks_cwt
。之后,我发现峰值peaks_diff = peaks_pos[1:] - peaks_pos[:-1]
的差异,并将其用作我的峰值之间的帧,然后将其放入公式中以计算每分钟的心跳数frequency = 60000/(peaks_frames * 60)
。
到目前为止,它似乎运行良好,下一步是创建另一个图frequency
。我复制了绘制average_int
的图形的方式,这给了我这个错误:ValueError: setting an array element with a sequence.
。
这是我当前的while循环减去图形线条。 编辑:添加了图形代码行
cap = cv2.VideoCapture('cardiacbody.wmv')
fgbg = cv2.createBackgroundSubtractorMOG2(varThreshold=21, detectShadows=False)
x_len = 60
y_range = [-2, 30]
fig = plt.figure()
ax = fig.add_subplot(1,1,1)
fig2 = plt.figure()
ax2 = fig2.add_subplot(2,1,1)
xs = list(range(0,60))
ys = [0] * x_len
ys2 = [0] * x_len
result_array = np.array([0])
line, = ax.plot(xs, ys, color='k')
line2, = ax2.plot(xs, ys2, color='b')
ax.set_ylim(y_range)
ax2.set_ylim(y_range)
plt.title('Movement over Time')
def animate(i, ys):
ys.append(average_int)
ys = ys[-x_len:]
line.set_ydata(ys)
return line,
def animate2(i, ys2):
ys2.append(frequency)
ys2 = ys2[-x_len:]
line2.set_ydata(ys2)
return line2,
ani = animation.FuncAnimation(fig, animate, fargs=(ys,),interval=100, blit=False)
ani2 = animation.FuncAnimation(fig2, animate2, fargs=(ys2,),interval=100, blit=False)
while True:
ret, frame = cap.read()
fgmask = fgbg.apply(frame)
cv2.imshow('Original', frame)
cv2.imshow('Masked', fgmask)
average = (np.average(fgmask)) # Average pixel intensity of frame
average_int = int(average) # Convert average into an integer
result_array = np.append(result_array, average_int) # Append average_int into result_array
peaks_pos = signal.find_peaks_cwt(result_array, range(1, 3)) # Find peaks of result_array
peaks_diff = peaks_pos[1:] - peaks_pos[:-1] # Count the frames between each peak
peaks_frames = peaks_diff[-1:] # Select counted frame value from end of array
frequency = 60000/(peaks_frames * 60) # calculate beats per minute with 60000ms/(peaks_frames)*60ms
print(type(average_int)) #class: 'int'
print(type(frequency)) #class: 'numpy.ndarray
print(frequency)
plt.pause(0.00001)
k = cv2.waitKey(30) & 0xff
if k == 27:
break
主要问题似乎是当我绘制average_int
时它起作用了,但是当我尝试绘制frequency
时却出现了错误。这些类是不同的,我尝试将frequency
转换为int或float,但仍然没有影响,并且得到相同的错误。
让我知道您是否想要我的实时图形代码行。