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.
how to easily read this file into verilog project?
ReplyDeletethank u for ur comments , and ur request will be posted soon
DeleteThis comment has been removed by the author.
ReplyDeleteuse $readmemh or $readmemb to read into a register variable as memory array.
ReplyDeleteThis is very efficient as the ISE doesn't need to wait the OS to complete file read operation.
the text file generated after conversion is in hexadecimal or decimal number system?
ReplyDeletecan we get image to txt file in binary data, currently it is giving in decimal format? if yes then please let us know
ReplyDelete@suguresh kumar :> yes it is possible. Convert the unsigned 8bit number to BINARY & write it to text file. Plz note to do it in loop for each element of 2D array.
Deleteits showing error as
ReplyDeleteError using imwrite (line 459)
Can't open file "newimage.jpg" for writing.
You may not have write permission.
Error in image_file_io (line 31)
imwrite(Jh,['newimage','.jpg']);
Undefined function or variable 'isize'.
ReplyDelete