Modifying an imageDatastore without restoring the images - matlab

I am using an imageDatastore containing the labels and grayscale images for a deep learning classification problem. I want to fine tune alexnet on my dataset but it accepts RGB images.
How can I modify the imageDatastore without storing them again assuming that I just want to concatenate the grayscale image in 3 channels?

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 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!

Built in Images on Matlab

Are there any matlab images that are built in like 'mandrill' that are at least 512x512? I need to load an image that is already built in with this size and then apply SVD compression, but I'm unfamiliar with any images that are of this size.
rgb = imread('ngc6543a.jpg');
Gives you a 650*600 image.
which('ngc6543a.jpg')
points you at a directory with a few more images and stuff.

How to store the images with colourmap in 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

Image Conversions in Matlab

I converted an RGB image (which is in double format) to a gray scale image of the same format using rgb2gray in Matlab. Now I want to convert the same image from gray to RGB. I used gray2rgb in Matlab but it's giving an error. So how can we convert a grayscale image to an RGB image using Matlab?
Short answer: you can't. Not perfectly at least.
As Sean says, this is because you have dropped some information when converting to grayscale. In other words, converting back from grayscale to RGB is an under-determined inverse problem, so there is no easy solution.
Now this doesn't mean you can't try. If you have some prior on the image, you can use it in addition to the information you have left to compute an estimate of the original RGB image.
For example if you know (or suppose) that the original image was already grayscale (in an RGB container) then you can reverse the process exactly. This is what the gray2rgb function Sean mentions is doing.
Most of these are open problems, so it's probably beyond what you want.
I'm sorry to say it's not possible.
By converting the image to grayscale you've reduced the amount of information (3 dimensions at each pixel down to 1) and this can't be recovered.
The rgb2gray function is one included in Matlab and works fine.
The gray2rgb function is not a standard Matlab function. If you are referring to this function on Matlab central, it's documentation states it doesn't do anything useful but just creates a 3d matrix from the 1d matrix; the image will still be grayscale.