彩色图像的表示
RGB图像
f=imread ('Fig0604(a).tif');
fr=f(:,:,1);
fg=f(:,:,2);
fb=f(:,:,3);
figure,subplot(221),imshow(f),title("原图像");
subplot(222),imshow(fr,[]),title('R分量');
subplot(223),imshow(fg,[]),title('G分量');
subplot(224),imshow(fb,[]),title('B分量');
fact=cat(3,fr,fg,fb);
figure,imshow(fact),title('RGB合成彩色图像');
索引图像
[X,map]=imread('trees.tif');
figure,imshow(X,map)
figure,image(X)
colormap(map)%颜色映射
%映射行数少于X中不同数值数量
X=ones(256,256);
X(1:64,:)=1;
X(65:128,:)=64;
X(129:192,:)=128;
X(192:256,:)=256;
map=[0 0 0 ; 1 1 1];
figure,imshow(X,map)
%预定义彩色映射
X=ones(256,256);
X=uint8(X);
for i=1:256
X(i,:)=i;
end
figure,imshow(X,[])
colormap(copper(256))
title('copper');
%使用较少的颜色近似一幅索引图像
[Y,newmap]=imapprox(X,copper,8);
figure,imshow(Y,newmap),title('8中颜色近似');
彩色图像空间滤波
彩色图像平滑
f=imread('Fig0604(a).tif');
figure,subplot(231),imshow(f),title('原图像');
%分离RGB分量
fr=f(:,:,1);
fg=f(:,:,2);
fb=f(:,:,3);
w=fspecial('average',25);%创建平均滤波器
frf=imfilter(fr,w,'replicate');%对图像R分量进行滤波处理
fgf=imfilter(fg,w,'replicate');
fbf=imfilter(fb,w,'replicate');
ff1=cat(3,frf,fgf,fbf);%将滤波后的RGB分量图像重新组合为一个图像
subplot(232),imshow(ff1),title('RGB分量滤波');
f=imread('Fig0604(a).tif');
ff2=imfilter(f,w,'replicate');%对整个图像进行滤波处理
subplot(233),imshow(ff2),title('RGB整体滤波');
h=rgb2hsi(f);%将RGB图像转换为HSI 颜色空间
hf1=imfilter(h,w,'replicate');%对HSI颜色空间图像进行滤波处理
f1=hsi2rgb(hf1);%将滤波后的hsi图像转换为RGB图像
subplot(234),imshow(hf1),title('BSI 整体滤波');
%分离HSI图像的三个分量
H=h(:,:,1);
s=h(:,:,2);
I=h(:,:,3);
If=imfilter(I,w,'replicate');%对分量I进行滤波处理
hf2=cat(3,H,s,If);%将滤波后的三个HSI分量重新合成一个图像
f2=hsi2rgb(hf2);%将合成后的颜色空间图像转换为RGB图像
subplot(235),imshow(f2),title('仅对I分量滤波');
彩色图像锐化
fb=imread('Fig0604(a).tif');
figure,subplot(221),imshow(f),title('原图像');
lapmask=[1 1 1;1 -8 1 ;1 1 1];%定义拉普拉斯滤波器
fb=im2double(fb);%数据类型转换为双精度浮点数
fen=fb-imfilter(fb,lapmask,'replicate');%使用拉普拉斯滤波器对图像进行卷积操作,得到锐化后的图像
subplot(236),imshow(fen),title('锐化图像');
直接在RGB向量空间处理
边缘检测
f=imread('Fig0604(a).tif');
r=f(:,:,1);%分离图像的R分量
subplot(2,3,1),imshow(r),title('r');
g=f(:,:,2);分离图像的G分量
subplot(2,3,2),imshow(g),title('g');
b=f(:,:,3);
subplot(2,3,3);
imshow(b),title('b');
[VG,VA,PPG]=colorgrad(f);%计算图像的颜色梯度
subplot(2,3,4),imshow(f),title('RGB');
subplot(2,3,5),imshow(VG),title('VG');
subplot(2,3,6),imshow(PPG),title('PPG');
直接在RGB向量空间与合并各分量梯度结果的区别
R=imread('Fig0627(a).tif');
G=imread('Fig0627(b).tif');
B=imread('Fig0627(c).tif');
f=cat(3,R,G,B);
figure,subplot(231),imshow(R),title('R');
subplot(232),imshow(G),title('G');
subplot(233),imshow(B),title('B');
subplot(234),imshow(f),title('RGB彩色图像');
[VG,A,PPG]=colorgrad(f);
subplot(235),imshow(VG),title('RGB 向量空间直接计算');
subplot(236),imshow(PPG),title('分别计算RGB二梯度再合成');
图像分割
f=imread('Fig0630(a).tif');
mask=roipoly(f);%创建相关区域
R=immultiply(f(:,:,1),mask);%进行像素级乘法
G=immultiply(f(:,:,2),mask);
B=immultiply(f(:,:,3),mask);
g=cat(3,R,G,B);
figure,subplot(121),imshow(f),title('原图像');
subplot(122),imshow(g),title('RIO');
[M,N,K]=size(f);%获取图像大小
I=reshape(f,M*N,3);%将图像f重塑列向量
idx=find(mask);&找到索引
I=double(I(idx,1:3));
[C,m]=covmatrix(I);%计算提取像素的协方差矩阵
d=diag(C);%提取协方差矩阵C的对角线元素
sd=sqrt(d);%得到标准差
E025=colorseg('euclidean',f,25,m);%进行颜色分割
E050=colorseg('euclidean',f,50,m);
E075=colorseg('euclidean',f,75,m);
E0100=colorseg('euclidean',f,100,m);
figure,subplot(221),imshow(E025),title('欧式距离 E=25');
subplot(222),imshow(E050),title('欧式距离 E=50');
subplot(223),imshow(E075),title('欧式距离 E=75');
subplot(224),imshow(E0100),title('欧式距离 E=100');
EM25=colorseg('mahalanobis',f,25,m,C);
EM50=colorseg('mahalanobis',f,50,m,C);
EM70=colorseg('mahalanobis',f,70,m,C);
EM2100=colorseg('mahalanobis',f,100,m,C);
figure,subplot(221),imshow(EM25),title('马氏距离 E =25');
subplot(221),imshow(EM25),title('马氏距离 E =25');
subplot(222),imshow(EM50),title('马氏距离 E =50');
subplot(223),imshow(EM70),title('马氏距离 E =70');
subplot(224),imshow(EM2100),title('马氏距离 E =100');
注意:实验图片为彩色图片,你的本机matlab 软件可能缺少相关函数文件,相关文件已附载
为学习笔记,如有错误,请指教,谢谢