How to convert 1 column vector text file to rgb image?

조회 수: 3 (최근 30일)
Tham  Wei Jian
Tham Wei Jian 2019년 8월 15일
답변: Athul Prakash 2019년 8월 19일
I have a text file(.txt), how can I convert the 1 column vector (1x150528) to image 224x224x3 as shown below??
can show me the coding ?? thanks for helping me T.T
input.bmp
  댓글 수: 3
Tham  Wei Jian
Tham Wei Jian 2019년 8월 15일
Here is my coding and output. Do you have method help me remove the line inside the image?This very annoying...
Please help me edit my coding...
============================================================
fid = fopen('input_data.txt','r');
img = fscanf(fid,'%x');
fclose(fid);
outImg = reshape(img,[224*3 224*1]);
outImg = outImg';
imshow(outImg,[])
=========================================================
error.png
Geoff Hayes
Geoff Hayes 2019년 8월 15일
Tham - since your image is 224x244x3, then why are you doing
outImg = reshape(img,[224*3 224*1]);
since this will create a 672x224 (grayscale) image? Why not try what Adam suggested with
outImg = reshape(img, [224 224 3]);
Also, why the %x
img = fscanf(fid,'%x');
and not %d?

댓글을 달려면 로그인하십시오.

답변 (1개)

Athul Prakash
Athul Prakash 2019년 8월 19일
Hi Tham,
As mentioned here already, your shape of image should be 224x224x3, and you should be using a 3 dimensional array.
Why Lines are there: When it's being reshaped to 672x224 instead, all 3 color channels are forced into the same dimension: The RGB values for each pixel now appear as different pixels along the same column. Thus, the image would have horizontal stripes, each stripe being the value of a different color channel. After tansposing, the lines become vertical lines ofcourse.
To show the color image:
Load using %d instead of %x.
fid = fopen('input_data.txt','r');
img = fscanf(fid,'%d');
fclose(fid);
Reshape into 3x224x224. The order in which the data was stored is important here. In the file given, it seems that channels are stored along the first dimension.
outputImg = reshape(img, [3 224 224]);
To display the image, we need to make sure that color channels are present along the last dimension, i.e. 224x224x3. Also, we need to transpose the image as well. We can reorder the dimensions of the image using permute()
outputImg = permute(outputImg, [3 2 1]);
See this documentation:
Convert the image to type uint8 (very important) and call imshow()
outputImg = uint8(outputImg);
imshow(outputImg);

카테고리

Help CenterFile Exchange에서 Convert Image Type에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by