Creating an Image from a Textfile Data using Matlab

1 回表示 (過去 30 日間)
Manu Chaudhary
Manu Chaudhary 2022 年 1 月 14 日
コメント済み: Manu Chaudhary 2022 年 1 月 15 日
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.

採用された回答

Simon Chan
Simon Chan 2022 年 1 月 15 日
編集済み: Simon Chan 2022 年 1 月 15 日
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 件のコメント
Simon Chan
Simon Chan 2022 年 1 月 15 日
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.
Manu Chaudhary
Manu Chaudhary 2022 年 1 月 15 日
Thank you Simon. Great help.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeImage Processing Toolbox についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by