Image segmentation based algorithm and implementation example

Source: Internet
Author: User

Recent projects involve the field of image processing, a small study, and the collection of data to achieve several basic functions.

First, image inversion

I=imread (' input_image.jpg '); J=double (I); j=-j+ (256-1); % image inversion Linear transform h=uint8 (J); subplot (3,3,4), Imshow (H); title (' Image reversal linear transformation '); axis ([50,250,50,200]); axis on;

TwoGray-scale linear transformation

I=imread (' input_image.jpg '); subplot (3,3,1), imshow (i); title (' Original image '); axis ([50,250,50,200]); axis on;i1 = Rgb2gray (i) ; subplot (3,3,2), Imshow (I1) title (' Grayscale image ') axis ([50,250,50,200]); grid on;axis on; K=imadjust (i1,[0.3 0.7],[]); subplot (3,3,3), Imshow (K); title (' Linear transform image [0.3 0.7] '); axis ([50,250,20,200]); Grid On;axis On
ThreeNonlinear Transformations

I=imread (' input_image.jpg '); I1 = Rgb2gray (I); subplot (3,3,5), Imshow (I1), title (' Grayscale image '), axis ([50,250,50,200]); grid On % display grid line axis on; % Display coordinate system j=double (I1); j=40* (log (j+1)); H=uint8 (J); subplot (3,3,6), Imshow (H); title (' Logarithmic transform image '); axis ([50,250,50,200]); grid on; % display grid line axis on; % Display coordinate system

The above code results:


Four, histogram equalization

I=imread (' input_image.jpg '); Figure;i=rgb2gray (i); subplot (2,2,1); imshow (i); Subplot (2,2,2); imhist (I); title (' Histogram Equalization of images '); I1 = Histeq (I); subplot (2,2,3); Imshow (I1); subplot (2,2,4); imhist (I1);

The above code results:


Five, linear smoothing filter
I=imread (' input_image.jpg '); Figure;subplot (231) imshow (i) title (' Original image ') I=rgb2gray (i); I1=imnoise (i, ' Salt & Pepper ', 0.02); subplot (232) Imshow (I1) title (' Add salt and pepper noise image ') K1=filter2 (fspecial (' Average ', 3), I1)/255; % for 3*3 Template Smoothing Filter k2=filter2 (fspecial (' Average ', 5), I1)/255; % for 5*5 Template Smoothing Filter k3=filter2 (fspecial (' average ', 7), I1)/255; % for 7*7 Template Smoothing Filter k4=filter2 (fspecial (' average ', 9), I1)/255; % for 9*9 template Smoothing filter subplot (233), imshow (K1), title (' 3*3 Template Smoothing filter '), subplot (234), Imshow (K2); title (' 5*5 Template smoothing filter '); subplot (235) , Imshow (K3), title (' 7*7 Template Smoothing filter '), subplot (236), Imshow (K4), title (' 9*9 Template smoothing filter ');

The above code results:


VI. Median filter

Figure;i=imread (' input_image.jpg '); I=rgb2gray (i); subplot (231), imshow (i); title (' Original image '); J=imnoise (I, ' salt & Pepper ', 0.02); subplot (232), Imshow (J); title (' Add salt and pepper noise image '); K1=MEDFILT2 (J); % for 3*3 template median filter k2=medfilt2 (j,[5,5]); % for 5*5 template median filter k3=medfilt2 (j,[7,7]); % for 7*7 template median filter k4=medfilt2 (j,[9,9]); % for 9*9 template median filter subplot (233), imshow (K1), title (' 3*3 template median filter '), subplot (234), Imshow (K2); title (' 5*5 template median filter '); subplot (235) , Imshow (K3), title (' 7*7 template median filter '), subplot (236), imshow (K4); title (' 9*9 template median filter ');

The above code results:


Vii. Use ofSobeloperator and Laplace on image sharpening

Figure;i=imread (' input_image.jpg '); subplot (2,2,1), Imshow (I); title (' Original image '); axis ([50,250,50,200]); grid on; % display grid line axis on; % Display coordinate system I1=IM2BW (I); subplot (2,2,2), Imshow (I1); title (' Binary image '); axis ([50,250,50,200]); grid on; % display grid line axis on; % Display coordinate system h=fspecial (' Sobel '); % Select Sobel operator j=filter2 (H,I1); % convolution operation subplot (2,2,3), Imshow (J); title (' Sobel operator sharpening image '); axis ([50,250,50,200]); grid on; % display grid line axis on; % display coordinate system I1 = double (I1); h=[0 1 0,1-4 1,0 1 0]; % Laplace operator J1=conv2 (i1,h, ' same '); % convolution operation subplot (2,2,4), Imshow (J1), title (' Laplace operator sharpening image '), axis ([50,250,50,200]); grid on; % display grid line axis on; % Display coordinate system

The above code results:


Eightgradient operator detects edges

Figure;i=imread (' input_image.jpg '); subplot (2,3,1); Imshow (I); title (' Original image '); axis ([50,250,50,200]); grid on; % display grid line axis on; % Display coordinate system I1=IM2BW (I); subplot (2,3,2); Imshow (I1); title (' Binary image '); axis ([50,250,50,200]); grid on; % display grid line axis on; % Display coordinate system I2=edge (I1, ' Roberts '); Subplot (2,3,3); Imshow (I2); title (' Roberts operator segmentation result '); axis ([50,250,50,200]); grid on; % display grid line axis on; % Display coordinate system I3=edge (I1, ' Sobel '); subplot (2,3,4); Imshow (I3); title (' Sobel operator segmentation result '); axis ([50,250,50,200]); grid on; % display grid line axis on; % Display coordinate system I4=edge (I1, ' Prewitt '); subplot (2,3,5); Imshow (I4); title (' Prewitt operator segmentation result '); axis ([50,250,50,200]); grid on; % display grid line axis on; % Display coordinate system
NineLOGoperator Detection Edge
I1=rgb2gray (I); I2=edge (I1, ' log '), subplot (2,3,6); Imshow (I2); title (' Log operator segmentation result ');

The above code results:


TenCannyoperator Detection Edge
Figure;i=imread (' input_image.jpg '); subplot (2,2,1); imshow (i); title (' Original image ') I1=rgb2gray (i); subplot (2,2,2); Imshow ( I1); title (' Grayscale image '); I2=edge (I1, ' canny '); subplot (2,2,3); Imshow (I2); title (' Canny operator segmentation result ');

The above code results:


ElevenBoundary Tracking (bwtraceboundaryfunction)

I=imread (' input_image.jpg '); Figuresubplot (2,2,1); Imshow (I); title (' Original image '); I1=rgb2gray (I); % Convert color image to grayscale image Threshold=graythresh (I1); % calculates the threshold BW=IM2BW (I1, threshold) required to convert a grayscale image to a two-value image; % convert grayscale image to two value image subplot (2,2,2); Imshow (BW); title (' Binary image '); Dim=size (BW); Col=round (Dim (2)/2)-90; % calculates the starting point column coordinates row=find (BW (:, col), 1); % calculates the starting point line coordinates connectivity=8;num_points=180;contour=bwtraceboundary (Bw,[row,col], ' N ', connectivity,num_points); Extract boundary subplot (2,2,3); Imshow (I1); hold On;plot (Contour (:, 2), Contour (:, 1), ' G ', ' linewidth ', 2); title (' Boundary tracking image ');

The above code results:


TwelveHoughTransformations

Figure;i=imread (' input_image.jpg '), Roti=rgb2gray (I), subplot (2,2,1), Imshow (RotI), title (' Grayscale image '), Axis ([ 50,250,50,200]); grid on;axis on; Bw=edge (RotI, ' Prewitt '); subplot (2,2,2); Imshow (BW); title (' Prewitt operator edge detection image '); axis ([50,250,50,200]); Grid On;axis On [H,t,r]=hough (BW); subplot (2,2,3); Imshow (h,[], ' XData ', T, ' Ydata ', R, ' initialmagnification ', ' fit '); title (' Hough Transform graph '); Xlabel (' \theta '), Ylabel (' \rho '); axis on, Axis normal, hold on; P=houghpeaks (h,5, ' threshold ', ceil (0.3*max (H (:)))), X=t (P (:, 2)), Y=r (P (:, 1));p lot (x, y, ' s ', ' color ', ' white '); lines= Houghlines (bw,t,r,p, ' Fillgap ', 5, ' MinLength ', 7), subplot (2,2,4), Imshow (RotI), title (' Hough Transform image detection '), Axis ([ 50,250,50,200]); grid On;axis On;hold on;max_len=0;for k=1:length (lines) xy=[lines (k). Point1;lines (k). Point2];p Lot ( XY (:, 1), XY (:, 2), ' LineWidth ', 2, ' Color ', ' Green ';p lot (xy (), XY (+), ' x ', ' LineWidth ', 2, ' Color ', ' yellow ');p lot ( XY (2,1), XY (2,2), ' x ', ' LineWidth ', 2, ' Color ', ' Red '), Len=norm (lines (k). Point1-lines (k). Point2); if (Len>max_len) Max_len=len;xy_long=xy;endendplot (Xy_long(:, 1), Xy_long (:, 2), ' LineWidth ', 2, ' Color ', ' cyan '); 

The above code results:


13.Histogram threshold Value method

Figure;i=imread (' input_image.jpg '); I1=rgb2gray (I); subplot (2,2,1); Imshow (I1); title (' Grayscale image ') axis ([50,250,50,200]) ; grid on; % display grid line axis on; % display coordinate system [M,n]=size (I1); % measurement image size parameter Gp=zeros (1,256); % pre-created vector for K=0:255GP (k+1) =length (Find (I1==k))/(M*n) that holds the probability of grayscale occurrence; % calculates the probability of each level of grayscale appearing, depositing it into the corresponding position in GP Endsubplot (2,2,2), bar (0:255,GP, ' g ')% plotting histogram title (' Grayscale histogram ') xlabel (' grayscale value ') ylabel (' probability of occurrence ') i2= IM2BW (i,150/255); subplot (2,2,3), Imshow (I2); title (' Segmented image of threshold 150 ') axis ([50,250,50,200]); grid on; % display grid line axis on; % Display coordinate system I3=IM2BW (i,200/255); %subplot (2,2,4), Imshow (I3); title (' Segmented image of threshold 200 ') axis ([50,250,50,200]); grid on; % display grid line axis on; % Display coordinate system

The above code results:


14.Automatic threshold Method:OtsuMethod

Clcclear allfigure;i=imread (' input_image.jpg '); subplot (1,2,1), Imshow (I); title (' Original image ') axis ([50,250,50,200]); grid On % display grid line axis on; % Display coordinate system Level=graythresh (I); % determines the grayscale threshold BW=IM2BW (I,level), subplot (1,2,2), Imshow (BW), title (' Otsu method threshold segmentation image ') axis ([50,250,50,200]), grid on; % display grid line axis on; % Display coordinate system

The above code results:


xvswellingOperation

Figure;i=imread (' input_image.jpg '); I1=rgb2gray (I); subplot (1,2,1); Imshow (I1); title (' Grayscale image ') axis ([50,250,50,200]) ; grid on; % display grid line axis on; % Display coordinate system Se=strel (' disk ', 1); % generated circular structural elements i2=imdilate (I1,SE); % expands the image with the generated structural elements subplot (1,2,2); Imshow (I2); title (' Expanded image '); axis ([50,250,50,200]); grid on; % display grid line axis on; % Display coordinate system

The above code results:


16.Corrosion Operation

Figure;i=imread (' input_image.jpg '); I1=rgb2gray (I); subplot (1,2,1); Imshow (I1); title (' Grayscale image ') axis ([50,250,50,200]) ; grid on; % display grid line axis on; % Display coordinate system Se=strel (' disk ', 1); % generated circular structural elements i2=imerode (I1,SE); % uses the resulting structural elements to corrode the image subplot (1,2,2); Imshow (I2); title (' Corroded image '); axis ([50,250,50,200]); grid on; % display grid line axis on; % Display coordinate system

The above code results:


17.opening and closing operations

Figure;i=imread (' input_image.jpg '); subplot (2,2,1), Imshow (I); title (' Original image '); axis ([50,250,50,200]); axis on; % Display coordinate system I1=rgb2gray (I); subplot (2,2,2), Imshow (I1); title (' Grayscale image '); axis ([50,250,50,200]); axis on; % Display coordinate system Se=strel (' disk ', 1); % using a circle with a radius of 1 as the structural element I2=imopen (I1,SE); % Open Operation I3=imclose (I1,SE); % closed operation subplot (2,2,3), Imshow (I2), Title (' Open op image '), axis ([50,250,50,200]); % display coordinate system subplot (2,2,4), Imshow (I3), title (' Closed Operation image '), axis ([50,250,50,200]); % Display coordinate system

The above code results:


18.opening and closing combination operations
Figure;i=imread (' input_image.jpg '); subplot (3,2,1), Imshow (I); title (' Original image '); axis ([50,250,50,200]); axis on; % Display coordinate system I1=rgb2gray (I); subplot (3,2,2), Imshow (I1); title (' Grayscale image '); axis ([50,250,50,200]); axis on; % Display coordinate system Se=strel (' disk ', 1); I2=imopen (I1,se); % Open Operation I3=imclose (I1,SE); % closed operation subplot (3,2,3), Imshow (I2), Title (' Open op image '), axis ([50,250,50,200]); % display coordinate system subplot (3,2,4), Imshow (I3), title (' Closed Operation image '), axis ([50,250,50,200]); % Display coordinate system Se=strel (' disk ', 1); I4=imopen (I1,se); I5=imclose (I4,se); subplot (3,2,5), Imshow (I5); % open-Close operation Image title (' Open-closed Operation image '); axis ([50,250,50,200]); axis on; % Display coordinate system i6=imclose (I1,SE); I7=imopen (I6,se); subplot (3,2,6), Imshow (I7); % closed-Open Operation Image title (' Closed-open operation Image '); axis ([50,250,50,200]); axis on; % Display coordinate system

The above code results:


NineteenMorphological Boundary Extraction
Figure;i=imread (' input_image.jpg '); subplot (2,3,1), Imshow (I); title (' Original image '); axis ([50,250,50,200]); grid on; % display grid line axis on; % Display coordinate system I1=IM2BW (I); subplot (2,3,2), Imshow (I1); title (' Binary image '); axis ([50,250,50,200]); grid on; % display grid line axis on; % Display coordinate system I2=bwperim (I1); % gets the perimeter of the zone subplot (2,3,3), Imshow (I2); title (' Two-value image of the perimeter of the boundary '); axis ([50,250,50,200]); grid on;axis on; I3=bwmorph (I1, ' Skel ', 1); subplot (2,3,4), Imshow (I3); title (' 1-time skeleton extraction '); axis ([50,250,50,200]); axis on; I4=bwmorph (I1, ' Skel ', 2); subplot (2,3,5), Imshow (I4); title (' 2-time skeleton extraction '); axis ([50,250,50,200]); axis on;

The above code results:


Image segmentation based algorithm and implementation example

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.