Python matplotlib imshow 很慢

发布于 2024-10-18 21:02:11 字数 1469 浏览 1 评论 0原文

我想使用 imshow 显示图像文件。这是一个 1600x1200 灰度图像,我发现 matplotlib 使用 float32 来解码值。加载图像大约需要 2 秒,我想知道是否有任何方法可以加快加载速度。重点是我并不真正需要高分辨率图像,我只是想标记某些点并将图像绘制为背景。所以,

  • 第一个问题:对于这样的图像来说 2 秒是一个好的性能还是 我可以加快速度吗?
  • 第二个问题:如果性能好的话我该如何制作流程 通过降低分辨率来加快速度。重要的一点:我仍然想要 图像最终拉伸超过 1600x1200 像素。

我的代码:

import matplotlib
import numpy

plotfig     = matplotlib.pyplot.figure()
plotwindow  = plotfig.add_subplot(111)
plotwindow.axis([0,1600,0,1200])
plotwindow.invert_yaxis() 
img = matplotlib.pyplot.imread("lowres.png")
im  = matplotlib.pyplot.imshow(img,cmap=matplotlib.cm.gray,origin='centre')
plotfig.set_figwidth(200.0)
plotfig.canvas.draw()
matplotlib.pyplot.show()

这就是我想做的。现在,如果保存在 lowres.png 中的图片分辨率较低,为 1600x1200(即 400x300),它会按应有的方式显示在右上角。如何将其缩放到 1600x1200 像素的整个区域? 如果我运行这个程序,缓慢的部分来自下面的 canvas.draw() 命令。有没有办法加快这个命令的速度?

先感谢您!

根据你的建议我已经更新到最新版本的matplotlib

版本1.1.0svn,结账8988

我还使用以下代码:

img = matplotlib.pyplot.imread(pngfile)
img *= 255
img2 = img.astype(numpy.uint8)
im  = self.plotwindow.imshow(img2,cmap=matplotlib.cm.gray, origin='centre')

并且仍然需要大约 2 秒才能显示图像...还有其他想法吗?

补充一下:我发现了以下功能

zoomed_inset_axes

所以原则上 matplotlib 应该能够完成这个任务。人们还可以以“缩放”的方式绘制图片......

I want to display an image file using imshow. It is an 1600x1200 grayscale image and I found out that matplotlib uses float32 to decode the values. It takes about 2 seconds to load the image and I would like to know if there is any way to make this faster. The point is that I do not really need a high resolution image, I just want to mark certain points and draw the image as a background. So,

  • First question: Is 2 seconds a good performance for such an image or
    can I speed up.
  • Second question: If it is good performance how can I make the process
    faster by reducing the resolution. Important point: I still want the
    image to strech over 1600x1200 Pixel in the end.

My code:

import matplotlib
import numpy

plotfig     = matplotlib.pyplot.figure()
plotwindow  = plotfig.add_subplot(111)
plotwindow.axis([0,1600,0,1200])
plotwindow.invert_yaxis() 
img = matplotlib.pyplot.imread("lowres.png")
im  = matplotlib.pyplot.imshow(img,cmap=matplotlib.cm.gray,origin='centre')
plotfig.set_figwidth(200.0)
plotfig.canvas.draw()
matplotlib.pyplot.show()

This is what I want to do. Now if the picture saved in lowres.png has a lower resolution as 1600x1200 (i.e. 400x300) it is displayed in the upper corner as it should. How can I scale it to the whole are of 1600x1200 pixel?
If I run this program the slow part comes from the canvas.draw() command below. Is there maybe a way to speed up this command?

Thank you in advance!

According to your suggestions I have updated to the newest version of matplotlib

version 1.1.0svn, checkout 8988

And I also use the following code:

img = matplotlib.pyplot.imread(pngfile)
img *= 255
img2 = img.astype(numpy.uint8)
im  = self.plotwindow.imshow(img2,cmap=matplotlib.cm.gray, origin='centre')

and still it takes about 2 seconds to display the image... Any other ideas?

Just to add: I found the following feature

zoomed_inset_axes

So in principle matplotlib should be able to do the task. There one can also plot a picture in a "zoomed" fashion...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

冧九 2024-10-25 21:02:11

数据的大小与最终图像的像素尺寸无关。

既然您说不需要高分辨率图像,那么您可以通过对数据进行下采样来更快地生成图像。如果您的数据采用 numpy 数组的形式,则一种快速而肮脏的方法是使用 data[::n,::n]nth 列和行>。

您可以使用 fig.set_size_inchesplt.savefigdpi 参数控制输出图像的像素尺寸:

import matplotlib.pyplot as plt
import matplotlib.cm as cm
import numpy as np

data=np.arange(300).reshape((10,30))
plt.imshow(data[::2,::2],cmap=cm.Greys)

fig=plt.gcf()
# Unfortunately, had to find these numbers through trial and error
fig.set_size_inches(5.163,3.75)  
ax=plt.gca()
extent=ax.get_window_extent().transformed(fig.dpi_scale_trans.inverted())

plt.savefig('/tmp/test.png', dpi=400,
            bbox_inches=extent)

在此处输入图像描述

The size of the data is independent of the pixel dimensions of the final image.

Since you say you don't need a high-resolution image, you can generate the image quicker by down-sampling your data. If your data is in the form of a numpy array, a quick and dirty way would be to take every nth column and row with data[::n,::n].

You can control the output image's pixel dimensions with fig.set_size_inches and plt.savefig's dpi parameter:

import matplotlib.pyplot as plt
import matplotlib.cm as cm
import numpy as np

data=np.arange(300).reshape((10,30))
plt.imshow(data[::2,::2],cmap=cm.Greys)

fig=plt.gcf()
# Unfortunately, had to find these numbers through trial and error
fig.set_size_inches(5.163,3.75)  
ax=plt.gca()
extent=ax.get_window_extent().transformed(fig.dpi_scale_trans.inverted())

plt.savefig('/tmp/test.png', dpi=400,
            bbox_inches=extent)

enter image description here

送舟行 2024-10-25 21:02:11

您可以通过将以下行添加到 matplotlibrc 文件(通常位于 ~/.matplotlib/matplotlibrc)来禁用 imshow 的默认插值:

图像插值:无

结果是渲染速度更快,图像更清晰。

You can disable the default interpolation of imshow by adding the following line to your matplotlibrc file (typically at ~/.matplotlib/matplotlibrc):

image.interpolation : none

The result is much faster rendering and crisper images.

提赋 2024-10-25 21:02:11

我找到了一种解决方案,只要只需要显示低分辨率图像即可。可以使用

im  = matplotlib.pyplot.imshow(img,cmap=matplotlib.cm.gray, origin='centre',extent=(0,1600,0,1200))

范围参数告诉 matplotlib 在此范围内绘制图形的线来完成此操作。如果使用分辨率较低的图像,则会大大加快该过程。尽管如此,如果有人知道额外的技巧来使过程更快,以便以相同的速度使用更高分辨率,那就太好了。

感谢所有考虑我的问题的人,欢迎进一步评论!

I found a solution as long as one needs to display only low-resolution images. One can do so using the line

im  = matplotlib.pyplot.imshow(img,cmap=matplotlib.cm.gray, origin='centre',extent=(0,1600,0,1200))

where the extent-parameter tells matplotlib to plot the figure over this range. If one uses an image which has a lower resolution, this speeds up the process quite a lot. Nevertheless it would be great if somebody knows additional tricks to make the process even faster in order to use a higher resolution with the same speed.

Thanks to everyone who thought about my problem, further remarks are appreciated!!!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文