Creating an Image from a Textfile Data using Matlab

I am completely new to matlab and image related applications. I am struggling to understand image related functionality in matlab. With the below lines of code, I am breaking the 64x64 size colored image and storing it in a textfile after normalization.
input_image_filename = './Images/image_64x64.jpg'; % A 64x64 size colored image
input_image_3D = imread(input_image_filename); % Breaking image into pixel
value_data= double(reshape(input_image_3D,[],1));
norm_image_3D= value_data/norm(value_data);
fid= fopen('filename.txt','wt');
fprintf(fid,'%0.16f\n',norm_image_3D); % Written the image to file % The number of entries i am getting in text file is 12288
fclose(fid);
After this, suppose I want to generate the image back from the textfile data, how should I do that? I tried something as below but it doesnot work.
filename = 'filename.txt';
testing_image_filename= './Testing_64x64_output.jpg';
testingReadFile= importdata(filename);
imwrite(testingReadFile,testing_image_filename);
imshow3D(testingReadFile);
Please help me in completing this code.

 Risposta accettata

Simon Chan
Simon Chan il 15 Gen 2022
Modificato: Simon Chan il 15 Gen 2022
Try the followings: (with editing below)
Normalization just divide by 255 for uint8 image.
[Ny,Nx,Nc] = size(input_image_3D); % Ypur input_image_3D, size is 64x64x3
J = zeros(size(input_image_3D)); % Initialize matrix J
for k =1:Nc
I = double(input_image_3D(:,:,k));
J(:,:,k) = I/255; % Perform normalization channel by channel
end
J = reshape(J,Ny,[],1); % Reshape to 2 dimensions first
J = reshape(J,[],1); % Reshape to a column vector
%% No commnent below, same as your code
%
%
fid=fopen('filename.txt','wt');
fprintf(fid,'%0.16f\n',J);
fclose(fid);
% The txt file is a column vector and hecnce you need to revert back to
% its original size. Also use imshow instead of imshow3D
%
testingReadFile=importdata('filename.txt'); % Import the txt file
revertI = reshape(testingReadFile,Ny,[]); % Reshape back to 64 rows first
revertI = reshape(revertI,Ny,[],Nc); % Revert back to 3 channels
imshow(revertI,[]) % Showing the image

6 Commenti

Thank you for the response. The image which is being displayed is not created by reading data from the textfile. imshow(I, []) is creating the image directly from the image. I am looking for a solution where image is recreated from the data read from the textfile.
Simon Chan
Simon Chan il 15 Gen 2022
Modificato: Simon Chan il 15 Gen 2022
Sorry for some errors.
Revised the following:
(1) Suimply divide the image pixel values by 255 for uint8 image for normalization
(2) Typo error on last line, now showing the reverted image
I tried your code as below:
input_image_filename = './Images/Image_64x64.jpg'; % A 64x64 size colored image
[Ny,Nx,Nc] = size(input_image_3D); % Ypur input_image_3D, size is 64x64x3
J = zeros(size(input_image_3D)); % Initialize matrix J
for k =1:Nc
I = double(input_image_3D(:,:,k));
J(:,:,k) = normalize(I); % Perform normalization channel by channel
end
J = reshape(J,Ny,[],1); % Reshape to 2 dimensions first
J = reshape(J,[],1); % Reshape to a column vector
%% No commnent below, same as your code
%
%
fid=fopen('filename.txt','wt');
fprintf(fid,'%0.16f\n',J);
fclose(fid);
% The txt file is a column vector and hecnce you need to revert back to
% its original size. Also use imshow instead of imshow3D
%
testingReadFile=importdata('filename.txt'); % Import the txt file
testing_image_filename= './Testing_64x64_output.jpg';
revertI = reshape(testingReadFile,Ny,[]); % Reshape back to 64 rows first
revertI = reshape(revertI,Ny,[],Nc); % Revert back to 3 channels
imwrite(revertI,testing_image_filename);
imshow(revertI,[]) % Showing the image
The input and output image i am testing are attached, which differs.
I also tried updating my initial code as below:
input_image_filename = './Images/Image_64x64.jpg'; % A 64x64 size colored image
input_image_3D = imread(input_image_filename); % Breaking image into pixel
value_data= double(reshape(input_image_3D,[],1));
%norm_image_3D= value_data/norm(value_data);
fid= fopen('filename_test1.txt','wt');
fprintf(fid,'%0.16f\n',value_data); % Written the image to file
fclose(fid);
filename = 'filename_test1.txt';
testing_image_filename= './Testing_64x64_output.jpg';
testingReadFile= importdata(filename);
data_in_matrix_form= double(reshape(testingReadFile,[64 64 3]));
imwrite(data_in_matrix_form,testing_image_filename);
imshow(data_in_matrix_form);
With this code I am getting the result as attached, which is also wrong.
Your last updated code gave me the perfect answer. Thank you for this great help.It would be great if you give some links to learn matlab.
Sorry for the confusion. Previous code using function 'normalize' would change the range of pixel values and hence the reverted picture is not correct. So simply divide by 255 (uint8 image) should preserve the range.
Thank you Simon. Great help.

Accedi per commentare.

Più risposte (0)

Categorie

Community Treasure Hunt

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

Start Hunting!

Translated by