在Matlab中显示Y Cb和Cr组分

Displaying Y Cb and Cr components in Matlab

我有一个尺寸为640 * 480的RGB图像。 我已通过使用Matlab命令Rgbtoycbcr将其转换为Y Cb Cr图像,但我想分别显示Y Cb Cr分量,如图所示。 我该怎么做?


这是Benoit_11代码的扩展。 该代码不是将每个通道的值显示为灰度图像,而是以50%的恒定值填充其他通道。 这样,每个通道对最终图像的影响就更容易观察到。 特别是Y通道的重要性是显而易见的。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
RGB = imread('board.tif');
YCBCR = rgb2ycbcr(RGB);

figure;

lb={'Y','Cb','Cr'};

for channel=1:3
subplot(1,3,channel)
YCBCR_C=YCBCR;
YCBCR_C(:,:,setdiff(1:3,channel))=intmax(class(YCBCR_C))/2;
imshow(ycbcr2rgb(YCBCR_C))
title([lb{channel} ' component'],'FontSize',18);
end

enter image description here


您可以显示每个组件,就像对RGB图像的第3维索引的红色,绿色或蓝色通道一样。

简单的例子:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
RGB = imread('board.tif');
YCBCR = rgb2ycbcr(RGB);

figure;

subplot(1,3,1)
imshow(YCBCR(:,:,1))
title('Y component','FontSize',18);

subplot(1,3,2)
imshow(YCBCR(:,:,2))
title('Cb component','FontSize',18);

subplot(1,3,3)
imshow(YCBCR(:,:,3))
title('Cr component','FontSize',18);

输出:

enter image description here


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
rgbImage = imread('C:\\Users\\Public\\Pictures\\Sample Pictures\\Desert.jpg'); %# A sample RGB image
A = [65.481 -37.797 112; ...       %# A 3-by-3 matrix of scale factors
 128.553 -74.203 -93.786; ...
 24.966 112 -18.214];
%# First convert the RGB image to double precision, scale its values to the
%#   range 0 to 1, reshape it to an N-by-3 matrix, and multiply by A:
ycbcrImage = reshape(double(rgbImage)./255,[],3)*A;

%# Shift each color plane (stored in each column of the N-by-3 matrix):
ycbcrImage(:,1) = ycbcrImage(:,1)+16;
ycbcrImage(:,2) = ycbcrImage(:,2)+128;
ycbcrImage(:,3) = ycbcrImage(:,3)+128;

%# Convert back to type uint8 and reshape to its original size:
ycbcrImage = reshape(uint8(ycbcrImage),size(rgbImage));
imshow(ycbcrImage);
ycbcry = ycbcrImage;
ycbcry(:,:,2) = 2;
ycbcry(:,:,3) = 2;
rgb1 = ycbcr2rgb(ycbcry);
figure, imshow(rgb1);
% display the cb component as a color image
ycbcru = ycbcrImage;
ycbcru(:,:,1) = 4;
ycbcru(:,:,3) = 4;
rgb2 = ycbcr2rgb(ycbcru);
figure, imshow(rgb2);
% display the cr component as a color image
ycbcrv = ycbcrImage;
ycbcrv(:,:,1) = 8;
ycbcrv(:,:,2) = 8;
rgb3 = ycbcr2rgb(ycbcrv);
figure, imshow(rgb3);