Binary morphology--corrosion and expansion and C language code implementation

Source: Internet
Author: User
Tags reflection

Reference: Digital Image Processing (third edition) He Dongjian Xidian University Publishing house

The operands in binary morphology are sets , but in the actual operation, when two sets are involved, they are not considered to be equal to each other. General set A is the image set, S is the structural element, the mathematical morphology operation is using S to a. The structure element itself is also an image collection, although it is usually much smaller than the target image. You can specify an origin for a structure element as a reference point for a structural element to participate in the morphological operation. The origin can be contained within a structure element or not in a structure element, but the results of the operation are often different. The following dots represent areas with a value of 1, and the white point represents an area with a value of 0, and the operation is for a region with a value of 1.

1. Corrosion

Corrosion is one of the most basic mathematical morphology operations. For the given target image x and the structure element s, move s on the image, then at each current position x, S+x has only 3 possible states, as shown in:

The case of (1) indicates that s+x is related to x;

The case of paragraph (2) indicates that s+x is not related to X;

The case of paragraph (3) shows that s+x is only partially related to X.

thus satisfies (1) The whole element of the point x, called the point set for S to x corrosion (for short, corrosion, also called X with S corrosion), recorded as.

Corrosion can also be defined in the form of a set:

This formula indicates that X's corrosion with s results in the collection of all x that is still in X after the S is shifted by X. In other words, the set that is used to corrode x with S is the set of the origin of S where S is completely contained in X.

The function of corrosion in mathematical morphology operation is to eliminate the object boundary point, remove the object less than the structure element, and clear the small connectivity between two objects . If the structure element takes a 3x3 block of pixels, the corrosion will reduce the boundary of the object by 1 pixels along the perimeter.

"Corrosion" diagram: (corrosion reduces the image (area))

Code implementation:

"Note" Two-value corrosion basic operation, the background is black, the target is white.

1 //Two value corrosion2 /*function Parameters:3 a--images to be corroded4 results of corrosion after b--5 mat[5][5]--structure element, I set the size of 5*5 here by default6 */7 voidBi_corrosion (Mat &a, Mat &b,intmat[5][5])8 {9     intI, J, K, O;Ten     introws =a.rows; One     intcols = a.cols*a.channels (); A  -     BOOLFlag; -  theUchar *DST =B.data; -Uchar *SRC =A.data; -     //for each pixel position in the image, determine if the structure element can be filled into the target interior -      for(i =2; I < rows-2; i++) { +          for(j =2; J < cols-2; J + +) {         -             //determines whether the structure element can be filled into the target interior at the current point, 1 is yes, 0 is not possible +Flag =1; A              for(k =-2; K <=2&& Flag; k++) { at                  for(o =-2; o <=2; o++) { -                     //if the current structure element position is 1, determine if the pixel on the corresponding image is not 0 . -                     if(mat[k+2][o+2]) { -                         //if the image current pixel is 0, the point is not hit, not the corroded output -                         if(!* (src+ (i+k) *cols+j+o)) { -Flag =0; Break; in                         } -                     } to                 } +             } -* (DST+I*COLS+J) = flag?255:0; the         } *     } $}
View Code

2. Expansion

Corrosion can be thought of as the contraction of each subset of the image x that is congruent with the structure element s s+x to point X. Conversely, each point x in X can be expanded to s+x, that is, the expansion operation, recorded as. The definition of expansion operations in the set language is:

Icon:

"Attention" to look at the special case: After the expansion of B, the results shifted to pan left, while the reflection of the B image expands position unchanged.

  

For the asymmetric structure s, the expansion will cause the original image to be shifted, but the expansion will not, the total position and shape unchanged, so the expansion formula can also be written:

The expansion of the set X can also be seen as a complement to the corrosion of a set of X-complements with dual characteristics :

The relationship between corrosion and expansion operations and set operations is as follows:

Code implementation:

"Note" The two-value expansion basic operation, the background is black, the target is white.

1 //Two value expansion2 /*function Parameters:3 a--images to be corroded4 results of corrosion after b--5 mat--structural elements6 */7 voidBi_expansion (Mat &a, Mat &b,intmat[5][5]) {8     intI, J, K, O;9     introws =a.rows;Ten     intcols = a.cols*a.channels (); OneMat tmp =A.clone (); Auchar* src =Tmp.data; -     //expansion is the corrosion of the target complement set in the image, so the complement of the input image data is first obtained. -      for(i =0; i < rows; i++) the          for(j =0; J < cols; J + +) -* (SRC+I*COLS+J) =255-* (src+i*cols+j); -     //The expansion is the corrosion of the symmetric set of the structural element to the complement set, and the reflection is obtained here -      for(i =0; I <5; i++) +          for(j =0; J <= I; J + +) -MAT[I][J] =Mat[j][i]; +     BOOLFlag; Auchar* DST =B.data; at     //for each pixel position in the image, determine if the structure element can be filled into the target interior -      for(i =2; I < rows-2; i++) { -          for(j =2; J < cols-2; J + +) {         -             //determines whether the structure element can be filled into the target interior at the current point, 1 is yes, 0 is not possible -Flag =1; -              for(k =-2; K <=2&& Flag; k++) { in                  for(o =-2; o <=2; o++) { -                     //if the current structure element position is 1, determine if the pixel on the corresponding image is not 0 . to                     if(mat[k+2][o+2]) { +                         if(!* (src+ (i+k) *cols+j+o)) {//not hit . -Flag =0; Break; the                         } *                     } $                 }Panax Notoginseng             } -* (DST+I*COLS+J) = flag?255:0; the         } +     } A     //after the corrosion of the target complement set by the symmetric set of structural elements, it is also needed to make a supplementary set for the structure, which is the output of the expansion structure. the     //An assignment structure element corrodes The missing area, restoring the original image to a two-value image +      for(i =0; i < rows; i++) { -          for(j =0; J < cols; J + +) { $* (DST+I*COLS+J) =255-* (dst+i*cols+j); $             if(* (dst+i*cols+j)! =255&& * (dst+i*cols+j)! =0) -* (DST+I*COLS+J) =0; -         } the     } -}
View Code

Binary morphology--corrosion and expansion and C language code implementation

Related Article

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.