matrix to rgb conversion

12 ビュー (過去 30 日間)
Mohammad Golam Kibria
Mohammad Golam Kibria 2011 年 3 月 28 日
When we write
i=imread('test.jpg');
if test.jpg is a colored image then it returns us i as a 3D matrix.
if we write
figure,imshow(i)
it shows the colored image test.jpg
but if we assign the values in i to another variable using the following syntax:
a=i(:,:,1); a=i(:,:,2); a=i(:,:,3);
then if i write
figure,imshow(a)
it does not return me the colored image as test.jpg
can any one say why this happen? How can i get the make a rgb image if i assign value to a 3D matrix manually how can i get the corresponding colored rgb image?

採用された回答

Matt Fig
Matt Fig 2011 年 3 月 29 日
The reason why you are having the problem is that when you call the ZEROS function, you are getting an array of doubles, whereas a jpg image is of type uint8.
I = imread('test.jpg');
figure
imshow(I)
a = zeros(size(I),'uint8'); % Or, a = zeros(size(I),class(I));
a(:,:,1) = I(:,:,1);
a(:,:,2) = I(:,:,2);
a(:,:,3) = I(:,:,3);
figure
imshow(a)
Note that the above is a horrible way to copy any data from one array to another. Simply use:
a = I; % Don't use variable name i!
  1 件のコメント
Mohammad Golam Kibria
Mohammad Golam Kibria 2011 年 3 月 29 日
Yes. That works fine.
Thanks

サインインしてコメントする。

その他の回答 (1 件)

Jan
Jan 2011 年 3 月 28 日
i = imread('test.jpg');
a = i(:,:,1);
% Now [a] is a 2D-Matrix containing the first
% submatrix of i.
a = i(:,:,2);
% Now [a] is a 2D-Matrix containing the second
% Submatrix of i. The former values are overwritten.
a = i(:,:,3);
% Now [a] is a 2D-Matrix containing the third
% Submatrix of i. The former values are overwritten.
Therefore [a] is a matrix at the end. To copy the 3D-Array:
a = cat(3, i(:,:,1), i(:,:,2), i(:,:,3));
% Or simpler:
a = i;
  1 件のコメント
Mohammad Golam Kibria
Mohammad Golam Kibria 2011 年 3 月 29 日
sorry, your answers to my code is right.But I have made a little mistake in the code. please see the following:
i = imread('test.jpg');
figure,imshow(i)
a=zeros(size(i));
a(:,:,1) = i(:,:,1);
a(:,:,2) = i(:,:,2);
a(:,:,3) = i(:,:,3);
figure,imshow(a)
c = cat(3, a(:,:,1), a(:,:,2), a(:,:,3));
figure,imshow(c)
The problem is that both a and c are not colored image as i found in i.
would you please explain me why this is happening.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeImages についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by