- Please post one question per problem only
- See How to format code in the forum
- What does the Hex file contain? The image data only or even the JPEG header and meta data?
convert hex data to viewable image(JPEG) in matlab
32 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Sushikshitha C
el 29 de Jun. de 2018
Comentada: Chandler Timm Doloriel
el 31 de Mzo. de 2022
i have a hex data of JPEG image from a software called "HxD Editor".The file attached below is the text file of hex characters now, by using that convert back to its original JPEG image in matlab R2018a
fid = fopen('hexdata.txt', 'r');
% Scann the txt file
img = fscanf(fid, '%x', [256,120]);
% Close the txt file
fclose(fid);
% restore the image
outImg = reshape(img,[128 128]);
outImg = outImg';
figure, imshow(outImg,[])
3 comentarios
Guillaume
el 29 de Jun. de 2018
Editada: Guillaume
el 29 de Jun. de 2018
And duplicated again as https://www.mathworks.com/matlabcentral/answers/408047-display-clear-jpeg-image-from-hex-characters-in-matlab-r2018a
Stop asking the same question over and over. This only irritates us.
edit: and again!
Respuestas (2)
Jan
el 29 de Jun. de 2018
Editada: Jan
el 29 de Jun. de 2018
Import the HEx data as 8 Bit values:
img = fscanf(fid, '%2x', [256,120])
% ^ this matters
You cannot reshape a [256 x 120] array to a [128 x 128] array. Reshaping does not change the number of elements. Maybe you mean imresize? The lack of comments in your code force the readers to guess, what you want to do.
0 comentarios
Guillaume
el 29 de Jun. de 2018
Editada: Guillaume
el 29 de Jun. de 2018
Clearly, you have absolutely no idea what you're doing.
A quick search of the first few bytes of your hexadecimal stream reveals that FF D8 FF E0 is typical of the header of a JPEG file. So what you have is simply the whole content of a jpeg file encoded as hexadecimal. Reading that as an array of bytes and hoping it's somehow going to display as an image is never going to work.
The simplest method to decode the image is to resave the stream as a jpeg file and open that the normal way:
fid = fopen('hexdata.txt', 'r');
bindata = fscanf(fid, '%2x'); %read bytes encoded as hexadecimal character
fclose(fid);
fid = fopen('hexdata.jpg', 'w');
fwrite(fid, bindata); %save bytes to jpeg file
fclose(fid);
img = imread('hexdata.jpg');
imshow(img);
2 comentarios
Chandler Timm Doloriel
el 31 de Mzo. de 2022
@Guillaume Is there a way to convert this JPEG hex into RGB888 hex without using the fopen,fwrite?
Ver también
Categorías
Más información sobre Convert Image Type en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!