Creating an Image from a Textfile Data using Matlab
2 次查看(过去 30 天)
显示 更早的评论
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 个评论
采纳的回答
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
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.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Image Processing Toolbox 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!