【MATLAB100个实用小技巧】——图形处理(67-75)

文章目录

  • 前言
  • 系列文章
  • 67. 图像的块操作
  • 68. 图形的过滤操作
  • 69. 图像的频率操作
  • 70. 函数变换
  • 71. RADON 函数变换
  • 72. 图像分析(1)
  • 73. 过滤图像
  • 74. 图像的区域处理
  • 75. 图像的颜色处置

前言

MATLAB是一个功能强大的软件,我们可以利用MATLAB进行绘图、算法验证、仿真实验等等。在学习MATLAB的过程中,繁多的命令与代码往往容易使我们脑容量过载请添加图片描述
本系列将总结一些常见的MATLAB编程小技巧
可能有些地方会有些错误或者是不太完善的,还请大家在评论区直接指出❤️❤️❤️

系列文章

【MATLAB100个实用小技巧】——图形应用(1-10)
【MATLAB100个实用小技巧】——图形应用(11-20)
【MATLAB100个实用小技巧】——图形应用(21-32)
【MATLAB100个实用小技巧】——界面设计(33-43)
【MATLAB100个实用小技巧】——界面设计(44-55)
【MATLAB100个实用小技巧】——界面设计(56-66)
【MATLAB100个实用小技巧】——图形处理(67-75)
【MATLAB100个实用小技巧】——图形处理(76-84)
【MATLAB100个实用小技巧】——数值分析(85-100)

67. 图像的块操作

代码

h0=figure('toolbar','none',...
'position',[198 56 350 468],...
'name','实例 67');
h1=axes('parent',h0,... 
    'position',[0.2 0.45 0.6 0.5],...
'visible','off'); 
I=imread('tire.tif'); 
imshow(I);
b1=uicontrol('parent',h0,...
'units','points',...
'tag','b1',...
'backgroundcolor',[0.75 0.75 0.75],...
'style','pushbutton',...
'string','边沿操作',...
'position',[30 100 50 20],...
'callback',[...
'cla,',... 
'I=imread(''tire.tif'');,',...
'f=inline(''max(x(:))'');,',...
'I2=nlfilter(I,[2 2],f);,',... 
'imshow(I2)']);
b2=uicontrol('parent',h0,... 
    'units','points',...
'tag','b2',...
'backgroundcolor',[0.75 0.75 0.75],...
'style','pushbutton',...
'string','显示块操作',...
'position',[100 100 50 20],...
'callback',[...
'cla,',... 
'I=imread(''tire.tif'');,',...
'f=inline(''uint8(round(mean2(x)*ones(size(x))))'');,',... 
'I2=blkproc(I,[6 6],f);,',...
'imshow(I2)']);
b3=uicontrol('parent',h0,...
'units','points',...
'tag','b3',...
'backgroundcolor',[0.75 0.75 0.75],...
'style','pushbutton',...
'string','交叠块操作',...
'position',[170 100 50 20],...
'callback',[...
'cla,',...
'I=imread(''tire.tif'');,',...
'f=inline(''uint8(round(mean2(x)*ones(size(x))))'');,',...
'I2=blkproc(I,[6 6],[3 3],f);,',...
'imshow(I2)']);
b4=uicontrol('parent',h0,...
'units','points',...
'tag','b4',...
'backgroundcolor',[0.75 0.75 0.75],...
'style','pushbutton',...
'string','关闭',...
'fontsize',14,...
'position',[90 50 70 30],...
'callback','close');

效果
【MATLAB100个实用小技巧】——图形处理(67-75)_第1张图片

68. 图形的过滤操作

这个可能还有些问题…
代码

h0=figure('toolbar','none',...
'position',[198 56 350 468],...
'name','过滤操作');
h1=axes('parent',h0,... 
    'position',[0.3 0.45 0.5 0.5],...
'visible','off'); 
I=imread("D:\opencv\JPEGdata\IMG_20210603_185840.jpg"'); 
imshow(I);
b1=uicontrol('parent',h0,...
'units','points',...
'tag','b1',...
'style','pushbutton',...
'backgroundcolor',[0.75 0.75 0.75],...
'string','均平过滤',...
'position',[50 120 50 20],...
'callback',[...
'cla,',... 
'I=imread("D:\opencv\JPEGdata\IMG_20210603_185840.jpg");,',...
'h=fspecial(''average'',6);,',...
'I2=uint8(round(filter2(h,I)));,',...
'imshow(I2)']);
b2=uicontrol('parent',h0,...
    'units','points',...
'tag','b2',...
'style','pushbutton',...
'backgroundcolor',[0.75 0.75 0.75],...
'string','Sobel 过滤',...
'position',[150 120 50 20],...
'callback',[...
'cla,',...
'I=imread("D:\opencv\JPEGdata\IMG_20210603_185840.jpg");,',...
'h=fspecial(''sobel'');,',...
'I2=filter2(h,I);,',...
'imshow(I2,[])']);
b1=uicontrol('parent',h0,...
'units','points',...
'tag','b1',...
'style','pushbutton',...
'backgroundcolor',[0.75 0.75 0.75],...
'string','关闭',...
'position',[85 60 80 30],...
'callback','close');

效果
【MATLAB100个实用小技巧】——图形处理(67-75)_第2张图片

69. 图像的频率操作

代码

h0=figure('toolbar','none',...
'position',[198 56 350 468],...
'name','频率操作');
h1=axes('parent',h0,... 
    'position',[0.3 0.45 0.5 0.5],...
'visible','off');
b=remez(10,[0 0.4 0.6 1],[1 1 0 0]);
h=ftrans2(b); 
[H,W]=freqz(b,1,64,'whole'); 
colormap(jet(64))
plot(W/pi-1,fftshift(abs(H)));
b1=uicontrol('parent',h0,... 
    'units','points',...
'tag','b1',...
'backgroundcolor',[0.75 0.75 0.75],...
'style','pushbutton',...
'string','频率变换',...
'position',[30 100 50 20],...
'callback',[...
'cla,',...
'b=remez(10,[0 0.4 0.6 1],[1 1 0 0]);,',...
'h=ftrans2(b);,',... 
'[H,W]=freqz(b,1,64,''whole'');,',...
'colormap(jet(64)),',...
'freqz2(h,[32 32])']);

b2=uicontrol('parent',h0,... 
    'units','points',...
'tag','b2',...
'backgroundcolor',[0.75 0.75 0.75],...
'style','pushbutton',...
'string','频率采样一',...
'position',[100 100 50 20],...
'callback',[...
'cla,',...
'Hd=zeros(11,11);,',...
'Hd(4:8,4:8)=1;,',...
'[f1,f2]=freqspace(11,''meshgrid'');,',... 
'mesh(f1,f2,Hd),',...
'axis([-1 1 -1 1 0 1.2]),',...
'colormap(jet(64))']);
b3=uicontrol('parent',h0,...
'units','points',...
'tag','b3',...
'backgroundcolor',[0.75 0.75 0.75],...
'style','pushbutton',...
'string','频率采样二',...
'position',[170 100 50 20],...
'callback',[...
'cla,',...
'Hd=zeros(11,11);,',...
'Hd(4:8,4:8)=1;,',...
'H=fsamp2(Hd);,',...
'freqz2(h,[32 32]),',...
'axis([-1 1 -1 1 0 1.2]),',...
'colormap(jet(64))']);
b4=uicontrol('parent',h0,...
'units','points',...
'tag','b4',...
'backgroundcolor',[0.75 0.75 0.75],...
'style','pushbutton',...
'string','关闭',...
'fontsize',15,...
'position',[80 50 80 30],...
'callback','close');

效果
【MATLAB100个实用小技巧】——图形处理(67-75)_第3张图片

70. 函数变换

代码

h0=figure('toolbar','none',...
'position',[198 56 350 468],...
'name','函数变换');

h1=axes('parent',h0,...
    'position',[0.25 0.45 0.5 0.5],...
'visible','off'); 
I=imread('cameraman.tif'); 
imshow(I);
b1=uicontrol('parent',h0,...
'units','points',...
'tag','b1',...
'backgroundcolor',[0.75 0.75 0.75],...
'style','pushbutton',...
'string','图像解压',...
'position',[30 100 50 20],...
'callback',[...
'cla,',... 
'I=imread(''cameraman.tif'');,',... 
'I2=im2double(I);,',...
'imshow(I2)']);
b2=uicontrol('parent',h0,... 
    'units','points',...
'tag','b2',...
'backgroundcolor',[0.75 0.75 0.75],...
'style','pushbutton',...
'string','图像压缩',...
'position',[100 100 50 20],...
'callback',[...
'cla,',... 
'I=imread(''cameraman.tif'');,',... 
'I=im2double(I);,',...
'T=dctmtx(8);,',...
'B=blkproc(I,[8 8],''P1*x*P2'',T,T'');,',...
'mask=[1 1 1 1 0 0 0 0;,',...
'1 1 1 0 0 0 0 0;,',...
'1 1 0 0 0 0 0 0;,',...
'1 0 0 0 0 0 0 0;,',...
'0 0 0 0 0 0 0 0;,',...
'0 0 0 0 0 0 0 0;,',...
'0 0 0 0 0 0 0 0;,',...
'0 0 0 0 0 0 0 0];,',...
'B2=blkproc(B,[8 8],''P1.*x'',mask);,',...
'I2=blkproc(B2,[8 8],''P1*x*P2'',T'',T);,',...
'imshow(I2)']); b3=uicontrol('parent',h0,...
'units','points',...
'tag','b3',...
'backgroundcolor',[0.75 0.75 0.75],...
'style','pushbutton',...
'string','线条解析',...
'position',[170 100 50 20],...
'callback',[...
'cla,',... 
'I=imread(''cameraman.tif'');,',... 
'BW=edge(I);,',...
'imshow(BW)']);
b4=uicontrol('parent',h0,...
'units','points',...
'tag','b4',...
'backgroundcolor',[0.75 0.75 0.75],...
'style','pushbutton',...
'string','关闭',...
'fontsize',15,...
'position',[80 50 80 30],...
'callback','close');

效果
【MATLAB100个实用小技巧】——图形处理(67-75)_第4张图片

71. RADON 函数变换

代码

h0=figure('toolbar','none',...
'position',[198 56 350 468],...
'name','实例 71');
h1=axes('parent',h0,...
    'position',[0.3 0.45 0.5 0.5],...
'visible','off'); 
P=phantom(256); 
imshow(P);
b1=uicontrol('parent',h0,...
'units','points',...
'tag','b1',...
'style','pushbutton',...
'string','变换一',...
'backgroundcolor',[0.75 0.75 0.75],...
'position',[30 100 50 20],...
'callback',[...
'cla,',...
'k=1;,',...
'theta1=0:10:170;,',...
'R1=radon(P,theta1);,',...
'imagesc(R1),',...
'colormap(hot),',... 
'colorbar']);
b2=uicontrol('parent',h0,...
'units','points',...
'tag','b2',...
'style','pushbutton',...
'string','变换二',...
'backgroundcolor',[0.75 0.75 0.75],...
'position',[100 100 50 20],...
'callback',[...
'cla,',...
'k=2;,',...
'theta2=0:5:175;,',...
'R2=radon(P,theta2);,',...
'imagesc(R2),',...
'colormap(hot),',... 
'colorbar']);
b3=uicontrol('parent',h0,... 
    'units','points',...
'tag','b3',...
'style','pushbutton',...
'string','变换三',...
'backgroundcolor',[0.75 0.75 0.75],...
'position',[170 100 50 20],...
'callback',[...
'cla,',...
'k=3;,',...
'theta3=0:2:178;,',...
'R3=radon(P,theta3);,',...
'imagesc(R3),',...
'colormap(hot),',... 
'colorbar']);
b4=uicontrol('parent',h0,...
    'units','points',...
'tag','b4',...
'style','pushbutton',...
'string','原始图像',...
'backgroundcolor',[0.75 0.75 0.75],...
'position',[50 50 70 30],...
'callback',[...
'cla,',...
'if k==1,',... 
'I1=iradon(R1,10);,',...
'imshow(I1),',...
'end,',...
'if k==2,',...
'I2=iradon(R2,5);,',...
'imshow(I2),',...
'end,',...
'if k==3,',...
'I3=iradon(R3,2);,',...
'imshow(I3),',... 
'end']);
b5=uicontrol('parent',h0,... 
    'units','points',...
'tag','b5',...
'style','pushbutton',...
'string','关闭',...
'backgroundcolor',[0.75 0.75 0.75],...
'position',[150 50 70 30],...
'callback','close');

效果
【MATLAB100个实用小技巧】——图形处理(67-75)_第5张图片

72. 图像分析(1)

这个可能还有些问题…
代码

h0=figure('toolbar','none',...
'position',[198 56 350 468],...
'name','实例 72');
h1=axes('parent',h0,...
    'position',[0.25 0.45 0.5 0.5],...
'visible','off');
I=imread("D:\opencv\JPEGdata\IMG_20210603_185840.jpg"');
imshow(I)
k=0;
b1=uicontrol('parent',h0,... 
    'units','points',...
'tag','b1',...
'style','pushbutton',...
'string','图像轮廓图',...
'backgroundcolor',[0.75 0.75 0.75],...
'position',[20 100 60 20],...
'callback',[...
'cla,',...
'k=1;,',...
'I=imread("D:\opencv\JPEGdata\IMG_20210603_185840.jpg");,',... 
'imcontour(I)']);
b2=uicontrol('parent',h0,... 
    'units','points',...
'tag','b2',...
'style','pushbutton',...
'string','SOBEL 边界图',...
'backgroundcolor',[0.75 0.75 0.75],...
'position',[100 100 60 20],...
'callback',[...
'cla,',...
'k=2;,',...
'I=imread("D:\opencv\JPEGdata\IMG_20210603_185840.jpg");,',...
'BW=edge(I,''sobel'');,',...
'imshow(BW)']);
b3=uicontrol('parent',h0,... 
    'units','points',...
'tag','b3',...
'style','pushbutton',...
'string','CANNY 边界图',...
'backgroundcolor',[0.75 0.75 0.75],...
'position',[180 100 60 20],...
'callback',[...
'cla,',...
'k=3;,',...
'I=imread("D:\opencv\JPEGdata\IMG_20210603_185840.jpg");,',...
'BW=edge(I,''canny'');,',... 
'imshow(BW)']);
b4=uicontrol('parent',h0,... 
    'units','points',...
'tag','b4',...
'style','pushbutton',...
'string','灰度调整',...
'backgroundcolor',[0.75 0.75 0.75],...
'position',[20 50 60 20],...
'callback',[...
'cla,',...
'k=4;,',...
'I=imread("D:\opencv\JPEGdata\IMG_20210603_185840.jpg");,',...
'J=imadjust(I,[0.15 0.9],[0 1]);,',...
'imshow(J,64)']);
b5=uicontrol('parent',h0,...
'units','points',...
'tag','b5',...
'style','pushbutton',...
'string','图像柱状图',...
'backgroundcolor',[0.75 0.75 0.75],...
'position',[180 50 60 20],...
'callback',[...
'if k==0,',...
'figure,',... 
'imhist(I,64),',... 
'end,',...
'if k==1,',...
'imhist(I,64),',... 
'end,',...
'if k==2,',... 
'imhist(BW,64),',...
'end,',...
'if k==3,',...
'imhist(BW,64),',...
'end,',...
'if k==4,',...
'imhist(J),',... 
'end']);
b6=uicontrol('parent',h0,... 
    'units','points',...
'tag','b6',...
'style','pushbutton',...
'string','关闭',...
'backgroundcolor',[0.75 0.75 0.75],...
'position',[100 50 60 20],...
'callback','close');

效果
【MATLAB100个实用小技巧】——图形处理(67-75)_第6张图片

73. 过滤图像

代码

h0=figure('toolbar','none',...
'position',[198 56 350 468],...
'name','实例 73');
h1=axes('parent',h0,... 
    'position',[0.25 0.45 0.5 0.5],...
'visible','off');
I=imread('eight.tif'); 
imshow(I) 
u1=uimenu('parent',h0,...
'tag','u1',...
'label',' 添 加 噪 声 ',...
'Foregroundcolor',[0.75 0.75 0.75]);
u11=uimenu('parent',u1,...
'tag','u11',...
'label','SALT&PEPPER 噪声',...
'Foregroundcolor',[0.75 0.75 0.75],...
'callback',[...
'set(u11,''checked'',''on'');,',...
'set(u12,''checked'',''off'');,',...
'cla,',...
'I=imnoise(I,''salt & pepper'',0.02);,',... 
'imshow(I)']);

u12=uimenu('parent',u1,...
'tag','u12',...
'label','GAUSSIAN 噪声',...
'Foregroundcolor',[0.75 0.75 0.75],...
'callback',[...
'set(u12,''checked'',''on'');,',...
'set(u11,''checked'',''off'');,',... 
'cla,',...
'I=imnoise(I,''gaussian'',0,0.005);,',... 
'imshow(I)']);
b1=uicontrol('parent',h0,...
    'units','points',...
'tag','b1',...
'style','pushbutton',...
'string','均平过滤',...
'Foregroundcolor',[0.75 0.75 0.75],...
'position',[30 100 50 20],...
'callback',[...
'cla,',... 
'J=filter2(fspecial(''average'',3),I)/255;,',... 
'imshow(J)']);
b2=uicontrol('parent',h0,... 
    'units','points',...
'tag','b2',...
'style','pushbutton',...
'string','中值过滤',...
'Foregroundcolor',[0.75 0.75 0.75],...
'position',[100 100 50 20],...
'callback',[...
'cla,',...
'J=medfilt2(I,[3 3]);,',...
'imshow(J)']); 
b3=uicontrol('parent',h0,...
'units','points',...
'tag','b3',...
'style','pushbutton',...
'string','自适应过滤',...
'Foregroundcolor',[0.75 0.75 0.75],...
'position',[170 100 50 20],...
'callback',[...
'cla,',...
'J=wiener2(I,[5 5]);,',...
'imshow(J)']); 
b4=uicontrol('parent',h0,...
'units','points',...
'tag','b4',...
'style','pushbutton',...
'string','关闭',...
'fontsize',15,... 
'Foregroundcolor',[0.75 0.75 0.75],...
'position',[90 50 70 30],...
'callback','close');

效果
【MATLAB100个实用小技巧】——图形处理(67-75)_第7张图片

74. 图像的区域处理

代码

h0=figure('toolbar','none',...
'position',[198 56 350 468],...
'name','实例 74');
h1=axes('parent',h0,... 
    'position',[0.25 0.45 0.5 0.5],...
'visible','off'); 
I=imread('trees.tif');
imshow(I) 
b1=uicontrol('parent',h0,...
'units','points',...
'tag','b1',...
'style','pushbutton',...
'string','区域过滤一',...
'backgroundcolor',[0.75 0.75 0.75],...
'position',[30 100 50 20],...
'callback',[...
'cla,',...
'I=imread(''trees.tif'');,',... 
'imshow(I),',...
'BW=roipoly;,',...
'h=fspecial(''unsharp'');,',...
'I2=roifilt2(h,I,BW);,',...
'imshow(I2)']);
b2=uicontrol('parent',h0,...
'units','points',...
'tag','b2',...
'style','pushbutton',...
'string','区域过滤二',...
'backgroundcolor',[0.75 0.75 0.75],...
'position',[100 100 50 20],...
'callback',[...
'cla,',...
'BW=imread(''text.tif'');,',...
'f=inline(''imadjust(x,[],[],0.01)'');,',...
'I2=roifilt2(I,BW,f);,',...
'imshow(I2)']);
b3=uicontrol('parent',h0,...
'units','points',...
'tag','b3',...
'style','pushbutton',...
'string','区域填充',...
'backgroundcolor',[0.75 0.75 0.75],...
'position',[170 100 50 20],...
'callback',[...
'cla,',...
'load trees,',... 
'I=ind2gray(X,map);,',...
'imshow(I),',...
'I2=roifill;,',...
'imshow(I2)']);
b4=uicontrol('parent',h0,...
'units','points',...
'tag','b4',...
'style','pushbutton',...
'string','关闭',...
'backgroundcolor',[0.75 0.75 0.75],...
'position',[90 50 70 30],...
'callback','close');

效果
【MATLAB100个实用小技巧】——图形处理(67-75)_第8张图片

75. 图像的颜色处置

代码

h0=figure('toolbar','none',...
'position',[198 56 350 468],...
'name','实例 75');
h1=axes('parent',h0,... 
    'position',[0.12 0.45 0.75 0.5],...
'visible','off'); 
I=imread('trees.tif');
imshow(I)
b1=uicontrol('parent',h0,...
'units','points',...
'tag','b1',...
'style','pushbutton',...
'string','减少颜色',...
'backgroundcolor',[0.75 0.75 0.75],...
'position',[30 100 50 20],...
'callback',[...
'cla,',...
'[X,map]=imread(''trees.tif'');,',...
'[Y,map2]=imapprox(X,map,64);,',... 
'image(Y),',...
'colormap(map2)']); 
b2=uicontrol('parent',h0,...
'units','points',...
'tag','b2',...
'style','pushbutton',...
'string','颜色抖动',...
'backgroundcolor',[0.75 0.75 0.75],...
'position',[100 100 50 20],...
'callback',[...
'cla,',... 
'I=imread(''trees.tif'');,',...
'[X,map]=rgb2ind(I,128,''nodither'');,',... 
'imshow(X)']);
b3=uicontrol('parent',h0,...
    'units','points',...
'tag','b3',...
'style','pushbutton',...
'string','颜色转换一',...
'backgroundcolor',[0.75 0.75 0.75],...
'position',[170 100 50 20],...
'callback',[...
'cla,',...
'I=imread(''trees.tif'');,',... 
'Y=rgb2ntsc(I);,',...
'J=Y(:,:,1);,',...
'imshow(J)']);
b4=uicontrol('parent',h0,...
'units','points',...
'tag','b4',...
'style','pushbutton',...
'string','关闭',...
'backgroundcolor',[0.75 0.75 0.75],...
'position',[170 50 50 20],...
'callback','close'); 
b5=uicontrol('parent',h0,...
'units','points',...
'tag','b5',...
'style','pushbutton',...
'string','颜色转换三',...
'backgroundcolor',[0.75 0.75 0.75],...
'position',[100 50 50 20],...
'callback',[...
'cla,',... 
'I=imread(''trees.tif'');,',... 
'J=rgb2ycbcr(I);,',... 
'imshow(J)']);
b6=uicontrol('parent',h0,...
'units','points',...
'tag','b6',...
'style','pushbutton',...
'string','颜色转换二',...
'backgroundcolor',[0.75 0.75 0.75],...
'position',[30 50 50 20],...
'callback',[...
'cla,',...
'I=imread(''trees.tif'');,',...
'J=rgb2hsv(I);,',... 
'imshow(J)']);

效果
【MATLAB100个实用小技巧】——图形处理(67-75)_第9张图片

你可能感兴趣的:(MATLAB100个编程小技巧,matlab,图像处理,开发语言)