This article is the "Digital image processing principle and practice (MATLAB version)" A Book of Code series PART7 (due to the previous release order adjustment, readers should note the page callout and not only based on the title number of the series article), the book on the 186th to No. 225 page of the code, for the need for readers to download research use. At this point the code has been published more than half. The results of the code execution are shown in the original book, and we recommend that you read the following before downloading the code:
About the principle and practice of Digital Image processing (MATLAB version) of the book Code published instructions
http://blog.csdn.net/baimafujinji/article/details/40987807
P186
A = Rgb2gray (Imread (' circle.png '));
B = Edge (A, ' canny ');
[Centers, radii, metric] = Imfindcircles (b,[22 65]);
Imshow (A);
Viscircles (centers, radii, ' edgecolor ', ' B ');
P195
BW = Imread (' contour.bmp ');
Imshow (bw,[]);
On
S=size (BW);
For row = 2:55:s (1)
For Col=1:s (2)
If BW (Row,col),
Break
End
End
Contour = Bwtraceboundary (BW, [Row, col], ' W ', 8);
if (~isempty (contour))
Plot (Contour (:, 2), Contour (:, 1), ' G ', ' linewidth ', 2);
End
End
P197
I = IM2BW (Imread (' penguins.bmp '), 0.38);
BW = 1-i;
B = bwboundaries (bw,8, ' noholes ');
Imshow (I)
On
For k = 1:length (B)
boundary = b{k};
Plot (Boundary (:, 2), Boundary (:, 1), ' G ', ' linewidth ', 2)
End
Add a little bit of finesse. I did not think of this question when I was writing the demo, and then it was because I wanted to make illustrations for the book, so we needed to get rid of the white edges of the results, and the following code realized the result. It's not about image processing, it doesn't appear in the book, it's just a little bit of a trick about how to save the image in Matlab.
I = IM2BW (Imread (' penguins.bmp '), 0.38);
BW = 1-i;
B = bwboundaries (bw,8, ' noholes ');
Imshow (I, ' border ', ' tight ');
On
For k = 1:length (B)
boundary = b{k};
Plot (Boundary (:, 2), Boundary (:, 1), ' G ', ' linewidth ', 2)
End
SaveAs (GCF, ' pengs3.bmp ');
P203
I = Imread (' nums.bmp ');
locs =[57 64;47 103;81 224;94 274;11 365;85 461;86 540];
BW = Imfill (I, locs, 4);
Imshow (BW);
P204
I = Imread (' nums.bmp ');
BW2 = Imfill (I, ' holes ');
Imshow (BW2);
P205
I = Imread (' tire.tif ');
I2 = Imfill (I);
Figure, Imshow (I), figure, Imshow (I2)
P206
I = Imread (' eight.tif ');
c = [222 272 300 270 221 194];
R = [21 21 75 121 121 75];
J = Roifill (i,c,r);
Imshow (I)
Figure, Imshow (J)
P207
function J = regiongrowing (i,x,y,threshold)
if (exist (' threshold ', ' var ') ==0), threshold=0.2; End
J = zeros (Size (I)); % two-value matrix used to mark output results
[m n] = size (I); % size of the input image
Reg_mean = I (x, y); % grayscale mean of divided area
Reg_size = 1; The number of pixels in the% region
% to store the stack of neighboring points of the partitioned area
Neg_free = 10000; neg_pos=0;
Neg_list = zeros (neg_free,3);
delta=0; % The difference between the newly introduced pixel and the area gray mean value
% region growth until termination conditions are met
while (Delta<threshold && Reg_size<numel (I))
% detects neighborhood pixels and interpret if they are zoned into areas
For i = -1:1
for j = -1:1
xn = x + i; Yn = y + j; % calculates the coordinates of a neighborhood point
% check if neighborhood pixels are out of bounds
Indicator = (xn >= 1) && (yn >= 1) && (xn <= m) && (yn <= n);
% if the neighborhood pixel is not part of the split area then the stack is added
if (indicator && (J (Xn,yn) ==0))
Neg_pos = neg_pos+1;
Neg_list (neg_pos,:) = [xn yn I (xn,yn)]; J (Xn,yn) = 1;
End
End
End
if (neg_pos+10>neg_free),% expands if stack space is low
neg_free=neg_free+10000;
Neg_list ((neg_pos+1): neg_free,:) = 0;
End
% Add the pixels closest to the region mean to the region
DIST = ABS (Neg_list (1:neg_pos,3)-reg_mean);
[Delta, index] = min (dist);
J (x, y) = 2; reg_size=reg_size+1;
% calculates the mean value of the new zone
Reg_mean = (reg_mean*reg_size + neg_list (index,3))/(reg_size+1);
% saves pixel coordinates and then removes pixels from the stack
x = Neg_list (index,1); y = neg_list (index,2);
Neg_list (index,:) =neg_list (Neg_pos,:); neg_pos=neg_pos-1;
End
% will be returned by a region-grown division region in the form of a two-value matrix
j=j>1;
P208
I = im2double (Rgb2gray (Imread (' penguins.bmp ')));
x = 244; y = 679;
J = regiongrowing (i,x,y,0.2);
Figure, Imshow (I+J);
P213
I = Imread (' liftingbody.png ');
S = Qtdecomp (i,.27);
Blocks = Repmat (uint8 (0), size (S));
For Dim = [512 256 128 64 32 16 8 4 2 1];
Numblocks = Length (Find (S==dim));
if (Numblocks > 0)
Values = Repmat (uint8 (1), [Dim Dim Numblocks]);
VALUES (2:dim,2:dim,:) = 0;
Blocks = QTSETBLK (blocks,s,dim,values);
End
End
Blocks (end,1:end) = 1;
Blocks (1:end,end) = 1;
Imshow (I), figure, Imshow (blocks,[])
P219
RGB = Imread (' potatos.jpg ');
I = Rgb2gray (RGB);
hy = fspecial (' Sobel ');
HX = Hy ';
Iy = IMFilter (double (I), HY, ' replicate ');
Ix = IMFilter (double (I), HX, ' replicate ');
Gradmag = sqrt (ix.^2 + iy.^2);
L = watershed (GRADMAG);
Lrgb = Label2rgb (L);
Figure
Subplot (1, 2, 1); Imshow (gradmag,[]), title (' Gradient amplitude image ')
Subplot (1, 2, 2); Imshow (LRGB); Title (' Watershed Partitioning for gradient graphs ')
p221-p224
RGB = Imread (' potatos.jpg ');
I = Rgb2gray (RGB);
hy = fspecial (' Sobel ');
HX = Hy ';
Iy = IMFilter (double (I), HY, ' replicate ');
Ix = IMFilter (double (I), HX, ' replicate ');
Gradmag = sqrt (ix.^2 + iy.^2);
SE = Strel (' disk ', 12);
Ie = Imerode (I, SE);
IOBR = Imreconstruct (Ie, I);
IOBRD = imdilate (iobr, SE);
IOBRCBR = Imreconstruct (Imcomplement (IOBRD), Imcomplement (IOBR));
IOBRCBR = Imcomplement (IOBRCBR);
FGM = Imregionalmax (IOBRCBR);
IT1 = RGB (:,:, 1);
It2 = RGB (:,:, 2);
IT3 = RGB (:,:, 3);
IT1 (FGM) = 255; It2 (FGM) = 0; IT3 (FGM) = 0;
I2 = Cat (3, IT1, It2, IT3);
Figure
Subplot (1, 2, 1); Imshow (FGM, []); Title (' Local Maximum value image ');
Subplot (1, 2, 2); Imshow (I2); Title (' Local maxima overlay image ');
Se2 = Strel (Ones (15,15));
FGM2 = Imclose (FGM, SE2);
FGM3 = Imerode (fgm2, SE2);
FGM4 = Bwareaopen (FGM3, 400);
BW = IM2BW (IOBRCBR, Graythresh (IOBRCBR));
D = Bwdist (BW);
DL = watershed (D);
BGM = DL = = 0;
Gradmag2 = Imimposemin (Gradmag, BGM | fgm4);
L = watershed (GRADMAG2);
% of the first display method
Lrgb = Label2rgb (L, ' Jet ', ' w ', ' Shuffle ');
Figure
Subplot (1,2,1), Imshow (Lrgb), title (' Watershed Segmentation results show 1 ');
% of the second display method
Subplot (1, 2, 2); Imshow (RGB, []), title (' Watershed Segmentation results show 2 ');
Hold on;
Himage = Imshow (LRGB);
Set (Himage, ' alphadata ', 0.3);
(The code release is not finished, please wait for follow-up ...) )
"Digital image processing principle and practice (MATLAB version)" A book Code PART7