Search code examples
matlabgnuplotgrayscale

How to read image data as a 2D grid using gnuplot


Gnuplot is a very powerful library that supports plotting of functions with numerous scientific operations. What my case is I want to read a single channel grayscale image just as we read in matlab or python using imread and store it into a 2D data grid using gnuPlot.

Basically I want to make contours of image gray scale intensities.To do that I am exporting the single channel luminance data of the image as a .dat file using matlab once it is exported I splot it using:

set contour base 
splot 'greyScaleImagePixelByPixelData.dat' matrix

This works fine but in case I dont want to use Matlab to export the pixel by pixel data to surface plot the image what is the way around?


Solution

  • The example below has been tested with 8-bit and 16-bit grayscale png images (no alpha channel). If your particular images do not match this, please provide a more complete description of how they are encoded.

    You haven't said exactly what you want to do with the pixel data after reading it in, so I show the obvious example of displaying it as an image (e.g. a regular array of pixels). If you want to do further manipulation of the values before plotting, please expand the question to give additional details.

    [~/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
    

    Note that gnuplot does not have any separate storage mode for grayscale vs. RGB image data. In this case it loads 3 copies of each 16-bit grayscale value into parallel storage as if it were separate R/G/B pixel data.

    enter image description here

    [2nd edit: show both grayscale image and contour levels]

    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