Morphological Operation implementation

Source: Internet
Author: User
Tags strcmp

The basic idea of mathematical morphology is to measure and extract the corresponding shapes in the image with certain morphological elements to achieve the purpose of image analysis and recognition. There are four basic operations for mathematical morphology: corrosion, expansion, opening and closing. Based on these basic operations, it can be deduced and combined into practical algorithms of mathematical morphology. In this experiment, we implemented four morphological operations for binary image and grayscale image respectively.

One or two-value image corrosion, expansion, opening and closing operations

Experimental results:

Second, the gray image of the corrosion, expansion, open, closed operation

Experimental results:

Code: (Download link)

%This experiment completes the corrosion, expansion, opening and closing operation of the two-value image and the grayscale image close All;clear all;grayi= Imread ('Rice.png'); Bini=IM2BW (Grayi,0.5);%ext. Two value graph SE= [0,1,0;1,1,1;0,1,0]; %Structural element Binierode= Myimerode (Bini,se,'binary'); Binidilate= Myimdilate (Bini,se,'binary'); Biniopen= Myimopen (Bini,se,'binary'); Biniclose= Myimclose (Bini,se,'binary'); Grayierode= Myimerode (Grayi,se,'Gray'); Grayidilate= Myimdilate (Grayi,se,'Gray'); Grayiopen= Myimopen (Grayi,se,'Gray'); Grayiclose= Myimclose (Grayi,se,'Gray'); Figure;subplot (2,3,1); Imshow (Bini); Title ('Original binary Graph'); subplot (2,3,2); Imshow (Binierode); Title ('Corrosion Results'); subplot (2,3,3); Imshow (binidilate); Title ('Swelling Results'); subplot (2,3,4); Imshow (Biniopen); Title ('Open Result'); subplot (2,3,5); Imshow (Biniclose); Title ('closed Results') Figure;subplot (2,3,1); Imshow (grayi); Title ('Original grayscale Image'); subplot (2,3,2); Imshow (Uint8 (Grayierode)); Title ('Corrosion Results'); subplot (2,3,3); Imshow (Uint8 (grayidilate)); Title ('Swelling Results'); subplot (2,3,4); Imshow (Uint8 (Grayiopen)); Title ('Open Result'); subplot (2,3,5); Imshow (Uint8 (Grayiclose)); Title ('closed Results'); 
View Code
function o=myimclose (i,se,type)percent closed operation %I: Input image (binary or grayscale)%se: structural element %type: Image type (gray, binary)%o: closed operation result o=myimerode (myimdilate (I,se,type), se,type);
View Code
function o=myimopen (i,se,type)% open operation %I: Input image (binary or grayscale)%se: structural element %Type: Image Type (gray, binary)%o: Open operation result o=myimdilate (Myimerode (I,se,type), se,type);
View Code
function o=Myimerode (I,se,type)%%Corrosion Operation%I: Input image (binary or grayscale)%se: structural element%Type : Image types (gray, binary)%o: Corrosion operation result O=Zeros (Size (I)); [R,c]=size (I); [M,n]=size (SE); LM=floor (M-1)/2); Ln=floor ((n1)/2); RM=m-lm-1; RN=n-ln-1; ORGM; l++1;%structure Primitive Origin orgn=ln+1;if(strcmp (Type,'binary'))%Two value image forI=1: R forj=1: C INDEXR=max (1, i-lm): Min (R,I+RM);Boundary processing Indexc=max (1, j-ln): Min (c,j+RN); if(SUM (SUM (SE (INDEXR-I+ORGM,INDEXC-J+ORGN) &i (INDEXR,INDEXC))) ==sum (SUM (SE (indexr-i+orgm,indexc-j+ORGN))))  O (I,J)=1; End End EndEndif(strcmp (Type,'Gray'))%Grayscale Image I=Double(I);  forI=1: R forj=1: C INDEXR=max (1, i-lm): Min (R,I+RM);Boundary processing Indexc=max (1, j-ln): Min (c,j+RN); O (I,J)=min (Min (I (indexr,indexc)-se (indexr-i+orgm,indexc-j+( orgn))); End EndEnd
View Code
function o=myimdilate (I,se,type)%%expansion Operation%I: Input image (binary or grayscale)%se: structural element%Type : Image types (gray, binary)%O: Expansion operation result SE=reflect (SE);structural element Reflection O=Zeros (Size (I)); [R,c]=size (I); [M,n]=size (SE); LM=floor (m/2);%difference from Erodeln=floor (n/2); RM=m-lm-1; RN=n-ln-1; ORGM; l++1;%structure Primitive Origin orgn=ln+1;if(strcmp (Type,'binary'))%Two value image forI=1: R forj=1: C INDEXR=max (1, i-lm): Min (R,I+RM);Boundary processing Indexc=max (1, j-ln): Min (c,j+RN); if(SUM (SUM (SE (INDEXR-I+ORGM,INDEXC-J+ORGN) &i (INDEXR,INDEXC))) >=1) O (i,j)=1; End End EndEndif(strcmp (Type,'Gray'))%Grayscale Image I=Double(I);  forI=1: R forj=1: C INDEXR=max (1, i-lm): Min (R,I+RM);Boundary processing Indexc=max (1, j-ln): Min (c,j+RN); O (I,J)=max (Max (I (INDEXR,INDEXC) +se (indexr-i+orgm,indexc-j+( orgn))); End Endendfunction Newse=reflect (SE)%%structural element Reflection operations%SE: Input structure element%Newse: Post-reflective structural element [M,n]=size (SE); Newse=zeros (m,n); forI=1: M forj=1: N newse (i,j)=se (m+1-i,n+1-j); EndEnd
View Code

Morphological Operation implementation

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.