필터 지우기
필터 지우기

How to show an image from a textfile?

조회 수: 12 (최근 30일)
Ali Purse
Ali Purse 2018년 10월 18일
답변: Guillaume 2018년 10월 18일
I have turned the image using dlmwrite function to a text file.
No I want to turn the textfile to image again, but it doesn't work correctly.
I'm using the following code:
Code to turn an image to text file:
a= imread('ut.jpg');
dlmwrite ( 'FileName23.txt', a);
Code to turn a text file to an image:
a = dlmread('FileName23.txt');
imshow(a);

채택된 답변

Guillaume
Guillaume 2018년 10월 18일
jpg images are typically colour images. Colour images are stored as 3D arrays in matlab. dlmread and dlmwrite can only read 2D arrays so your serialisation to text loses that third dimension. All three pages are concatenated horizontally. In addition, jpg images are typically of type uint8, these values get cast to double without rescaling when you serialise them.
You can get back your original array by reshaping it back to 3D and casting it back to uint8:
readimage = dlmread('FileName23.txt');
readimage = uint8(reshape(readimage, size(readimage, 1), [], 3));
imshow(readimage);
Note that this applies to 8-bit colour jpg, which is the majority of jpg files. If the original jpg was 16-bit then you'd have to cast to uint16. If the original jpg was greyscale, you don't need the reshape.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Import, Export, and Conversion에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by