How to store the images with colourmap in MATLAB - matlab

I am using HDF satellite data to retrieve bands from that I am concluding different vegetation indices. Every band in hdf data is in grey colour format, its a grey colour scale image. After HDF data processed I can convert into colour by using colour map (I am using jet for colourmap). My doubt is how to convert greyscale image into colourmaped while using imwrite. How to use colourmap within imwrite. I have tried many times, but the output is only in full blue colour, this spoil the output image. Please help me to do this.

Why use imwrite? You can use imshow.
Example:
imshow(im)
imshow(im,'Colormap',jet(255))
With reference: http://www.alecjacobson.com/weblog/?p=1655

Try using the ind2rgb function before using imwrite if you want to save to a format like .jpg, but if you are using an indexing image format (e.g. .png) you can just use imwrite directly as shown in the docs:
imwrite(X, map, filename)
where X is your greyscale image, map is your colourmap (i.e. jet) and filename is the is the name of the image you want to save ending in .png

Related

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?
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.
[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 ""

MATLAB imread() wrong gray scale

I made a simple grayscale image with paint.net:
Then I simply read the image using MATLAB imread() and got something like this (same thing for Octave):
I checked the background value and it's 55 instead of 255.
I then tried the same thing in Python using pyplot.imread() and get the expected result:
I saw this a couple of times even when I was reading something like Lena in MATLAB -- the gray scale was totally messed up. Does anyone know what's wrong with imread in MATLAB (and Octave)?
Your PNG image is an RGB image, not a gray-value image. It was saved as an indexed image, meaning that 56 different RGB values were stored in a table, and the image references those RGB values by specifying an index for each pixel.
The image you're seeing consists of the indices into the color table, not the actual RGB values saved.
You need to read both the indices and the color map as follows:
[img,cm] = imread('https://i.stack.imgur.com/rke2o.png');
Next, you can recover the original RGB image using ind2rgb, or, given that you are looking for a gray-value image, you can recover the gray-values using ind2gray:
img = ind2gray(img,cm);

MATLAB imshow for 3D color image

I have a medical imaging matrix of size [200x200x200].
In order to display it, I am currently using imshow3D function, which is an excellent tool, built by Maysam Shahedi.
This tool displays the 3D image slice by slice, with mouse based slice browsing
In my current project, I generate an RGB image for each z-layer from the original input image. The output is a 3D color image of size [200x200x200x3] (each layer is now represented by 3 channels).
The imshow3D function works great on grayscale images. Is it possible to use it to display RGB images?
I took a look at this nice imshow3D function from Matlab FileExchange, and it is quite straight-forward to change it to allow working with a stack of RGB images.
The magic part of the function is
imshow(Img(:,:,S))
which displays the slice S of the image Img. We can simply change it to show all 3 channels of image S by changing this to Img(:,:,S,:). The result will be of size 200-by-200-by-1-by-3, while MATLAB expects RGB images to be of size 200-by-200-by-3. Simply squeeze this image to get the correct dimension. This results in:
imshow(squeeze(Img(:,:,S,:))
So to show RGB images, do a search-and-replace inside the function imshow3D, to replace all occurrences of Img(:,:,S) with squeeze(Img(:,:,S,:)) and it works!

imshow shows different output than imwrite

I was wondering how it is possible to save an image created by imshow in matlab. The code below uses the imshow function with the min and max arguments specified - How can I apply this directly to the image itself instead of just specifying Matlab to show it?
maxBlur=3;
a = imshow(fDmap,[0 maxBlur]);
imwrite(a, 'img.png');
Writing to the file produces a different output to what is shown via imshow.
Can anyone suggest how to get the output from imshow saved as an image?
To specify upper and lower intensity limits for imwrite (similar to what you've done for imshow), you will want to use the second inputs to mat2gray to adjust the contrast of your image prior to saving it with imwrite.
imwrite(mat2gray(fDmap, [0 maxBlur]), 'img.png');
If you literally want an image of what you're seeing with imshow, you can use saveas to take a screenshot. This will potentially be lower resolution than the previous approach and will also include whitespace around the image.
imshow(fDmap, [0 maxBlur]);
saveas(gcf, 'img.png');
NOTE: The a variable that you passed to imwrite in your post is a MATLAB graphics handle to an image object that is used to manipulate the rendered image. imwrite expects image data in matrix form not as a graphics handle.

Colormap and GIF images in Matlab

I have a problem understanding colormaps in Matlab and using them to import and diplay .gif images.
I would like to import an image using
im = imread('I.gif')
and then display it using
imshow(im)
but the result is wrong
If I do
[im,map] = imread('I.gif')
and then display it using
imshow(im,map)
it works properly, but still I don't understand the need of this colormap
Is there a way to import and convert my gif image to greyscale so that when I do
imshow(im)
it shows the correct greyscale image without having to worry about the colormap?
SOrry for the noob question but I am just starting with image processing in Matlab and I would really appreciate some help. It is my first question! :)
Bye and thanks!
If you want to convert your gif to grayscale, use ind2gray:
[im,map] = imread('I.gif');
imGray = ind2gray(im,map);
The reason that you need the colormap is that the gif format doesn't store image intensities, it stores indices into the colormap. So color 0 could be red or green or a very light shade of mauve. It's the colormap that stores the actual RGB colors that the image needs. ind2gray will take each of those colors, convert them to a grayscale intensity, and replace the indices in the image with those intensities.
It looks like you've really already answered the question. It seems that .gif files support an indexed color format, as a way of saving space. See:
https://en.wikipedia.org/wiki/Indexed_color
This is different than a more typical RGB color, which is what is often received from an IMREAD call.
To convert to a grayscale, you would need to go through the colormap and assign a grayscale value to each color, and then substitute those values back into the im variable.