imread, reshape and zeros function syntax problems

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

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

The return type of reshape() is exactly the same as the original data object. When reshape() is applied to any of the primitive data types (such as numeric or cell), then reshape() creates a new "view" on the data in which the data has exactly the same type as before, but is organized in the specified number of rows and columns without changing the memory organization
Suppose that you had the array
A = [1 2; 3 4; 5 6]
A = 3×2
1 2 3 4 5 6
then unlike in C or C++, the internal implementation does not work by storing each row or column separately. Instead, the data is stored as a continuous stream in memory. The organization is to go "down" each column in turn and then move on to the next column. So the actual in-memory order of the data for A would be
1
3
5
2
4
6
as consecutive memory locations.
MATLAB knows what size of array this stream of memory represents because it has a separate header that gives the dimensions... [3 2] (three rows, two columns).
Now suppose you take the same internal data pointer, but give it a new header that says that the array size is [1 6] -- one row, six columns. The data does not need to move around in memory, just the header changes.
reshape(A, [1, 6])
same data type, same internal data pointer, but the "interpretation" of the data is now as
1 3 5 2 4 6
The data returned by imread() was very likely uint8 . uint8 is a series of 8-bit unsigned integers, possible values 0, 1, 2, 3, 4, ... up to 255, all as 8-bit patterns such as 01011010 internally (that would correspond to integer decimal 90).
When you call double() then a representation conversion is done that attempts to preserve value as best possible, but using a diffferent data type -- in particular, as IEEE 754 Double Precision, which is a 64 bit floating point representation that is notably more complicated, but can also represent a much fine range of values. double() does not just use the same bits and re-interpret them: it finds the nearest double-precision value that represents the uint8 value. In this example it would create the double precision value 90.0 (which happens to have bit pattern 0000000000000000000000000000000000000000100000000101011001000000 internally)
But... you have to be careful. Functions such as image() and imshow() and imwrite() expect that if an array is double precision, that the image information is represented in the range 0.0 to 1.0 and that any value below 0.0 is to be treated as 0.0 and any value above 1.0 is to be treated as 1.0 . There sometimes are good reasons to convert integer image data to the nearest floating point equivalent... but if you do so you have to be careful to later convert back to integer data type for image processing purposes.
What does the syntax of [n_data 1] means ?
In this context, n_data has already been assigned a value. The [n_data 1] part means that the value associated with n_data is retrieved, and that you then want to add the number 1 on as an additional column to it, producing a longer vector. (If n_data was not already a scalar or a row vector then you would encounter an error in trying to add the 1 as an additional column.)
In the context of the reshape(), it is asking for the output of reshape() to be a 12288 by 1 vector.
n_data= 16384 ;
x = [x; zeros(n_data - n_pix , 1)];
In context, x has already been created as a column vector of data, that is n_pix rows long and 1 column.
n_data - n_pix is subtracting 16384 - 12288, getting back 4096 .
zeros(4096, 1) is then asking to create an all-zero array that is 4096 by 1.
[x; zeros(etc)] is then asking to append the zeros to the bottom of the column of data that is stored in x.
So what is happening is that you are taking an image that has 12288 elements in it, and re-arranging the data to be a column of data, and then zero-padding it with 4096 more zeros, for a total of 16384 elements.
Thank you for answering my questions.
Suppose we have
y_data = reshape(y_perm_out, [64 64 4]); % y_perm_out is 16384 x1 matrix
out_im_3D = y_data(1:64, 1:64, 1:3);
I am really not able to understand the syntax of 64 64 4 in the arguments of rshape function and then 1:64, 1:64 and 1:3. All these operations are related to image generation. It would be really great if you can explain me the above two lines of code.
out_im_3D = uint8(abs(out_im_3D));
imwrite(out_im_3D, output_image_filename);
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.
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)

Categories

Find more on Mathematics in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!