- To extract the edges in Image Fig1006 (a) (building). tif.
Solution: Using the canny edge detector, the code is as follows:
function Canny ()% Canny algorithm written by itself, Chen Yu
CLC
Clear all;
Close all;
Img=imread (' Fig1006 (a) (building). tif ');
Figure (1); subplot (221); Imshow (IMG); title (' Original image ');
Figure (one); Imshow (IMG); title (' Original image ');
Img=double (IMG);
[M,n]=size (IMG);
Up to a Gaussian low pass filter, smoothing effect
IMG1=GLPF (IMG),% self-written Gaussian lowpass filter program
Figure (1); subplot (222); Imshow (Uint8 (IMG1)); Title (' Gaussian filter ');
Figure (a); Imshow (Uint8 (IMG1)); title (' Gaussian filtered image ');
Calculate gradient amplitude by percent
Sobel template for sobelx=[-1,-2,-1;0,0,0;1,2,1];%x direction
Sobel template for Sobely=[-1,0,1;-2,0,2;-1,0,1];%y direction
Gx=imfilter (Img1,sobelx, ' replicate '); % for the X-directional gradient component, replicate indicates that the image size is extended by copying the outer boundary
Gy=imfilter (img1,sobely, ' replicate '); % to find the Y-direction gradient component
MG=SQRT (gx.^2+gy.^2); % gradient Amplitude
Ag=atan (GY./GX); % Gradient Direction
Percent non-maximum inhibition
Img2=imaxinhibit (MG,AG);
Figure (Imshow); (Uint8 (Img2)); title (' Non-maximum suppression ');
Figure (1); subplot (223); Imshow (Uint8 (IMG2)); title (' Non-maximum inhibition ');
Percent-to-double threshold (hysteresis threshold) processing
Img3=dualthreshold (IMG2);
Figure (a); Imshow (IMG3); title (' Canny processing image ');
Figure (1); subplot (224); Imshow (IMG3); title (' Canny process image ');
End
%----------------------------------------------------------------------
% below is a sub-function
% Gaussian low pass filter sub-function
function GAUSS=GLPF (I)
[M, N]=size (I);
p=2*m;% Fill image as P*q
Q=2*n;
F0=zeros (P,Q);
F0 (1:m,1:n) =i; % Fill Image
f1= Fftshift (F0);% moves the frequency domain origin to the image center;
f= fft2 (F1); % Fourier transform
D0 = 400; % D0 as cutoff frequency
For U=1:p
For V=1:q
D (u,v) = sqrt ((U-P/2) ^2+ (V-Q/2) ^2); % distance
H (u,v) = 1-exp (-d (u,v) ^2/(2*d0^2)); % Gauss Filter function
% G (u,v) = H (u,v). *f (U,V);
End
End
G = h.*f;% is placed outside the loop, reducing the amount of computation
g = ifft2 (g);% Fourier inverse transformation
G1= Real (g); % take the real part
G2= Ifftshift (G1);% moves the frequency domain origin to the image center;
GAUSS=G2 (1:m,1:n);% crop
End
Non-maximal inhibitory sub-function
function Img=imaxinhibit (MG,AG)
[M,n]=size (MG);
ag=ag*180;
IMG=MG;
For i=2:m-1
For j=2:n-1
If Ag (i,j) >=-22.5&&ag (i,j) <22.5| | ABS (Ag (I,J)) >=157.5% horizontal Edge
If Mg (i,j) <mg (i+1,j) | | Mg (i,j) <mg (I-1,J)
IMG (I,J) = 0; % suppression
End
Else
If
Ag (i,j) >=-67.5&&ag (i,j) <-22.5| | Ag (i,j) >=112.5&&ag (i,j) <157.5%+45°
If Mg (i,j) <mg (i+1,j-1) | | Mg (i,j) <mg (i-1,j+1)
IMG (I,J) = 0; % suppression
End
Else
If
Ag (i,j) >=-112.5&&ag (i,j) <-67.5| | Ag (i,j) >=67.5&&ag (i,j) <112.5% Vertical
If Mg (i,j) <mg (i,j-1) | | Mg (i,j) <mg (i,j+1)
IMG (I,J) = 0; % suppression
End
Else
If
Ag (i,j) >-157.5&&ag (i,j) <-112.5| | Ag (i,j) >=22.5&&ag (i,j) <67.5%-45°
If Mg (i,j) <mg (i-1,j-1) | | Mg (i,j) <mg (i+1,j+1)
IMG (I,J) = 0; % suppression
End
End
End
End
End
End
End
End
Percent double threshold (lag threshold) processing sub-function
function G=dualthreshold (Gn)
[M,n]=size (Gn);
Gn=mat2gray (Gn);% normalization
GNH=GN;
GNL=GN;
th=0.12;
tl=0.04;% satisfies the ratio of high and low thresholds to 3:1 or 2:1
For i=2:m-1
For j=2:n-1
If Gn (i,j) <th
GNH (I,J) =0;% High threshold suppression
End
If Gn (i,j) <tl
GNL (I,J) =0;% low threshold suppression
End
End
End
gnl1=gnl-gnh;% Remove all non-0 pixels from the Gnl from GNH, see teaching material P465 (10.2-35)
b=[0,1,0;1,1,1;0,1,0];% is connected with an expanded method
Gnl1=imdilate (GNL1,B);
g=gnh+gnl1;% attaching all non-0 pixels in the GNL1 to Gnh
G=GNH;
Figure (Imshow); (GNL); title (' Gnl ');
Figure (+); Imshow (GNH); title (' Gnh ');
Figure (Imshow); (GNL1); title (' Gnl-gnh ');
End
The results of the operation are as follows:
- The optimal Global threshold segmentation method (i.e. global optimal segmentation, Dajing algorithm) is implemented and applied to the segmentation of the image Fig1013 (a) (Scanned-text-grayscale). tif text, and the optimal global optimal threshold is obtained.
Solution: Dajing algorithm, the code is as follows:
function Otsu ()% write your own Dajing algorithm, CK
CLC
Clear all;
Close all;
Img=imread (' Fig1013 (a) (Scanned-text-grayscale). tif '); % read in picture
Img=rgb2gray (IMG);% because the original is 3-dimensional, it needs to be converted
Figure (1), Imshow (IMG), title (' Original image ')
P=zeros (1,256);% of each gray level
Img=double (IMG);% double precision
[M,n]=size (IMG);
% computed image histogram
For k=0:255
P (k+1) =length (Find (Img==k))/(M*n); % calculates the probability of each level of grayscale appearing (array subscript starting from 1)
End
k=1:1:256;
Figure (2); Bar (K,P); Title (' Original image histogram ');
For i=2:256
If P (i) ~=0
mint=i+1;% finding the smallest gray value with a ratio of not 0
Break
End
End
For I=256:-1:1
If P (i) ~=0;
maxt=i-1;% finding the maximum gray value of a ratio of not 0
Break
End
End
MG=0;%MG is the global mean
For i=1:255
Mg=mg+i*p (i);
End
Var=zeros (1, (maxt-mint));% Inter-class variance
var1=0;% maximum inter-class variance
For K=mint:maxt
The probability of p1=0;% being divided into C1
m=0;% the average gray value of the pixels that are divided into C1
For I=1:k
P1=p1+p (i);% of the probability of being divided into C1, referring to the textbook P481 (10.3-4)
M=m+i*p (i);% The average gray value of the pixels that are divided into C1, referring to the textbook P481 (10.3-8)
End
Var (k) = (mg*p1-m) ^2/(p1* (1-P1));% inter-class variance, reference textbook P481 (10.3-15)
if (Var (k) >=var1)
Var1=var (k);% get maximum inter-class variance
End
End
% if there are more than one k to maximize the variance between the classes, take the average of multiple K as the best threshold, referring to the textbook P481
K=zeros (1, (maxt-mint));% optimal threshold value
The number of thresholds for obtaining the maximum inter-class variance of j=0;% statistics
For K=mint:maxt
if (Var (k) ==var1)
j=j+1;
K (j) =k;% the threshold for obtaining the maximum inter-class variance
End
End
K1=sum (k)/j% takes the average of multiple K as the optimal threshold value
% optimal threshold processing with Ostu method
For i=1:m
For J=1:n
if (IMG (i,j) >k1)
IMG1 (i,j) = 255;
Else
IMG1 (I,J) = 0;
End
End
End
Figure (3); imhist (IMG1); title (' Otsu histogram ');
Figure (4); imshow (IMG1); title (' Otsu best threshold processing image ');
Text (' Position ', [400,480], ' String ', ' Otsu best threshold: ', ' Color ', ' r ')% mark the best threshold value
Text (' Position ', [510,480], ' String ', Num2str (K1), ' Color ', ' r ')
The results of the operation are as follows:
Optimal global threshold value: 102
Edge extraction, Dajing algorithm