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!
Related
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 ""
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);
I have a series of black and white images (not greyscale, black and white; 2D matrices in Matlab), and I need to randomly scramble the pixels. I found this package in Mathworks File Exchange (https://it.mathworks.com/matlabcentral/fileexchange/66472-image-shuffle); one of the functions, imScrambleRand, does exactly what I need, but it works for RGB images (3D matrices). Is there a way to transform b&w images into 3D matrices so that I can use that function? Or can anyone suggest any other script that does what I need? Keep in mind that I'm not familiar with Matlab, but I'll do my best.
Thank you.
EDIT 1: When I import the BW image I get a 2D matrix of logic values (0 = black, 1 = white). I think the different data format (logic vs integer) is what yields errors when using the function for RGB images.
EDIT 2: I adapted the demo code from the aforementioned package and I used the suggestion by #Jonathan for transforming a 2D matrix into a 3D matrix, and added a loop to transform the logic values into RGB integer values, then use the imScrambleRand function. It works, but what I obtain is the following image: SCRAMBLED IMAGE. This is the BW picture I start with: BW IMAGE. So I checked the scrambled image, and the function from the FEX file actually scrambles within the RGB values, meaning that I found, for instance, a pixel with RGB 0,255,0. So I solved a problem but actually there's a problem within the function: it doesn't scramble pixels, it scrambles values generating colors that were not in the original picture.
EDIT 3: I used the code provided by #nhowe and I obtain exactly what I need, thanks!
EDIT 4: Ok, turns out it's not ok to scramble the pixels since it makes the image too scattered and different from the starting image (you don't say?), but I need to scramble BLOCKS OF PIXELS so that you can't really recognize the image but the black pixels are not too scattered. Is there a way to do that using the code provided by #nhowe?
EDIT 5: It should be ok with this function: https://it.mathworks.com/matlabcentral/fileexchange/56160-hio-been-hb-imagescramble
A simple way to scramble matrix M:
r = rand(size(M));
[~,ri] = sort(r(:));
M(ri) = M;
The simplest solution to go from grayscale to RGB might be this:
rgbImage = cat(3, grayImage, grayImage, grayImage);
Then apply your function from FEX and extract one color channel, assuming that the FEX function will yield three identical color channels.
This question already has an answer here:
What does the index refer to when selecting a pixel on an image in Matlab?
(1 answer)
Closed 6 years ago.
I have gray scale image "lena.bmp". I want read this image in matlab using imread() function.
When i use code below to read and show image my image is dark (black).
img = imread('lena.bmp');
imshow(img);
But when i use code below, I have no problem to view.
[img map]= imread('lena.bmp');
imshow(img,map);
It seems that my first code doses not reading image in grayscale mode (like what rgb2gray function generate).
My image is as follows:
What can i do to solve this problem?
Your image is an "indexed" image. That means it contains integer values which act as "labels" more than anything, and each of those labels is mapped to a colour (i.e. an rgb triplet). Your map variable represents that mapping; at row 5 you have the rgb triplet that corresponds to 'label' "5", for instance.
To see what I mean, do unique(img) and you'll see that the values of your img array are in fact quite regular. The command rgbplot can demonstrate the actual colourmap graphically. Run rgbplot(map) on your map variable to see the mapping for each of the red green and blue colours.
Now, save and read the image below on your computer as img2 and compare the array values.
This image was generated by converting from the "indexed" image you linked to, to a "grayscale" one using photoediting software (the GIMP). The difference is that
in a grayscale image, the pixel values represent actual intensities, rather than integer 'labels'. Imread reads grayscale images as uint8 images by default, meaning it assigns intensity values to pixels ranging from 0 (black) to 255 (white). Since these values happen to be integers you could still cheat and treat them as 'labels' and force a colour-mapping on them. But if you assign a 'linear map' (i.e. value 1 = intensity 1, value 2 = intensity 2, etc) then your image will look as you would expect.
You'll see that the values from unique(img2) are quite different. If you imshow(img2) you'll see this displays as you'd expect. If you don't specify a colormap for imshow, it will assume that the map is a linear mapping from the lowest to the highest value in the image array, which explains why your indexed image looked weird, since its values were never supposed to correspond to intensities.
Also try imagesc(img2) which will show this but using the "current" colormap. imagesc causes the colormap to be "scaled", so that the lowest colour goes to the lowest value in the image, and similarly for the highest.
The default colormap is jet so you should see a psychedelic looking image but you should be able to make out lena clearly. If you try colormap gray you should see the gray version again. Also try colormap hot. Now to make sense of the colormaps, try the rgbplot command on them (e.g. rgbplot(gray), rgbplot(hot) etc).
So, going back to imshow, imshow basically allows you to display an indexed image, and specify what colormap you want to use to display it. If you don't specify the colormap, it will just use a linear interpolation from the lowest value to the highest as your map. Therefore imshow(img) will show the image pretty much in the same way as imagesc(img) with a gray colormap. And since the values in your first img represent evenly spaced 'labels' rather than actual intensities, you'll get a rubbish picture out.
EDIT: If you want to convert your indexed image to a grayscale image, matlab provides the ind2gray function, e.g.:
[img, map] = imread('lena.bmp');
img_gray = ind2gray(img, map);
This is probably what you need if you mean to process pixel values as intensities.
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