Image to Text conversion
34 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
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 Kommentare
Antworten (1)
Geoff Hayes
am 5 Mai 2014
Bearbeitet: Geoff Hayes
am 5 Mai 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 Kommentare
dipak
am 26 Feb. 2016
Bearbeitet: Geoff Hayes
am 27 Feb. 2016
img = rgb2gray(img);
imshow(img);
dlmwrite('image.txt',img,'delimiter','\t');
Walter Roberson
am 27 Feb. 2016
To match the user's requirements you would need
dlmwrite('image.txt', img(:), 'precision', '%02x');
Siehe auch
Kategorien
Mehr zu Convert Image Type finden Sie in Help Center und File Exchange
Produkte
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!