Current location - Recipe Complete Network - Pregnant women's recipes - Find MATLAB code
Find MATLAB code
MATLAB practical source code

1 image reading and rotation

a = imread('); % read image

Subplots (2, 2, 1), im show (a), title ('original'); % output image

I = RGB 2 gray(A);

Subplot (2,2,2), imshow(A), title ('gray image');

Subplot (2,2,3), imhist(I), title ('gray image histogram'); % output original histogram

θ= 30°; J = imrotate(I,theta); % Try changing the angle θ.

Subplot (2, 2, 4), instant message display (j), title ("rotating image")

2 edge detection

I=imread('C:\Users\HP\Desktop\ usual summary \ luffy.jpg');

Subplots (2, 2, 1), im show (I), title ('original');

I 1=edge(I,' Sobel ');

Subplot (2 2,2,2), im show (i 1), title ('Sobel edge detection');

I2=edge(I,' prewitt ');

Subplot (2, 2, 3), im show (I2), title ('prewitt edge detection');

I3=edge(I,' log ');

Subplots (2, 2, 4), im show (i3), Title ('log edge detection');

3 image inversion

MATLAB program is implemented as follows:

I = im read(' xian . BMP ');

j = double(I);

J =-J+(256- 1); % image inversion linear transformation

h = uint 8(J);

Subplots (1, 2, 1), im show (i);

Subplots (1, 2, 2), im show (h);

4. Gray linear transformation

MATLAB program is implemented as follows:

I = im read(' xian . BMP ');

Subplots (2,2, 1), im show (i);

Title ("original");

axis();

Axis open; % display coordinate system

I 1 = RGB 2 gray(I);

Subplots (2, 2, 2), im show (I1);

Title ("grayscale image");

axis();

Axis open; % display coordinate system

J=imadjust(I 1,[0. 1 0.5],[]); % local stretching, stretching gray scale is between [0. 1.5] and [0. 1].

Subplots (2, 2, 3), im show (j);

Title ('Linear Transform Image [0.1.5]');

axis();

Grid open; % show gridlines

Axis open; % display coordinate system

K=imadjust(I 1,[0.3 0.7],[]); % local stretching, stretching gray scale is between [0.3-0.7] and [0. 1].

Subplots (2, 2, 4), im show (k);

Title ('Linear Transform Image [0.30.7]');

axis();

Grid open; % show gridlines

Axis open; % display coordinate system

5. Nonlinear conversion

MATLAB program is implemented as follows:

I = im read(' xian . BMP ');

I 1 = RGB 2 gray(I);

Subplots (1, 2, 1), im show (I1);

Title ("grayscale image");

axis();

Grid open; % show gridlines

Axis open; % display coordinate system

j = double(I 1);

J = 40 *(log(J+ 1));

h = uint 8(J);

Subplots (1, 2, 2), im show (h);

Title ("Logarithmically transformed image");

axis();

Grid open; % show gridlines

Axis open; % display coordinate system

4. Histogram equalization

MATLAB program is implemented as follows:

I = im read(' xian . BMP ');

I = RGB 2 gray(I);

Figure;

Subplots (2, 2,1);

Imshow (1);

Subplots (2, 2, 2);

Imhist (1);

I 1 = histeq(I);

Figure;

Subplots (2, 2,1);

imshow(I 1);

Subplots (2, 2, 2);

imhist(I 1);

5. Linear smoothing filter

Using MATLAB to realize the program of suppressing noise by regional average method;

I = im read(' xian . BMP ');

Subplot (23 1)

Imshow (1)

Title ("Original Image")

I = RGB 2 gray(I);

I 1=imnoise(I,' salt & pepper', 0.02);

Subplots (232)

imshow(I 1)

Title ("Image with Salt and Pepper Noise")

k 1 = filter 2(f special(' average ',3),I 1)/255; % 3*3 Template smoothing filtering.

k2=filter2(fspecial('average ',5),I 1)/255; % for 5*5 template smoothing filter k3 = filter2 (fspecial ('average', 7), I1)/255; % 7*7 template smooth filtering.

k4=filter2(fspecial('average ',9),I 1)/255; % 9*9 template smooth filtering.

Subplot (233), im show (k1); Title ('3*3 template smooth filtering');

Subplots (234), IM Show (K2); Title ('5*5 template smooth filtering');

Subplots (235), IM Show (K3); Title ('7*7 template smooth filtering');

Subplots (236), IM SHOW (K4); Title ('9*9 Template Smooth Filtering');

6. Median filter

The program of median filtering with MATLAB is as follows:

I = im read(' xian . BMP ');

I = RGB 2 gray(I);

J=imnoise(I,' salt & pepper', 0.02);

Subplot (23 1), im show (I); Title ("original");

Subplot (232), im show (j); Title ('Add salt and pepper noise image');

k 1 = medfilt 2(J); % 3*3 template median filtering.

k2=medfilt2(J,); % for 5*5 template median filtering.

k3=medfilt2(J,); Median filtering of% 7*7 template.

k4=medfilt2(J,); Median filtering of% 9*9 template.

Subplot (233), im show (k1); Title ('3*3 template median filtering');

Subplots (234), IM Show (K2); Title ('5*5 template median filtering');

Subplots (235), IM Show (K3); Title ('7*7 template median filtering');

Subplots (236), IM SHOW (K4); Title ('9*9 template median filtering');

7. Use Sobel operator and Laplacian operator to sharpen the image:

I = im read(' xian . BMP ');

Subplots (2,2, 1), im show (i);

Title ("original");

axis();

Grid open; % show gridlines

Axis open; % display coordinate system

I 1 = im2bw(I);

Subplots (2, 2, 2), im show (I1);

Title ("binary image");

axis();

Grid open; % show gridlines

Axis open; % display coordinate system

h = f special(' Sobel '); % select sobel operator

J=filter2(H,I 1); % convolution operation

Subplots (2, 2, 3), im show (j);

Title ("sobel operator sharpening image");

axis();

Grid open; % show gridlines

Axis open; % display coordinate system

h=[0 1 0, 1 -4 1,0 1 0]; % Laplace operator

J 1=conv2(I 1, h,' same'); % convolution operation

Subplots (2, 2, 4), im show (j1);

Title ('Laplace operator sharpening image');

axis();

Grid open; % show gridlines

Axis open; % display coordinate system

8. Gradient operator edge detection

MATLAB is used to realize the following functions:

I = im read(' xian . BMP ');

Subplots (2, 3,1);

Imshow (1);

Title ("original");

axis();

Grid open; % show gridlines

Axis open; % display coordinate system

I 1 = im2bw(I);

Subplots (2, 3, 2);

imshow(I 1);

Title ("binary image");

axis();

Grid open; % show gridlines

Axis open; % display coordinate system

I2=edge(I 1,' Roberts ');

Figure;

Subplots (2, 3, 3);

imshow(I2);

Title ("roberts operator segmentation result");

axis();

Grid open; % show gridlines

Axis open; % display coordinate system

I3=edge(I 1,' Sobel ');

Subplots (2, 3, 4);

im show(I3);

Title ('sobel operator segmentation result');

axis();

Grid open; % show gridlines

Axis open; % display coordinate system

I4=edge(I 1,' Prewitt ');

Subplots (2, 3, 5);

im show(I4);

Title ("Prewitt operator segmentation result");

axis();

Grid open; % show gridlines

Axis open; % display coordinate system

9. Logarithmic operator for edge detection

MATLAB program is used to realize the following functions:

I = im read(' xian . BMP ');

Subplots (2, 2,1);

Imshow (1);

Title ("original");

I 1 = RGB 2 gray(I);

Subplots (2, 2, 2);

imshow(I 1);

Title ("grayscale image");

I2=edge(I 1,' log ');

Subplots (2, 2, 3);

imshow(I2);

Title ("Logarithmic Operator Division Result");

Edge detection of 10. Canny operator

MATLAB program is used to realize the following functions:

I = im read(' xian . BMP ');

Subplots (2, 2,1);

Imshow (1);

Title ("Original Image")

I 1 = RGB 2 gray(I);

Subplots (2, 2, 2);

imshow(I 1);

Title ("grayscale image");

I2=edge(I 1,' canny ');

Subplots (2, 2, 3);

imshow(I2);

Title ("canny operator segmentation result");

1 1. boundary tracing (bwtraceboundary function)

Chromaticity control center

Clean up all

I = im read(' xian . BMP ');

I guessed.

Imshow (1);

Title ("original");

I 1 = RGB 2 gray(I); % Converts a color image to a grayscale image.

threshold = gray thresh(I 1); % Calculate the threshold required to convert gray image into binary image.

BW=im2bw(I 1, threshold); % Converts a grayscale image to a binary image

I guessed.

im show(BW);

Title ("binary image");

Dim = size (bw);

col = round(dim(2)/2)-90; % calculate starting point column coordinates

row=find(BW(:,col), 1); % Calculate the coordinates of the starting line

Connectivity = 8;

num _ points = 180;

Contour = bwtraceboundary, [row, col],' n', connectivity, num _ points);

% extract boundary

I guessed.

imshow(I 1);

Hold on;

plot(contour(:,2),contour(:, 1),' g ',' LineWidth ',2);

Title ("Boundary Tracking Image");

12. Hough transform

I = im read(' xian . BMP ');

Pancake = rgb2gray (i);

Subplots (2, 2,1);

Imshow (pancake);

Title ("grayscale image");

axis();

Grid open;

Axis open;

BW=edge (pancake, Prewitt);

Subplots (2, 2, 2);

im show(BW);

Title ("image after edge detection by prewitt operator");

axis();

Grid open;

Axis open;

[H,T,R]= Hough(BW);

Subplots (2, 2, 3);

imshow(H,[],' XData ',T,' YData ',R,' InitialMagnification ',' fit ');

Title ('Hough Transform Diagram');

xlabel('\theta '),ylabel(' \ rho ');

Open the shaft, the shaft is normal, and keep it;

P=houghpeaks(H,5,' threshold ',ceil(0.3 * max(H(:)));

x=T(P(:,2)); y=R(P(:, 1));

Plot(x, y,' s',' color',' white');

lines=houghlines(BW,T,R,P,' FillGap ',5,' MinLength ',7);

Subplots (2, 2, 4); , imshow (pancake);

Title ('Hough Transform Image Detection');

axis();

Grid open;

Axis open;

Hold on;

max _ len = 0;

For k= 1: length (line)

xy =[lines(k). point 1; lines(k). point 2];

Plot(xy (:,1), xy (:,2),' line width', 2,' color',' green');

Plot(xy( 1, 1), xy( 1, 2),' x',' line width', 2,' color',' yellow');

Plot(xy(2, 1), xy(2,2),' x',' line width', 2,' color',' red');

len = norm(lines(k). point 1-lines(k). point 2);

if(len & gt; max_len)

max _ len = len

xy _ long = xy

end

end

Plot(xy_long (:,1), xy_long (:,2),' line width', 2,' color',' cyan');

13. histogram threshold method

Using MATLAB to realize histogram threshold method;

I = im read(' xian . BMP ');

I 1 = RGB 2 gray(I);

Figure;

Subplots (2, 2,1);

imshow(I 1);

Title ("Gray Image")

axis();

Grid open; % show gridlines

Axis open; % display coordinate system

[m,n]= size(I 1); % Measure the image size parameter

GP = zero (1, 256); % Create a vector in advance to store the gray probability.

For k=0:255

GP(k+ 1)= length(find(I 1 = = k))/(m * n); % Calculate the probability of each gray level and store it in the corresponding position in GP.

end

Plot (2 2,2,2), bar (0: 255, gp,' g')% plot histogram.

Title ("Gray Histogram")

Xlabel ('gray value')

Ylabel ("probability of occurrence")

I2=im2bw(I, 150/255);

Subplots (2, 2, 3), IM Show (I2);

Title ("Segmentation Image with Threshold of 150")

axis();

Grid open; % show gridlines

Axis open; % display coordinate system

I3=im2bw(I,200/255); %

Subplots (2, 2, 4), IM Show (i3);

Title ("Segmentation Image with Threshold of 200")

axis();

Grid open; % show gridlines

Axis open; % display coordinate system

14. Automatic threshold method: Otsu method

Realization of Otsu algorithm with MATLAB;

Chromaticity control center

Clean up all

I = im read(' xian . BMP ');

Subplots (1, 2, 1), im show (i);

Title ("Original Image")

axis();

Grid open; % show gridlines

Axis open; % display coordinate system

level = gray thresh(I); % determine the gray threshold

BW=im2bw(I,level);

Subplots (1, 2, 2), im show (bw);

Title ("Otsu Threshold Segmentation Image")

axis();

Grid open; % show gridlines

Axis open; % display coordinate system

15. Expansion operation

I = im read(' xian . BMP '); % load image

I 1 = RGB 2 gray(I);

Subplots (1, 2,1);

imshow(I 1);

Title ("Gray Image")

axis();

Grid open; % show gridlines

Axis open; % display coordinate system

se=strel('disk ', 1); % Generate circular structural elements

I2 = im dila(I 1,se); % Expand the image with the generated structural elements

Subplots (1, 2, 2);

imshow(I2);

Title ("Extended Image");

axis();

Grid open; % show gridlines

Axis open; % display coordinate system

16. Corrosion operation

Realization of corrosion operation with MATLAB

I = im read(' xian . BMP '); % load image

I 1 = RGB 2 gray(I);

Subplots (1, 2,1);

imshow(I 1);

Title ("Gray Image")

axis();

Grid open; % show gridlines

Axis open; % display coordinate system

se=strel('disk ', 1); % Generate circular structural elements

I2=imerode(I 1,se); % Etch the image with the generated structural elements.

Subplots (1, 2, 2);

imshow(I2);

Title ("Etched image");

axis();

Grid open; % show gridlines

Axis open; % display coordinate system

Open and close operations.

Realization of opening and closing operation with MATLAB

I = im read(' xian . BMP '); % load image

Subplots (2,2, 1), im show (i);

Title ("original");

axis();

Axis open; % display coordinate system

I 1 = RGB 2 gray(I);

Subplots (2, 2, 2), im show (I1);

Title ("grayscale image");

axis();

Axis open; % display coordinate system

se=strel('disk ', 1); % adopts a circle with a radius of 1 as the structural element.

I2=imopen(I 1,se); % open operation

I3=imclose(I 1,se); % close operation

Subplots (2, 2, 3), IM Show (I2);

Title ("Open image after calculation");

axis();

Axis open; % display coordinate system

Subplots (2, 2, 4), IM Show (i3);

Title ("image after closing operation");

axis();

Axis open; % display coordinate system

18. Combined opening and closing operations

I = im read(' xian . BMP '); % load image

Subplots (3, 2, 1), im show (i);

Title ("original");

axis();

Axis open; % display coordinate system

I 1 = RGB 2 gray(I);

Subplots (3, 2, 2), im show (I1);

Title ("grayscale image");

axis();

Axis open; % display coordinate system

se=strel('disk ', 1);

I2=imopen(I 1,se); % open operation

I3=imclose(I 1,se); % close operation

Subplots (3, 2, 3), IM Show (I2);

Title ("Open image after calculation");

axis();

Axis open; % display coordinate system

Subplots (3, 2, 4), IM Show (i3);

Title ("image after closing operation");

axis();

Axis open; % display coordinate system

se=strel('disk ', 1);

I4=imopen(I 1,se);

I5=imclose(I4,se);

Subplots (3, 2, 5), IM Show (i5); % switch operation image

Title ("Open-Close Operation Image");

axis();

Axis open; % display coordinate system

I6=imclose(I 1,se);

I7=imopen(I6,se);

Subplots (3, 2, 6), IM Show (i7); % switch operation image

Title ("Close-Open Operation Image");

axis();

Axis open; % display coordinate system

19. Morphological boundary extraction

Use MATLAB to realize the following functions:

I = im read(' xian . BMP '); % load image

Subplots (1, 3, 1), im show (i);

Title ("original");

axis();

Grid open; % show gridlines

Axis open; % display coordinate system

I 1 = im2bw(I);

Subplot (1, 3, 2), im show (I1);

Title ("binary image");

axis();

Grid open; % show gridlines

Axis open; % display coordinate system

I2 = bwperim(I 1); % Get the perimeter of the area

Subplots (1, 3, 3), im show (I2);

Title ("Binary image of boundary perimeter");

axis();

Grid open;

Axis open;

20. Morphological skeleton extraction

Use MATLAB to realize the following functions:

I = im read(' xian . BMP ');

Subplots (2,2, 1), im show (i);

Title ("original");

axis();

Axis open;

I 1 = im2bw(I);

Subplots (2, 2, 2), im show (I1);

Title ("binary image");

axis();

Axis open;

I2=bwmorph(I 1,' skel ', 1);

Subplots (2, 2, 3), IM Show (I2);

Title ('1 skeleton extraction');

axis();

Axis open;

I3=bwmorph(I 1,' skel ',2);

Subplots (2, 2, 4), IM Show (i3);

Title ("Secondary Skeleton Extraction");

axis();

Axis open;

2 1. Directly extract four vertex coordinates.

I = im read(' xian . BMP ');

I = I(:,:, 1);

BW = im2bw(I);

I guessed.

imshow(~BW)

[x,y]=getpts

Smoothing filtering

h=fspecial('average ',9);

I_gray=imfilter(I_gray,h,' replicate '); % smooth filtering