Wednesday, 26 June 2013

IMAGE FILE TO TEXT FILE CONVERSION MATLAB – TEXT FILE TO IMAGE FILE CONVERSION MATLAB EXAMPLE



             Here is an example to convert an image into a text file in matlab, for processing in HDL (verilog) or in someother tool. Also the text file is further converted into image file.
           

An image is a 2D matrix. For converting into text file, it needs to be converted into 1D matrix and then to text file using file write in matlab. The image size is also a constraint, so it needs to be fixed for conversion. Then converted to 1D matrix using reshape command. Then write into text file.



A=imread('1.bmp');
B=rgb2gray(A);
disp('Image file read successful');
%figure,imshow(B),title('org');

C=imresize(B,[isize isize]);
figure,imshow(C),title('croped');
d=reshape(C,1,[]);
disp('Reshapping done');
fid = fopen('img.txt', 'wt');
fprintf(fid, '%8d\n', d);
disp('Text file write done');disp(‘ ‘);
fclose(fid);


For converting text file into image, it needs to be converted from 1D matrix and then to image. The image size must same as previous for processing text into image. Then converted to 1D matrix using reshape command. Then write into text file.

·         First, the text file is read using file read  as vector (1D matrix).
·         Then it has to be converted to matrix.
·         Finally transpose to complete it as image.

fidh = fopen('img.txt');
Ah = fscanf(fidh, '%g %g', [1 inf]);
disp('Text file read done');
fclose(fidh);
S1h= vec2mat(Ah,isize,isize);
disp('Vector conversion done');
        %c=imresize(S1,[128 128]);
Sh= transpose(S1h);
Jh=uint8(Sh);
disp('Image is ready');
imwrite(Jh,['newimage','.jpg']);
figure,imshow(Jh),title('IMAGE form TEXT file');


OUTPUT:






If you have any queries mail us to admin@elecdude.com
Viewers comments are encouraged.
It helps us to do better.

Thank you.

Reactions:

9 comments:

Post a Comment

Search Here...