Making an RGB image
조회 수: 2 (최근 30일)
이전 댓글 표시
Hi all
I have a problem with making new RGB image. In the following code, at first, it reads an RGB image and stores it in 'A'. Then, it makes a new 3D matrix as same size as 'A' named 'A2'. Then, every component(R, G and B) of original image('A') is stored in equivalent component of 'A2'. But, when it shows 'A2', it is different from 'A'. May anybody help me?
clear all
clc
A=imread('Sun.jpg');
R=A(:,:,1);
G=A(:,:,2);
B=A(:,:,3);
A2=zeros(size(A,1),size(A,2),size(A,3));
A2(:,:,1)= R;
A2(:,:,2)= G;
A2(:,:,3)= B;
A2 = im2uint8(A2);
imwrite(A2,'Sun2.jpg');
imshow(A2)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/157490/image.jpeg)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/157492/image.jpeg)
댓글 수: 0
채택된 답변
Image Analyst
2016년 10월 29일
A2 is not the same class as A - it's double, not uint8 or uint16. See this corrected code.
A=imread('peppers.png');
subplot(2,2, 1);
imshow(A);
R=A(:,:,1);
G=A(:,:,2);
B=A(:,:,3);
A2=zeros(size(A), class(A));
A2(:,:,1)= R;
A2(:,:,2)= G;
A2(:,:,3)= B;
subplot(2,2, 2);
imshow(A2);
filename = fullfile(pwd, 'Delete Me.Png');
imwrite(A2, filename);
recalledA2 = imread(filename);
subplot(2,2, 3);
imshow(recalledA2);
delete(filename); % Delete temporary file.
댓글 수: 0
추가 답변 (2개)
Guillaume
2016년 10월 29일
I don't understand why you're going about it the way you're doing. It would be much simpler to do:
A2 = A;
and be done with it.
The problem you're seeing is one of image class. Your original image is of type uint8 (I assume). You then create A2 of type double since you didn't specify a class in the zeros call. You're then copying uint8 values into a double matrix. Matlab automatically convert these uint8 values to double without rescaling them. You them have a call to im2uint8 which, since the input is double, assumes the range is [0 1] and anything above 1 gets converted to 255. So in effect, all these original uint8 between 1 and 255, then converted to double between 1 and 255 are changed to 255 by im2uint8. You have in effect binarised all three colour planes.
As said, the simplest thing would be to just copy the matrix. Otherwise, specify the right class in zeros:
A2 = zeros(size(A), class(A));
(or leave A2 as double but divide the values by 255 after converting them to double when converting them).
댓글 수: 0
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!