Creating an Image from a Textfile Data using Matlab

조회 수: 7 (최근 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 CenterFile Exchange에서 Large Files and Big Data에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by