How to convert 1 column vector text file to rgb image?
3 ビュー (過去 30 日間)
古いコメントを表示
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
3 件のコメント
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
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);
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Convert Image Type についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!