Image to Text conversion

I try to convert image of size 512 x 512 into text file of hexadecimal values using this code
a= imread('im16_1a.png');
I=rgb2gray(a);
imgTrans = I';
% iD conversion
img1D = I(:);
% Decimal to Hex value conversion
imgHex = dec2hex(img1D);
% New txt file creation
fid = fopen('im16_1.txt', 'wt');
% Hex value write to the txt file
fprintf(fid, '%x\n', imgHex);
% Close the txt file
fclose(fid)
the text file must contain (512 x 512) 262144 values but it shows (512 x 512 x 3) 786432 values, pls help, what to do.. ??

5 Comments

Are you sure this is the code you're actually running? I tried it on one of the images MATLAB provides:
a= imread('board.tif');
and got the following sizes:
>> size(a)
ans =
648 306 3
>> size(I)
ans =
648 306
>> size(img1D)
ans =
198288 1
And the contents of the file are a single column, as you would expect if you print img1D.
yes ofcourse, img1D has expected value, but it was in decimal but i need the same in hexadecimal
Right, I forgot this line:
>> size(imgHex)
ans =
198288 2
This is equal to the product 648*306, so I'm still not reproducing your results.
any suggestions??
How can we make suggestions if we can't reproduce your problem?

Sign in to comment.

Answers (1)

Geoff Hayes
Geoff Hayes on 5 May 2014
Edited: Geoff Hayes on 5 May 2014
The code is converting the uint8 integer array, Img1D, to the hexadecimal equivalent, stored as characters:
imgHex = dec2hex(img1D);
imgHex has the same number of rows as Img1D but two columns. The code then writes out the data to the text file with
fprintf(fid, '%x\n', imgHex);
But does the above not mean that the we are writing in hex the already hex'ed data? If I do the same with an image (once converted to a 1D array) of 215760 pixels, then the imgHex is 215760x2. I then write to the file (as above) and when I try to read it in again line-by-line (using getl), the result is a 431520x2 array which is twice the size of the original. In fact, just writing out one line to the file (with the above fprintf) results in two lines in the output file.
I think what you really want to be doing is to write out the hex of the uint8 data instead:
fprintf(fid, '%x\n', img1D);
Please try out the above line to see if that helps.
EDIT:
Try the following to demonstrate what is happening with the writing of the hex data in hex:
fprintf('%x\n','00')
The '00' is two character equivalent of the conversion from uint8 (to hex) of the integer 0. The fprintf will then convert each character in this string to its hex equivalent. The hex equivalent for each character is '30'. And so the output is
30
30

2 Comments

dipak
dipak on 26 Feb 2016
Edited: Geoff Hayes on 27 Feb 2016
img = rgb2gray(img);
imshow(img);
dlmwrite('image.txt',img,'delimiter','\t');
To match the user's requirements you would need
dlmwrite('image.txt', img(:), 'precision', '%02x');

Sign in to comment.

Categories

Find more on Convert Image Type in Help Center and File Exchange

Asked:

on 5 May 2014

Commented:

on 27 Feb 2016

Community Treasure Hunt

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

Start Hunting!