matlab图像处理基础实例.docx
- 文档编号:9277166
- 上传时间:2023-02-04
- 格式:DOCX
- 页数:56
- 大小:2.61MB
matlab图像处理基础实例.docx
《matlab图像处理基础实例.docx》由会员分享,可在线阅读,更多相关《matlab图像处理基础实例.docx(56页珍藏版)》请在冰豆网上搜索。
matlab图像处理基础实例
·边缘检测(edge)
边缘检测时先要把其他格式图像转化为灰度图像
>>f=imread('');
>>a=rgb2gray(f);
>>[g,t]=edge(a,'canny');
>>imshow(g)
·剪贴(imcrop)、subplot等
imfinfocolormapsubimage
imaddimsubtractimmultiplyimdivide
imresizeimrotate(旋转)
>>a=imread('');
>>b=imcrop(a,[7568130112]);
%I2=IMCROP(I,RECT)
%RECTisa4-elementvectorwiththeform[XMINYMINWIDTHHEIGHT];
%subplot(121)一行两列的显示,当前显示第一个图片
>>subplot(121);imshow(a);
>>subplot(122);imshow(b);
·roipoly选择图像中的多边形区域
>>a=imread('');
>>c=[200250278248199172];
>>r=[21217512112175];
>>b=roipoly(a,c,r);
>>subplot(121);imshow(a);
>>subplot(122);imshow(b);
·roicolor按灰度值选择的区域
>>a=imread('');
>>i=rgb2gray(a);
>>b=roicolor(i,128,255);
>>subplot(121);imshow(a);
>>subplot(122);imshow(b);
·转化指定的多边形区域为二值掩膜
poly2mask
>>x=[631865419063];
>>y=[606020920460];
>>b=poly2mask(x,y,256,256);
>>imshow(b);
>>hold
Currentplotheld
>>plot(x,y,'b','LineWidth',2)
·roifilt2区域滤波
a=imread('');
i=rgb2gray(a);
c=[200250278248199172];
r=[21217512112175];
b=roipoly(i,c,r);
h=fspecial('unsharp');
j=roifilt2(h,i,b);
subplot(121),imshow(i);
subplot(122),imshow(j);
·roifill区域填充
>>a=imread('');
>>i=rgb2gray(a);
>>c=[200250278248199172];
>>r=[21217512112175];
>>j=roifill(i,c,r);
>>subplot(211);imshow(i);
>>subplot(212);imshow(j);
·FFT变换
f=zeros(100,100);
f(20:
70,40:
60)=1;
imshow(f);
F=fft2(f);
F2=log(abs(F));
imshow(F2),colorbar
·补零操作和改变图像的显示象限
f=zeros(100,100);
f(20:
70,40:
60)=1;
subplot(121);imshow(f);
F=fft2(f,256,256);
F2=fftshift(F);subplot(122);
imshow(log(abs(F2)))
·离散余弦变换(dct)
>>a=imread('');
>>i=rgb2gray(a);
>>j=dct2(i);
>>subplot(131);imshow(log(abs(j))),colorbar
>>j(abs(j)<10)=0;
>>k=idct2(j);
>>subplot(132);imshow(i);
>>subplot(133);imshow(k,[0,255]);
info=imfinfo('')%显示图像信息
·edge提取图像的边缘
cannyprewittsobel
radon函数用来计算指定方向上图像矩阵的投影
>>a=imread('');
>>i=rgb2gray(a);
>>b=edge(i);
>>theta=0:
179;
>>[r,xp]=radon(b,theta);
>>figure,imagesc(theta,xp,r);colormap(hot);
>>xlabel('\theta(degrees)');
>>ylabel('x\prime');
>>title('r_{\theta}(x\prime)');
>>colorbar
·filter2均值滤波
>>a=imread('');
>>i=rgb2gray(a);
>>imshow(i)
>>k1=filter2(fspecial('average',3),i)/255;%3*3
>>k2=filter2(fspecial('average',5),i)/255;%5*5
>>k3=filter2(fspecial('average',7),i)/255;%7*7
>>figure,imshow(k1)
>>figure,imshow(k2)
>>figure,imshow(k3)
wiener2滤波
eg:
k=wiener(I,[3,3]))
medfilt2中值滤波
同上
deconvwnr维纳滤波
马赫带效应(同等差色带条)
·减采样
>>a=imread('');
>>b=rgb2gray(a);
>>[wid,hei]=size(b);
>>quarting=zeros(wid/2+1,hei/2+1);
>>i1=1;j1=1;
>>fori=1:
2:
wid
forj=1:
2:
hei
quarting(i1,j1)=b(i,j);
j1=j1+1;
end
i1=i1+1;
j1=1;
end
>>figure
>>imshow(uint8(quarting))
>>title('4倍减采样')
>>quarting=zeros(wid/4+1,hei/4+1);
i1=1;j1=1;
fori=1:
4:
wid
forj=1:
4:
hei
quarting(i1,j1)=b(i,j);
j1=j1+1;
end
i1=i1+1;
j1=1;
end
>>figure,imshow(uint8(quarting));title('16倍减采样')
结论:
在采用不同的减采样过程中,其图像的清晰度和尺寸均发生了变化
灰度级转化
>>a=imread('');
>>b=rgb2gray(a);
>>figure;imshow(b)
>>[wid,hei]=size(b);
>>img2=zeros(wid,hei);
>>fori=1:
wid
forj=1:
hei
img2(i,j)=floor(b(i,j)/128);
end
end
>>figure;imshow(uint8(img2),[0,2])%2级灰度图像
图像的基本运算
>>i=imread('');
>>figure;subplot(231);imshow(i);
>>title('原图');
>>j=imadjust(i,[.3;.6],[.1.9]);
%Adjustimageintensityvaluesorcolormap图像灰度值或colormap调整
%J=IMADJUST(I,[LOW_IN;HIGH_IN],[LOW_OUT;HIGH_OUT])
>>subplot(232);imshow(j);title('线性扩展');
>>i1=double(i);i2=i1/255;c=2;k=c*log(1+i2);
>>subplot(233);imshow(k);
>>title('非线性扩展');
>>m=255-i;
>>subplot(234);imshow(m)
>>title('灰度倒置')
>>n1=im2bw(i,.4);n2=im2bw(i,.7);
>>subplot(235);imshow(n1);title('二值化阈值')
>>subplot(236);imshow(n2);title('二值化阈值')
图像的代数运算
加。
减,乘法(获取感兴趣的区域)
imresize放大
a=imresize(I,2)%比例
a=imresize(I,[3324])%非比例
imrotate旋转
a=imrotate(I,45)
时域旋转多少度,频域也就旋转多少度
>>i=zeros(256,256);
>>i(88:
168,124:
132)=1;
>>imshow(i)
>>j=fft2(i);f=abs(j);j1=fftshift(f);figure;imshow(j1,[550])
>>j=imrotate(i,90,'bilinear','crop');
>>figure;imshow(j);
>>j1=fft2(j);f=abs(j1);j2=fftshift(f);
>>figure;imshow(j2,[550])
边缘检测
>>a=imread('');
>>subplot(211);imshow(a);title('pri')
>>b=edge(a,'canny');subplot(212);imshow(b);title('by')
>>figure,c=edge(a,'prewitt');imshow(c)
腐蚀和膨胀
>>a=imread('');
>>subplot(231);imshow(a);
>>title('原灰度图像')
>>t=graythresh(a);
>>bw1=im2bw(a,t);
>>se1=strel('square',3);se2=strel('square',5);
>>bw2=imerode(bw1,se1);
>>subplot(232);imshow(bw2)
>>title('3*3腐蚀')
>>bw3=imdilate(bw1,se1);subplot(233);imshow(bw3);title('3*3膨胀')
>>bw4=imerode(bw1,se2);subplot(234);imshow(bw4);title('5*5腐蚀')
>>bw5=imdilate(bw1,se2);subplot(235);imshow(bw5);title('5*5膨胀')
log算子
>>x=-2:
.06:
2;
>>y=-2:
.06:
2;
>>sigma=.6;y=y';
>>fori=1:
(4/.06+1)
xx(i,:
)=x;
yy(:
i)=y;
end
>>r=1/(2*pi*sigma^4)*((xx.^2+yy.^2)/(sigma^2)-2).*exp(-(xx.^2+yy.^2)/(sigma^2));
>>colormap(jet(16));mesh(xx,yy,r)
分水岭算法分割图像
>>f=imread('');
>>f=rgb2gray(f);subplot(221);imshow(f)
>>title('pri')
>>subplot(222);f=double(f);hv=fspecial('prewitt');hh=hv.';
>>gv=abs(imfilter(f,hv,'replicate'));
>>gh=abs(imfilter(f,hh,'replicate'));
>>g=sqrt(gv.^2+gh.^2);subplot(222);l=watershed(g);wr=l==0;
>>imshow(wr);title('分水岭')
>>f(wr)=255;subplot(223);imshow(uint8(f));title('(c)分割结果')
>>rm=imregionalmin(g);subplot(224);imshow(rm);title('(d)局部极小值')
区域生长法
彩色基础
>>rgb_r=zeros(512,512);
>>rgb_r(1:
256,257:
512)=1;
>>rgb_g=zeros(512,512);
>>rgb_g(1:
256,1:
256)=1;
>>rgb_g(257:
512,257:
512)=1;
>>rgb_b=zeros(512,512);
>>rgb_b(257:
512,1:
256)=1;
>>rgb=cat(3,rgb_r,rgb_g,rgb_b);
>>figure,imshow(rgb),title('rgb彩色图像')
将上题转化到HIS空间
模糊H分量,得到
rgb=imread('');
rgb1=im2double(rgb);
r=rgb1(:
:
1);
g=rgb1(:
:
2);
b=rgb1(:
:
3);
i=(r+g+b)/3;
tmp1=min(min(r,g),b);
tmp2=r+g+b;tmp2(tmp2==0)=eps;
s=1-3.*tmp1./tmp2;
tmp1=.5*((r-g)+(r-b));
tmp2=sqrt((r-g).^2+(r-b).*(g-b));
theta=acos(tmp1./(tmp2+eps));
h=theta;
h(b>g)=2*pi-h(b>g);
h=h/(2*pi);
h(s==0)=0;
subplot(231);imshow(rgb);title('image')
subplot(232);imshow(h),title('h')
subplot(233);imshow(s),title('s')
subplot(234);imshow(i),title('i')
h1=filter2(fspecial('average',25),h)/255;
s1=filter2(fspecial('average',25),s)/255;
subplot(235);imshow(mat2gray(h1)),title('h模糊结果')
hsi=cat(3,h1,s,i);
h=hsi(:
:
1)*2*pi;
s=hsi(:
:
2);
i=hsi(:
:
3);
R=zeros(size(hsi,1),size(hsi,2));
G=zeros(size(hsi,1),size(hsi,2));
B=zeros(size(hsi,1),size(hsi,2));
ind=find((h>=0)&(h<2*pi/3));
B(ind)=i(ind).*(ind));
R(ind)=i(ind).*+s(ind).*cos(h(ind))./cos(pi/(ind)));
G(ind)=(R(ind)+B(ind));
ind=find((h>2*pi/3)&(h<4*pi/3));
h(ind)=h(ind)-pi*2/3;
r(ind)=i(ind).*(ind));
g(ind)=i(ind).*+s(ind).*cos(h(ind))./cos(pi/(ind)));
b(ind)=(R(ind)+G(ind));
ind=find((h>4*pi/3)&(h<2*pi));
h(ind)=h(ind)-pi*4/3;
g(ind)=i(ind).*(ind));
b(ind)=i(ind).*+s(ind).*cos(h(ind))./cos(pi/(ind)));
r(ind)=(G(ind)+B(ind));
RGB=cat(3,R,G,B);
subplot(236);imshow(RGB);title('处理后的图像')
结论:
图像变柔和
(2)模糊S分量
将hsi=cat(3,h1,s,i);给为hsi=cat(3,h,s1,i);
结论:
图像饱和度降低
边缘
clear
rgb_r=zeros(128,128);
rgb_r(1:
64,1:
64)=1;
rgb_g=zeros(128,128);
rgb_g(1:
64,65:
128)=1;
rgb_b=zeros(128,128);
rgb_b(65:
128,1:
64)=1;
rgb=cat(3,rgb_r,rgb_g,rgb_b);
sob=fspecial('sobel');
rx=imfilter(double(rgb(:
:
1)),sob,'replicate');
ry=imfilter(double(rgb(:
:
1)),sob,'replicate');
gx=imfilter(double(rgb(:
:
2)),sob,'replicate');
gy=imfilter(double(rgb(:
:
2)),sob,'replicate');
bx=imfilter(double(rgb(:
:
3)),sob,'replicate');
by=imfilter(double(rgb(:
:
3)),sob,'replicate');
r_gradiant=mat2gray(max(rx,ry));
g_gradiant=mat2gray(max(gx,gy));
b_gradiant=mat2gray(max(bx,by));
rgb_gradiant=rgb2gray(cat(3,r_gradiant,g_gradiant,b_gradiant));
gxx=rx.^2+gx.^2+bx.^2;
gyy=ry.^2+gy.^2+by.^2;
gxy=rx.*ry+gx.*gy+bx.*by;
theta=.5*((atan(2*gxy./(gxx-gyy+eps))));
g1=.5*((gxx+gyy)+(gxx-gyy).*cos(2*theta)+2*gxy.*sin(2*theta));
theta=theta+pi/2;
g2=.5*((gxx+gyy)+(gxx-gyy).*cos(2*theta)+2*gxy.*sin(2*theta));
g1=g1.^5;
g2=g2.^5;
rgb_vectorgradiant=mat2gray(max(g1,g2));
diff=abs(rgb_vectorgradiant-rgb_gradiant);
subplot(331);imshow(rgb);title('彩色原图')
subplot(332);imshow(r_gradiant);title('R分量边缘')
subplot(333);imshow(g_gradiant);title('G分量边缘')
subplot(334);imshow(b_gradiant);title('B分量边缘')
subplot(335);imshow(rgb_gradiant);title('分量合成边缘')
subplot(336);imshow(rgb_vectorgradiant);title('向量梯度边缘')
subplot(337);imshow(diff);title('差别')
平滑再合成
锐化
rgb1=imread('');
rgb=im2double(rgb1);
fr=rgb(:
:
1);
fg=rgb(:
:
2);fb=rgb(:
:
3);
lapmatrix=[111;1-81;111];
fr_filtered=imfilter(fr,lapmatrix,'replicate');
fg_filtered=imfilter(fg,lapmatrix,'replicate');
fb_filtered=imfilter(fb,lapmatrix,'replicate');
rgb_tmp=cat(3,fr_filtered,fg_filtered,fb_filtered);
rgb_filtered=imsubtract(rgb,rgb_tmp);
i1=(fr+fg+fb)/3;
tmp1=min(min(fr,fg),fb);
tmp2=fr+fg+fb;
tmp2(tmp2==0)=eps;%
s=1-3.*tmp1./tmp2;
tmp1=.5*((fr-fg)+(fr-fb));
tmp2=sqrt((fr-fg).^2+(fr-fb).*(fg-fb));
theta=acos(tmp1./(tmp2+eps));
h1=theta;
h1(fb>fg)=2*pi-h1(fb>fg);
h1=h1/(2*pi);
h1(s==0)=0;
lapmatrix=[111;1-81;111];
i=imfilter(i1,lapmatrix,'replicate')
i=imfilter(i1,lapmatrix,'replicate');
subplot(331);imshow(rgb1);title('pri')
subplot(332);imshow(fr_flitered);title('fr_flitered')
subplot(333);imshow(fg_filtered);title('fg_filtered')
subplot(334);imshow(fb_filtered);title('fb_filtered')
subplot(335);imshow(h1);title('h1')
subplot(336);imshow(s);title('s')
subplot(337);imshow(i1);title('i1')
subplot(338);imshow(rgb_tmp);title('mixture')
subplot(339);imshow(cat(3,h1,s,i1));title('hsimixture')
彩色图像向量梯度的计算
>>rgb=imread('');
>>sob=fspecial('sobel');
>>rx=imfilter(double(rgb(:
:
1)),sob,'replicate');
>>ry=imfilter(double(rgb(:
:
1)),sob','replicate');
>>gx=imfilter(double(rgb(:
:
2)),sob,'replicate');
gy=
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- matlab 图像 处理 基础 实例
![提示](https://static.bdocx.com/images/bang_tan.gif)