如何使用 gnuplot 将图像数据读取为 2D 网格

问题描述

Gnuplot 是一个非常强大的库,支持绘制具有大量科学操作的函数。我的情况是我想读取单通道灰度图像,就像我们使用 imread 在 matlab 或 python 中读取一样,并使用 gnuPlot 将其存储到二维数据网格中。

基本上我想绘制图像灰度强度的轮廓。为此,我使用 matlab 将图像的单通道亮度数据导出为 .dat 文件,我使用以下方法绘制它:>

set contour base 
splot 'greyScaleImagePixelByPixelData.dat' matrix

这很好用,但如果我不想使用 Matlab 逐个像素地导出像素数据来表面绘制图像,那该怎么办?

解决方法

以下示例已使用 8 位和 16 位灰度 png 图像(无 alpha 通道)进行了测试。如果您的特定图片与此不符,请提供更完整的编码说明。

在读入像素数据后,您还没有确切说明要对像素数据做什么,因此我展示了将其显示为图像(例如,常规像素阵列)的明显示例。如果您想在绘图前对值进行进一步操作,请扩展问题以提供更多详细信息。

[~/temp] file galaxy-16bitgrayscale.png
galaxy-16bitgrayscale.png: PNG image data,580 x 363,16-bit grayscale,non-interlaced
[~/temp] gnuplot
set autoscale noextend
plot 'galaxy-16bitgrayscale.png' binary filetype=png with rgbimage

请注意,gnuplot 没有任何单独的灰度与 RGB 图像数据存储模式。在这种情况下,它将每个 16 位灰度值的 3 个副本加载到并行存储中,就好像它们是单独的 R/G/B 像素数据一样。

enter image description here

[第二次编辑:同时显示灰度图像和轮廓级别]

set autoscale noextend
set view map
set contour surface
set cntrparam levels discrete 100,200
set cntrparam firstlinetype 1
set key outside title "Contour levels"
splot 'galaxy16bit.png' binary filetype=png with rgbimage notitle,\
      '' binary filetype=png with lines nosurface title ""

enter image description here