MATLAB数字图像处理提取颜色分量

clear;
close all;
clc;
img=imread('D:\pictures\lena1.jfif');%读入原彩色图像
imshow(img);%展现图像
%对红色分量提取
img_r=img(:,:,1);
img_r(:,:,2)=0;
img_r(:,:,3)=0;
figure,imshow(img_r);
%对绿色分量提取
img_g=img(:,:,2);
img_r(:,:,1)=0;
img_r(:,:,3)=0;
figure,imshow(img_g);
%对蓝色分量提取
img_b=img(:,:,3);
img_r(:,:,2)=0;
img_r(:,:,1)=0;
figure,imshow(img_b);

提问:为什么只有取第一个维度运行出来是鲜明红色,取其他维度就没有鲜明的颜色?
解答:粗心问题,语句*img_g=img(:,:,2);
img_r(:,:,1)=0;
img_r(:,:,3)=0;*中的img_r应该改为img_g

更改后的代码:

clear;
close all;
clc;
img=imread('D:\pictures\lena1.jfif');%读入原彩色图像
imshow(img);%展现图像
%对红色分量提取
% img_r=img(:,:,1);%下面两条语句是对此语句的改进
% img_r=uint8(zeros(512,512,3));%
% img_r(:,:,1)=img(:,:,1);%
img_r=img(:,:,1);
img_r(:,:,2)=0;
img_r(:,:,3)=0;
figure,imshow(img_r);
%对绿色分量提取
img_g=img(:,:,2);
img_g(:,:,1)=0;
img_g(:,:,3)=0;
figure,imshow(img_g);
%对蓝色分量提取
img_b=img(:,:,3);
img_b(:,:,2)=0;
img_b(:,:,1)=0;
figure,imshow(img_b);

但是出现报错:
错误使用 images.internal.imageDisplayValidateParams>validateCData (line 115)
多平面图像的输入项必须是大小为 MxNx3 的 RGB 图像。

出错 images.internal.imageDisplayValidateParams (line 27)
common_args.CData = validateCData(common_args.CData,image_type);

出错 images.internal.imageDisplayParseInputs (line 78)
common_args = images.internal.imageDisplayValidateParams(common_args);

出错 imshow (line 222)
images.internal.imageDisplayParseInputs({‘Parent’,‘Border’,‘Reduce’},preparsed_varargin{:});

出错 book32801 (line 38)
figure,imshow(img_b);


错误原因:* img_r=uint8(zeros(512,512,3));:原图片是256256的,而语句写的是512*512,导致下标匹配不起来

再次更改,正确代码:

clear;
close all;
clc;
img=imread('D:\pictures\lena1.jfif');%读入原彩色图像
subplot(1,4,1),imshow(img);%展现图像
%对红色分量提取
% img_r=img(:,:,1);%下面两条语句是对此语句的改进
img_r=uint8(zeros(256,256,3));%
img_r(:,:,1)=img(:,:,1);%
img_r(:,:,2)=0;
img_r(:,:,3)=0;
subplot(1,4,2),imshow(img_r);
%对绿色分量提取
%img_g=img(:,:,2);%同上
img_g=uint8(zeros(256,256,3));
img_g(:,:,2)=img(:,:,2);
img_g(:,:,1)=0;
img_g(:,:,3)=0;
subplot(1,4,3),imshow(img_g);
%对蓝色分量提取
%img_b=img(:,:,3);%同上
img_b=uint8(zeros(256,256,3));
img_b(:,:,3)=img(:,:,3);
img_b(:,:,2)=0;
img_b(:,:,1)=0;
subplot(1,4,4),imshow(img_b);

运行结果:
MATLAB数字图像处理提取颜色分量_第1张图片

你可能感兴趣的:(数字图像处理)