简体   繁体   English

在Python中以全分辨率显示和保存大型2D矩阵

[英]Display and Save Large 2D Matrix with Full Resolution in Python

I have a large 2D array (4000x3000) saved as a numpy array which I would like to display and save while keeping the ability to look at each individual pixels. 我有一个较大的2D数组(4000x3000),另存为一个numpy数组,我想显示并保存它,同时保持查看每个像素的能力。 For the display part, I currently use matplotlib imshow() function which works very well. 对于显示部分,我目前使用matplotlib imshow()函数,该函数效果很好。

For the saving part, it is not clear to me how I can save this figure and preserve the information contained in all 12M pixels. 对于节省部分,我不清楚如何保存该数字并保留所有1200万像素中包含的信息。 I tried adjusting the figure size and the resolution (dpi) of the saved image but it is not obvious which figsize/dpi settings should be used to match the resolution of the large 2D matrix displayed. 我尝试调整图形大小和保存图像的分辨率(dpi),但不清楚应该使用哪种figsize / dpi设置来匹配显示的大型2D矩阵的分辨率。 Here is an example code of what I'm doing (arr is a numpy array of shape (3000,4000)): 这是我在做什么的示例代码(arr是一个形状为numpy的数组(3000,4000)):

fig = pylab.figure(figsize=(16,12))
pylab.imshow(arr,interpolation='nearest')
fig.savefig("image.png",dpi=500)

One option would be to increase the resolution of the saved image substantially to be sure all pixels will be properly recorded but this has the significant drawback of creating an image of extremely large size (at least much larger than the 4000x3000 pixels image which is all that I would really need). 一种选择是实质上提高已保存图像的分辨率,以确保所有像素都将被正确记录,但这具有显着缺点,即创建尺寸非常大的图像(至少比4000x3000像素的图像大得多)我真的需要)。 It also has the disadvantage that not all pixels will be of exactly the same size. 它还具有并非所有像素都具有完全相同的尺寸的缺点。

I also had a look at the Python Image Library but it is not clear to me how it could be used for this purpose, if at all. 我也看过了Python图像库,但对我来说还不清楚如何将其用于此目的。

Any help on the subject would be much appreciated! 在这个问题上的任何帮助将不胜感激!

I think I found a solution which works fairly well. 我想我找到了一个效果很好的解决方案。 I use figimage to plot the numpy array without resampling. 我使用figimage绘制numpy数组而无需重新采样。 If you're careful in the size of the figure you create, you can keep full resolution of your matrix whatever size it has. 如果您对所创建图形的大小谨慎,则无论矩阵大小如何,都可以保持完整分辨率。

I figured out that figimage plots a single pixel with size 0.01 inch (this number might be system dependent) so the following code will for example save the matrix with full resolution (arr is a numpy array of shape (3000,4000)): 我发现figimage绘制了一个大小为0.01英寸的单个像素(此数字可能与系统有关),因此以下代码例如将以全分辨率保存矩阵(arr是一个形状为(3000,4000)的numpy数组):

rows = 3000
columns = 4000
fig = pylab.figure(figsize=(columns*0.01,rows*0.01))
pylab.figimage(arr,cmap=cm.jet,origin='lower')
fig.savefig("image.png")

Two issues I still have with this options: 这个选项仍然有两个问题:

  1. there is no markers indicating column/row numbers making it hard to know which pixel is which besides the ones on the edges 没有标记指示列/行号,除了边缘的像素外,很难知道哪个像素
  2. if you decide to interactively look at the image, it is not possible to zoom in/out 如果您决定交互式查看图像,则无法放大/缩小

A solution that also solves the above 2 issues would be terrific, if it exists. 如果存在的话,也可以解决上述两个问题的解决方案将是很棒的。

The OpenCV library was designed for scientific analysis of images. OpenCV库旨在用于图像的科学分析。 Consequently, it doesn't "resample" images without your explicitly asking for it. 因此,如果您没有明确要求,它不会“重新采样”图像。 To save an image: 要保存图像:

import cv2
cv2.imwrite('image.png', arr)

where arr is your numpy array. 其中arr是您的numpy数组。 The saved image will be the same size as your array arr . 保存的图像将与数组arr相同。

You didn't mention the color-model that you are using. 您没有提到您使用的颜色模型。 Pngs, like jpegs, are usually 8-bit per color channel. 像jpeg一样,Png通常每个颜色通道为8位。 OpenCV will support up to 16-bits per channel if you request it. 如果您要求,OpenCV将支持每个通道最多16位。

Documentation on OpenCV's imwrite is here . 有关OpenCV的imwrite的文档在此处

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM