为什么我在matlab中使用这个短代码来获取蓝色,绿色或红色通道?

use*_*426 2 matlab image image-processing

rgb = imread('peppers.png');
imshow(rgb(:,:,1));
Run Code Online (Sandbox Code Playgroud)

当我把它放在图像是灰色,而不是绿色或蓝色或红色.这是为什么?
难道我做错了什么?

Nom*_*Sim 6

图像是灰色的,因为您只查看一种颜色的值,MATLAB会看到颜色的值,但是它无法知道它是什么颜色,这就是显示灰色的原因.

举个例子,154的值是什么颜色的?当您只传递一个值矩阵时,imshow它将以灰度显示.

imshow(rgb(:,:,1)); %Shows the values of the red component of the image in grey

rgb = imread('peppers.png');
r = rgb;
r(:,:,2:3) = 0; % The red component without the other components
g = rgb;
g(:,:,1:2:3) = 0; % The green component without the other components
b = rgb;
b(:,:,1:2) = 0; % The blue component without the other components
figure();
imshow(r);
figure();
imshow(g);
figure();
imshow(b);
Run Code Online (Sandbox Code Playgroud)

通过执行上述操作,您可以在自己的颜色值中查看颜色大小的表示.


Gun*_*uyf 5

如果只想显示一个颜色通道,请将其他颜色通道设置为零:

peppers = imread('peppers.png');
onlyred_peppers = peppers;
onlyred_peppers(:,:,2:3)=0;
imshow(onlyred_peppers);
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

如果你imshow只是喂食peppers(:,:,1),你只给它一个NxMx1矩阵,由imshow解释为灰度,见这里.

如果您真的想要,可以更改色彩映射以将灰度图像更改为红色:

imshow(peppers(:,:,1));
cm_red = [linspace(0,1,256)' zeros(256,2)];
colormap(cm_red);
Run Code Online (Sandbox Code Playgroud)

这将给你与上面相同的图像.

如果您还希望缩放加载的图像中的颜色范围以使用完整的可用范围(0-1/0-255),则可以使用以下命令:

red_scaled_peppers = peppers(:,:,1);
red_scaled_peppers = double(red_scaled_peppers)/double(max(red_scaled_peppers(:)));
imshow(red_scaled_peppers);
colormap([linspace(0,1,256)' zeros(256,2)]);
Run Code Online (Sandbox Code Playgroud)

没有区别,因为在这种情况下,红色通道颜色跨度已经超出.