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

10 vues (au cours des 30 derniers jours)
Tham  Wei Jian
Tham Wei Jian le 15 Août 2019
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 commentaires
Tham  Wei Jian
Tham Wei Jian le 15 Août 2019
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 le 15 Août 2019
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?

Connectez-vous pour commenter.

Réponses (1)

Athul Prakash
Athul Prakash le 19 Août 2019
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);

Catégories

En savoir plus sur Convert Image Type dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by