Seaborn 生成像素变化热图仅需数分钟从图像数组

2024-05-28 18:51:48 发布

您现在位置:Python中文网/ 问答频道 /正文

我使用seaborn热图来显示一系列图像(从视频中获得)的像素变化,但这样做需要10分钟以上,并完全冻结我的办公电脑。我正在寻找一种方法来获得这个热图没有所有这些问题。你知道吗

我已经试着去掉了标签,因为我看到了一些可能有用的建议。你知道吗

vidcap = cv2.VideoCapture('video2.mp4')
#vidcap.set(cv2.CAP_PROP_FPS, 5)
success,image = vidcap.read()
count = 0
images = []

while success:
  #cv2.imwrite("frame%d.png" % count, image)     # save frame as png file      
  success, image = vidcap.read()
  if success == True:
      images.append(cv2.cvtColor(image, cv2.COLOR_BGR2GRAY))
  print('New frame: ', success)
  count += 1

images = np.asarray(images)

aax = sns.heatmap(images.std(axis = 0), yticklabels = False)
plt.show()

Tags: 图像imageread视频pngcount像素seaborn
1条回答
网友
1楼 · 发布于 2024-05-28 18:51:48

我想不是seaborn占用了这里的时间,而是您正在将视频的所有帧加载到内存中。你要确保你没有那样做!你知道吗

基本上,您希望计算“运行”或在线方差,而不存储中间值。有几种不同的折衷方法,但我建议您检查一下Welford's algorithm,甚至在wikipedia页面上有一个不错的Python实现

基本上,您可以将代码更改为:

success, image = vidcap.read()
image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
state = (1, np.array(image, dtype=float), np.zeros(image.shape))

while True:
    success, image = cap.read()
    if not success:
        break
    image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    state = update(state, image)

mu, var0, var1 = finalize(state)

image_sd = np.sqrt(var1)
sns.heatmap(image_sd)

其中updatefinalise来自维基百科页面

如果真的是seaborn导致了速度变慢,那么我会使用matplotlib中的imshow,因为它所做的工作要少得多,例如:

import matplotlib.pyplot as plt

plt.imshow(image_sd)

相关问题 更多 >

    热门问题