imread, reshape and zeros function syntax problems

1 view (last 30 days)
I am completely new to matlab. Studing some sample codes to learn matlab. I have some basic questions in the below code:
input_image_filename= './Images/Image_64x64.jpg'; % It is a 64x64 colored image
input_im_3D= imread(input_image_filename);
What is the return type of input_im_3D ? What I observe that it is storing some huge data in the variable input_im_3D?
n_pix= 12288;
x = double(reshape(input_im_3D, [n_pix 1]));
What I observe is that reshape function is putting data in the single column of a excel sheet till 12288.
I am really not able to understand the working of reshape function? Return type of reshape function ? What changes double put in the results? What does the syntax of [n_data 1] means ?
n_data= 16384 ;
x = [x; zeros(n_data - n_pix , 1)];
Please also explain me the syntax of zeros?

Accepted Answer

Walter Roberson
Walter Roberson on 16 Jan 2022
imread() returns the data type stored in the file. The most common data type stored in image files is uint8(), but there are other possibilities. For example,
basename = tempname();
fn1 = basename + "logical.png";
fn3 = basename + "uint16.png";
img1 = rand([48 64]) < 0.5;
img3 = randi([0 65535], [48 64 3], 'uint16');
imwrite(img1, fn1);
imwrite(img3, fn3);
back1 = imread(fn1);
back3 = imread(fn3);
whos back1 back3
Name Size Bytes Class Attributes back1 48x64 3072 logical back3 48x64x3 18432 uint16
isequal(img1, back1)
ans = logical
1
isequal(img3, back3)
ans = logical
1
True floating point images are possible, but writing one out takes more work for demonstration takes more work than I care to bother with at the moment.
  7 Comments
Walter Roberson
Walter Roberson on 16 Jan 2022
Remember that you started with a 64 x 64 x 3 matrix (RGB image), and you shaped that as a column of data, and then you added 4096 zeros to the end of the column. Now you are reshaping that result to 64 x 64 x 4.
But 4096 = 64 x 64, so what you have effectively done is taken the original 64 x 64 x 3 array, and added another layer of zeros on in the third dimension, giving you a 64 x 64 x 4 result. The same result could have been achieved by
y_data = input_im_3D;
y_data(:,:,4) = 0; %add a 4th layer that is all 0
After that, selecting y_data(1:64, 1:64, 1:3) would be the same as y_data(:,:,1:3)... which would be the data with that layer of 0 removed, giving you back the original data.
It is not clear why any of this is being done.
Manu Chaudhary
Manu Chaudhary on 16 Jan 2022
Trying to test quantum image processing application. Image converted to Textfile data of 16384 length-> Normalized -> Given Input to quantum circuit simulator -> Result taken out -> Image regenerated using matlab. Testing the basic input output image functionality of matlab.
Thank you Walter for this great help.

Sign in to comment.

More Answers (0)

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!