Seaborn 只需几分钟即可从一组图像中创建像素方差的热图
Seaborn takes minutes to create heatmap of pixel variance from an array of images
我正在使用 seaborn 热图来显示一组图像(从视频中获取)的像素方差,但这样做需要 10 多分钟,而且我的办公室 PC 完全卡住了。我正在寻找一种没有所有这些问题的方法来获取此热图。
我已经尝试删除 yticklabels,因为我看到了一些可能有用的建议。
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()
我想这不是 seaborn
在这里花时间,而是您将视频的所有帧加载到内存中这一事实。你要确保你没有那样做!
基本上您想要计算 "running" 或 on-line 方差,而不存储中间值。有几种方法可以通过各种权衡来做到这一点,但我建议您查看 Welford's algorithm,在维基百科页面
上什至有一个很好的 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)
其中 update
和 finalise
来自维基百科页面
如果确实是 seaborn
导致速度变慢,那么我会使用 matplotlib 中的 imshow
,因为它的工作要少得多,例如:
import matplotlib.pyplot as plt
plt.imshow(image_sd)
我正在使用 seaborn 热图来显示一组图像(从视频中获取)的像素方差,但这样做需要 10 多分钟,而且我的办公室 PC 完全卡住了。我正在寻找一种没有所有这些问题的方法来获取此热图。
我已经尝试删除 yticklabels,因为我看到了一些可能有用的建议。
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()
我想这不是 seaborn
在这里花时间,而是您将视频的所有帧加载到内存中这一事实。你要确保你没有那样做!
基本上您想要计算 "running" 或 on-line 方差,而不存储中间值。有几种方法可以通过各种权衡来做到这一点,但我建议您查看 Welford's algorithm,在维基百科页面
上什至有一个很好的 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)
其中 update
和 finalise
来自维基百科页面
如果确实是 seaborn
导致速度变慢,那么我会使用 matplotlib 中的 imshow
,因为它的工作要少得多,例如:
import matplotlib.pyplot as plt
plt.imshow(image_sd)