Creating an Image from a Textfile Data using Matlab
2 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Manu Chaudhary
le 14 Jan 2022
Commenté : Manu Chaudhary
le 15 Jan 2022
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.
0 commentaires
Réponse acceptée
Simon Chan
le 15 Jan 2022
Modifié(e) : Simon Chan
le 15 Jan 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 commentaires
Simon Chan
le 15 Jan 2022
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.
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Large Files and Big Data dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!