gpt4 book ai didi

python - 快速 Python 绘图库直接在 2D numpy 数组图像缓冲区上绘制绘图?

转载 作者:太空宇宙 更新时间:2023-11-03 11:47:46 24 4
gpt4 key购买 nike

我经常使用 opencv 绘图函数直接在来自 opencv 网络摄像头流的 2D numpy 数组图像缓冲区上绘制 2D 图。而且,我将 numpy 数组发送到 imshow 和视频编写器以监控和创建视频。

import cv2
import numpy as np

cap = cv2.VideoCapture(0)
ret, frame = cap.read() # frame is a 2D numpy array w640 h480
h,w,_ = frame.shape # (480,640,3)
x = np.arange(w)

writer = cv2.VideoWriter( 'out.avi', cv2.cv.FOURCC('D','I','V','3'),
fps=30, frameSize=(w,h), isColor=True )

while True:
ret, frame = cap.read() # frame is a 2D numpy array w640 h480

B = frame[:,:,0].sum(axis=0)
B = h - h * B / B.max()
G = frame[:,:,1].sum(axis=0)
G = h - h * G / G.max()
R = frame[:,:,2].sum(axis=0)
R = h - h * R / R.max()

pts = np.vstack((x,B)).astype(np.int32).T
cv2.polylines(frame, [pts], isClosed=False, color=(255,0,0))
pts = np.vstack((x,G)).astype(np.int32).T
cv2.polylines(frame, [pts], isClosed=False, color=(0,255,0))
pts = np.vstack((x,R)).astype(np.int32).T
cv2.polylines(frame, [pts], isClosed=False, color=(0,0,255))

writer.write(frame)

cv2.imshow('frame', frame)
key = cv2.waitKey(33) & 0xFF # for 64 bit PC
if key in 27: # ESC key
break

cap.release()
writer.release()

enter image description here

这很好用,但我想知道我是否可以做更多像 matplotlib 可以做的事情,例如轴、刻度、网格、标题、条形图等,而无需基于基本的 cv2 绘图函数推出我自己的绘图库,这将是可能,但我不想重新发明轮子。

调查https://wiki.python.org/moin/NumericAndScientific/Plotting , 有很多绘图库。所以,我觉得他们中的一个可能已经这样做了。

我考虑过使用 matplotlib 并通过 savefig 将绘图导出为图像。但这对于视频捕获来说会很慢。

(编辑)我可以按照接受的答案中的建议,使用 mplfig_to_npimage 将 matplotlib 图嵌入到框架中!视频速率似乎足够快。

import cv2
from pylab import *
from moviepy.video.io.bindings import mplfig_to_npimage

fp = r'C:/Users/Public/Videos/Sample Videos/Wildlife.wmv'

cap = cv2.VideoCapture(fp)
ret, frame = cap.read() # frame is a 2D numpy array
h,w,_ = frame.shape
writer = cv2.VideoWriter( 'out.avi', cv2.cv.FOURCC('D','I','V','3'),
fps=30, frameSize=(w,h), isColor=True )

# prepare a small figure to embed into frame
fig, ax = subplots(figsize=(4,3), facecolor='w')
B = frame[:,:,0].sum(axis=0)
line, = ax.plot(B, lw=3)
xlim([0,w])
ylim([40000, 130000]) # setup wide enough range here
box('off')
tight_layout()

graphRGB = mplfig_to_npimage(fig)
gh, gw, _ = graphRGB.shape

while True:
ret, frame = cap.read() # frame is a 2D numpy array
B = frame[:,:,0].sum(axis=0)
line.set_ydata(B)
frame[:gh,w-gw:,:] = mplfig_to_npimage(fig)

cv2.imshow('frame', frame)
writer.write(frame)

key = cv2.waitKey(33) & 0xFF # for 64 bit
if key in 27: # ESC key
break

cap.release()
writer.release()

enter image description here

最佳答案

所以,如果我做对了,您需要:

  • 在图像上绘制概念图(路径、多边形),以及开箱即用的指示器(轴、自动封闭图)
  • 视频转储和希望实时流媒体。

如果是这样,我建议使用 matplotlib with moviepy .

确实执行 savefig 来流式传输视频并不是最好的方法,但是您可以很容易地使这两个工作。

包括上面链接中的小示例以供记录(头脑许可证):

import matplotlib.pyplot as plt
import numpy as np
from moviepy.video.io.bindings import mplfig_to_npimage
import moviepy.editor as mpy

# DRAW A FIGURE WITH MATPLOTLIB

duration = 2

fig_mpl, ax = plt.subplots(1,figsize=(5,3), facecolor='white')
xx = np.linspace(-2,2,200) # the x vector
zz = lambda d: np.sinc(xx**2)+np.sin(xx+d) # the (changing) z vector
ax.set_title("Elevation in y=0")
ax.set_ylim(-1.5,2.5)
line, = ax.plot(xx, zz(0), lw=3)

# ANIMATE WITH MOVIEPY (UPDATE THE CURVE FOR EACH t). MAKE A GIF.

def make_frame_mpl(t):
line.set_ydata( zz(2*np.pi*t/duration)) # <= Update the curve
return mplfig_to_npimage(fig_mpl) # RGB image of the figure

animation =mpy.VideoClip(make_frame_mpl, duration=duration)
animation.write_gif("sinc_mpl.gif", fps=20)

关于python - 快速 Python 绘图库直接在 2D numpy 数组图像缓冲区上绘制绘图?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35281427/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com