Code of the book "Digital Image Processing Principles and Practices (MATLAB)" Part4, matlab Digital Image Processing
This article is part 4 of the Code series in the book "Digital Image Processing Principles and Practices (MATLAB)", which records the code from 135th to 183rd pages for readers to download and use. For the code execution result, see the original book layout. We recommend that you read the following before downloading the Code:
Introduction to the code release in the book "Digital Image Processing Principles and Practices (MATLAB )"
Http://blog.csdn.net/baimafujinji/article/details/40987807
P139
Original = imread('snowflakes.png ');
Figure, imshow (original );
Se = strel ('disk', 5 );
AfterOpening = imopen (original, se );
Figure, imshow (afterOpening, []);
P140
OriginalBW = imread('circles.png ');
Imshow (originalBW );
Se = strel ('disk', 10 );
CloseBW = imclose (originalBW, se );
Figure, imshow (closeBW)
P144
Bw = imread('bw.bmp ');
Shape1 = [0 0 0 0 1
0 0 0 1 1
0 0 1 1 1
0 1 1 1 1
1 1 1 1];
Shape2 = [1 1 0 0 0
1 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0];
Bw2 = bwhitmiss (bw, shape1, shape2 );
Imshow (bw2)
P146-1
I = imread('letter2.jpg ');
I = im2bw (I );
I1 = bwmorph (I, 'thin', inf );
Figure (1), imshow (I1 );
P146-2
I = imread('letter2.jpg ');
I = im2bw (I );
I2 = bwmorph (I, 'skel', inf );
Figure (2), imshow (I2 );
P153
I = imread('lena.jpg ');
I = rgb2gray (I );
BW1 = edge (I, 'ots ');
BW2 = edge (I, 'sobel ');
BW3 = edge (I, 'prewitt ');
Figure
Subplot (2, 2, 1), imshow (I), title ('original ')
Subplot (, 2), imshow (BW1), title ('ots ')
Subplot (2, 2, 3), imshow (BW2), title ('sobel ')
Subplot (2, 2, 4), imshow (BW3), title ('prewitt ')
P157
I = imread('einstein.bmp ');
I = rgb2gray (I );
N = [1, 2, 1
0, 0, 0
-1,-2,-1];
Edge_n = imfilter (I, N, 'shortric ', 'conv ');
Imwrite (edge_n, 'edge_n.jpg ');
P160
I = rgb2gray(imread('lena.jpg '));
M = [1, 1, 1
1,-8, 1
1, 1];
Img = imfilter (I, M );
[X, y] = size (I );
Img2 = img;
For I = 2: X-1
For j = 2: Y-1
A = [img (I, j + 1), img (I, J-1), img (I + 1, j + 1), img (I + 1, j-1 ),...
Img (I-1, j + 1), img (I-1, J-1), img (I + 1, j), img (I-1, j)];
If (max (a)-min (a)> 64 & max (a)> img (I, j) & min (a) Img2 (I, j) = 255;
Else
Img2 (I, j) = 0;
End
End
End
P165
I = imread('lena.jpg ');
IMG = rgb2gray (I );
Edge_LoG = edge (IMG, 'log ');
Imshow (Edge_LoG );
Figure
Subplot (1, 2), imshow (IMG );
Subplot (1, 2), imshow (Edge_LoG );
P167
I = double(rgb2gray(imread('lena.jpg ')));
Figure, imshow (uint8 (I ))
DoG = fspecial ('gaussian ', 5, 0.8)-fspecial ('gaussian', 5, 0.6 );
ImageDoG = imfilter (I, DoG, 'regionric ', 'conv ');
Figure, imshow (ImageDoG)
% Threshold = 2
Proc_Img1 = ImageDoG;
Proc_Img1 (find (proc_Img1 <2) = 0;
Figure, imshow (proc_Img1)
% Threshold = 3
Proc_Img2 = ImageDoG;
Proc_Img2 (find (proc_Img2 <3) = 0;
Figure, imshow (proc_Img2)
P172
Img = edge (I, 'ecold', [0.032, 0.08], 3 );
P183
RGB = imread('building.jpg ');
I = rgb2gray (RGB );
BW = edge (I, 'ecken ');
[H, T, R] = hough (BW, 'rhoresolution', 0.5, 'thetaresolution', 0.5 );
Figure, imshow (imadjust (mat2gray (H), 'xdata', T ,...
'Ydata', R, 'initialmagnification', 'fit ');
Xlabel ('\ theta'), ylabel ('\ ro ');
Axis on; axis normal; hold on;
Colormap (hot );
Peaks = houghpeaks (H, 15 );
Figure, imshow (BW );
Hold on;
Lines = houghlines (BW, T, R, peaks, 'fillgap ', 25, 'minlength', 15 );
Max_len = 0;
For k = 1: length (lines)
Xy = [lines (k). point1; lines (k). point2];
Plot (xy (:, 1), xy (:, 2), 'linewidth', 3, 'color', 'B ');
Plot (xy (1, 1), xy (1, 2), 'x', 'linewidth', 3, 'color', 'yellow ');
Plot (xy (2, 1), xy (2, 2), 'x', 'linewidth', 3, 'color', 'red ');
Len = norm (lines (k). point1-lines (k). point2 );
If (len> max_len)
Max_len = len;
Xy_long = xy;
End
End
(Code release is not complete. Please wait for the future ...)