im2double yields different results - matlab

I am trying to get RGB matrices for an image. When my image is 1200x1600, the following code
I=imread('testme.jpg');
I=im2double(I);
yields a 1200x1600x3 matrix and I can get RGB matrices but when the image is a screenshot of a part of this image, the code below
I=imread('testme_subpic.jpg');
I=im2double(I);
yields 167x228 matrix and I can't get the RGB matrices.
Likely, when I write
I=imread('testme.png');
I=im2double(I);
lines give me a 1200x1600 matrix.
My question is why can't I get a 3-dimensional matrix with the png or the smaller-sized jpg and how do I get it?

It is all about the way the images were saved.
Check wikipedia for some extra info about png pixel formats. To avoid this issue you can try to use MATLAB itself to write your images, that way you have control over pixel formats (imwrite)

Related

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);

Scramble the pixels of black and white images in Matlab

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.

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!

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.

How to display an image's matrix in MATLAB?

How can we display an image's matrix in MATLAB?
I have read the image using imread and have converted it into binary image. How do I see an image's matrix?
If you want to see the actual matrix, use disp(I) where I is the image. If you want to view it as an image, use imagesc(I) or imshow(I).