|
| 1 | +- [Matlab实现图像分割](#matlab实现图像分割) |
| 2 | + - [目的](#目的) |
| 3 | + - [内容](#内容) |
| 4 | + - [线检测](#线检测) |
| 5 | + - [边缘检测](#边缘检测) |
| 6 | + - [使用梯度的分水岭分割](#使用梯度的分水岭分割) |
| 7 | + - [控制标记符的分水岭分割](#控制标记符的分水岭分割) |
| 8 | + |
| 9 | + |
| 10 | +# Matlab实现图像分割 |
| 11 | + |
| 12 | +## 目的 |
| 13 | + |
| 14 | +1. 掌握线检测 |
| 15 | + |
| 16 | +2. 边缘检测 |
| 17 | + |
| 18 | +3. 使用梯度的分水岭分割 |
| 19 | + |
| 20 | +4. 控制标记符的分水岭分割 |
| 21 | + |
| 22 | +## 内容 |
| 23 | + |
| 24 | +### 线检测 |
| 25 | + |
| 26 | +```matlab |
| 27 | +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 28 | +function B=pixeldup(A, m, n) |
| 29 | +
|
| 30 | +
|
| 31 | +if nargin<2; |
| 32 | + error('At least two inputs are required'); |
| 33 | +end |
| 34 | +if nargin==2; |
| 35 | + n=m; |
| 36 | +end |
| 37 | +u=1:size(A,1); |
| 38 | +m=round(m); |
| 39 | +u=u(ones(1,m), :); |
| 40 | +u=u(:); |
| 41 | +
|
| 42 | +
|
| 43 | +
|
| 44 | +v = 1:size(A,1); |
| 45 | +n=round(n); |
| 46 | +v=v(ones(1,n), :); |
| 47 | +v=v(:); |
| 48 | +B=A(u,v); |
| 49 | +%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 50 | +clear all |
| 51 | +clc |
| 52 | +f=imread('D:\pic\DIP3E_CH10\Fig1005(a)(wirebond_mask).tif'); |
| 53 | +figure,imshow(f); |
| 54 | +
|
| 55 | +w=[2 -1 -1;-1 2 -1;-1 -1 2]; |
| 56 | +g=imfilter(double(f),w); |
| 57 | +figure,imshow(g,[])%fig 2 |
| 58 | +
|
| 59 | +gtop=g(1:120,1:120); |
| 60 | +gtop=pixeldup(gtop, 4); |
| 61 | +figure,imshow(gtop,[])%fig 3 |
| 62 | +
|
| 63 | +gbot=g(end-119:end,end-119:end); |
| 64 | +gbot=pixeldup(gbot,4); |
| 65 | +figure,imshow(gbot,[])%fig 4 |
| 66 | +
|
| 67 | +g=abs(g); |
| 68 | +figure,imshow(g,[])%fig 5 |
| 69 | +
|
| 70 | +T=max(g(:)); |
| 71 | +g=g>=T; |
| 72 | +figure,imshow(g)%fig 6 |
| 73 | +%%%%%%%%%%%%%%%%%%%%%%%% |
| 74 | +``` |
| 75 | + |
| 76 | +### 边缘检测 |
| 77 | + |
| 78 | +```matlab |
| 79 | +%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 80 | +clear all |
| 81 | +clc |
| 82 | +f=imread('D:\pic\DIP3E_CH10\Fig1016(a)(building_original).tif'); |
| 83 | +figure,imshow(f); |
| 84 | +[gv,t]=edge(f,'sobel','vertical'); |
| 85 | +figure,imshow(gv); |
| 86 | +t |
| 87 | +
|
| 88 | +gv=edge(f,'sobel',0.15,'vertical'); |
| 89 | +figure,imshow(gv); |
| 90 | +gboth=edge(f,'sobel',0.15); |
| 91 | +figure,imshow(gboth); |
| 92 | +w45=[-2 -1 0;-1 0 1;0 1 2] |
| 93 | +g45=imfilter(double(f),w45,'replicate'); |
| 94 | +T=0.3*max(abs(g45(:))); |
| 95 | +g45=g45>=T; |
| 96 | +figure,imshow(g45); |
| 97 | +%%%%%%%%%%%%%%%%%%%%%%%% |
| 98 | +%Sobel,LoG 和Canny 边缘检测器的比较 |
| 99 | +
|
| 100 | +clear all |
| 101 | +clc |
| 102 | +f=imread('D:\pic\DIP3E_CH10\Fig1016(a)(building_original).tif'); |
| 103 | +figure,imshow(f); |
| 104 | +
|
| 105 | +[g_sobel_default,ts]=edge(f,'sobel'); |
| 106 | +figure,imshow(g_sobel_default); |
| 107 | +[g_log_default,tlog]=edge(f,'log'); |
| 108 | +figure,imshow(g_log_default); |
| 109 | +[g_canny_default,tc]=edge(f,'canny'); |
| 110 | +figure,imshow(g_canny_default); |
| 111 | +
|
| 112 | +[g_sobel_best,ts]=edge(f,'sobel',0.05); |
| 113 | +figure,imshow(g_sobel_best); |
| 114 | +[g_log_best,tlog]=edge(f,'log',0.003,2.25); |
| 115 | +figure,imshow(g_log_best); |
| 116 | +[g_canny_best,tc]=edge(f,'canny',[0.04 0.10],1.5); |
| 117 | +figure,imshow(g_canny_best); |
| 118 | +``` |
| 119 | + |
| 120 | +### 使用梯度的分水岭分割 |
| 121 | + |
| 122 | +```matlab |
| 123 | +clear all |
| 124 | +clc |
| 125 | +f=imread('D:\pic\DIP3E_CH10\Fig1057(a)(small_blobs-original).tif'); |
| 126 | +figure,imshow(f); |
| 127 | +
|
| 128 | +h=fspecial('sobel'); |
| 129 | +fd=double(f); |
| 130 | +g=sqrt(imfilter(fd,h,'replicate').^2+... |
| 131 | + imfilter(fd,h','replicate').^2); |
| 132 | +figure,imshow(g); |
| 133 | +
|
| 134 | +L=watershed(g); |
| 135 | +figure,imshow(L); |
| 136 | +wr=L==0; |
| 137 | +
|
| 138 | +g2=imclose(imopen(g,ones(3,3)),ones(3,3)); |
| 139 | +figure,imshow(g2); |
| 140 | +L2=watershed(g2); |
| 141 | +figure,imshow(L2); |
| 142 | +wr2=L2==0; |
| 143 | +f2=f; |
| 144 | +f2(wr2)=255; |
| 145 | +figure,imshow(f2); |
| 146 | +``` |
| 147 | + |
| 148 | +### 控制标记符的分水岭分割 |
| 149 | + |
| 150 | +```matlab |
| 151 | +%%%%%%%%%%%%%%%%理解每幅图像的含义%%%%%%%%%%% |
| 152 | +clear all |
| 153 | +clc |
| 154 | +f=imread('D:\pic\DIP3E_CH10\Fig1057(a)(small_blobs-original).tif'); |
| 155 | +figure,imshow(f); |
| 156 | +
|
| 157 | +h=fspecial('sobel'); |
| 158 | +fd=double(f); |
| 159 | +g=sqrt(imfilter(fd,h,'replicate').^2+... |
| 160 | +imfilter(fd,h','replicate').^2); |
| 161 | +L=watershed(g); |
| 162 | +wr=L==0; |
| 163 | +
|
| 164 | +rm=imregionalmin(g); |
| 165 | +figure,imshow(rm); |
| 166 | +im=imextendedmin(f,2); |
| 167 | +figure,imshow(im); |
| 168 | +fim=f; |
| 169 | +fim(im)=175; |
| 170 | +figure,imshow(fim); |
| 171 | +
|
| 172 | +Lim=watershed(bwdist(im)); |
| 173 | +figure,imshow(Lim); |
| 174 | +em=Lim==0; |
| 175 | +
|
| 176 | +g2=imimposemin(g,im|em); |
| 177 | +figure,imshow(g2); |
| 178 | +
|
| 179 | +L2=watershed(g2); |
| 180 | +figure,imshow(L2); |
| 181 | +f2=f; |
| 182 | +f2(L2==0)=255; |
| 183 | +figure,imshow(f2); |
| 184 | +``` |
| 185 | + |
| 186 | +参考文献: |
| 187 | + |
| 188 | +[1] [Rafael C. Gonzalez, Richard E. Woods, and Steven L. Eddins. 2003. Digital Image Processing Using MATLAB. Prentice-Hall, Inc., USA.](https://github.com/timerring/digital-image-processing-matlab/blob/main/reference/Digital_Image_Processing_Using_Matlab.pdf) |
| 189 | + |
| 190 | +[2] [阮秋琦. 数字图像处理(MATLAB版)[M]. 北京:电子工业出版社, 2014.](https://github.com/timerring/digital-image-processing-matlab/blob/main/reference/Digital_Image_Processing_(MATLAB_version).pdf) |
| 191 | + |
| 192 | +[3] [冈萨雷斯. 数字图像处理(第三版)[M]. 北京:电子工业出版社, 2011.](https://github.com/timerring/digital-image-processing-matlab/blob/main/reference/Digital_Image_Processing_(Third_Edition).pdf) |
| 193 | + |
| 194 | + |
| 195 | + |
| 196 | +[返回首页](https://github.com/timerring/digital-image-processing-matlab) |
0 commit comments