Image morphology

Posted by webtech123 on Sun, 16 Jan 2022 12:17:59 +0100

//Image morphology
void COpenCVTestDlg::OnCbnSelchangeComboMorphology()
{
       if (m_comboMorphology.GetCurSel() == -1)
              return;
       if (!m_srcMatImg.empty())
       {
              //Define type
              int MorphologyType = MORPH_RECT;
              //Define size
              int MorphlogySize = 3;
              //Call the getStructuringElement function
              Mat element = getStructuringElement(MorphologyType,
                     Size(2 * MorphlogySize + 1, 2 * MorphlogySize + 1),
                     Point(MorphlogySize, MorphlogySize));
              CString szMorphology;
              m_comboMorphology.GetLBText(m_comboMorphology.GetCurSel(), szMorphology);
              if (szMorphology == L"corrosion")
              {
                     //Call erode
                     erode(m_srcMatImg, m_dstMatImg, element);
              }
              else if (szMorphology == L"expand")
              {
                     //Call dilate function
                     dilate(m_srcMatImg, m_dstMatImg, element);
              }
              else if (szMorphology == L"Open operation")
              {
                     //Call the morphologyEx function - morph_ Open - > open operation
                     morphologyEx(m_srcMatImg, m_dstMatImg, MORPH_OPEN, element);
              }
              else if (szMorphology == L"Closed operation")
              {
                     //Call the morphologyEx function - morph_ Close - > close operation
                     morphologyEx(m_srcMatImg, m_dstMatImg, MORPH_CLOSE, element);
              }
              else if (szMorphology == L"Grandient operation")
              {
                     //Call the morphologyEx function - morph_ Gradient - > gradient operation
                     morphologyEx(m_srcMatImg, m_dstMatImg, MORPH_GRADIENT, element);
              }
              else if (szMorphology == L"Top hat transformation")
              {
                     //Call the morphologyEx function - morph_ Tophat - > Top Hat transformation
                     morphologyEx(m_srcMatImg, m_dstMatImg, MORPH_TOPHAT, element);
              }
              else if (szMorphology == L"Black hat transformation")
              {
                     //Call the morphologyEx function - morph_ Blackhat - > black hat transformation
                     morphologyEx(m_srcMatImg, m_dstMatImg, MORPH_BLACKHAT, element);
              }
       }
}

Morphology, or mathematical morphology, is a very important research direction in the process of image processing

Morphology mainly extracts its component information from the image. This component information is usually of great significance for expressing and describing the shape of the image. It is usually the most essential shape feature used in image understanding

This part mainly includes: corrosion, expansion, open operation, close operation, morphological gradient, top cap, black cap and other operations

Corrode

Corrosion can eliminate the boundary points of the image, shrink the image inward along the boundary, or remove the parts smaller than the specified structural elements

void erode( InputArray src, OutputArray dst, InputArray kernel,
          Point anchor = Point(-1,-1
          int borderType = BORDER_CONSTANT,
          const Scalar& borderValue = morphologyDefaultBorderValue() );

src: the original image to be corroded, and the depth of the image must be CV_8U,CV_16U,CV_16S,CV_32F,CV_64F one of them

dst: target image output after corrosion

kernel: the type of structure used in corrosion operation

Anchor: the position of the anchor point in the element structure. The default value is (- 1, - 1), which is in the center of the core

Iterations: number of iterations of corrosion operation

borderType: method of image boundary processing

borderValue: boundary value, which is generally the default value

Where morphologyDefaultBorderValue() is used to return the "magic" boundary value of corrosion and expansion.

It is usually automatically converted to Scalar::all(-DBL_MAX) for expansion

The kernel in its parameters can be obtained through getStructuringElement

Mat getStructuringElement(int shape, Size ksize, Point anchor = Point(-1,-1));

 

This function returns a structural element of a specified size and shape for morphological operations

Shape: CV:: any element shape in morphshapes

With MORPH_RECT,MORPH_CROSS,MORPH_ Three kinds of ellipse

ksize: size of structure element

Anchor: anchor position in the structure element. The default value is (- 1, - 1), which is the of the shape. Only the cross star shape is closely related to the anchor position.

In other cases, the anchor point is only used for the adjustment of morphological operation results

 //Call the getStructuringElement function
   Mat element = getStructuringElement(MORPH_CROSS,Size(2 * 3+ 1, 2 * 3+ 1),Point(3, 3));
   //Call erode
   erode(m_srcImage, m_dstImage, element);

Swell

Expansion expands the boundary of the image, merges the background points in contact with the current object into the image, and expands the boundary points to the outside.

If the two objects in the image are close, the two objects may be connected together during the expansion process.

The expansion operation is very helpful to fill the blank in the image after image segmentation

 void dilate( InputArray src, OutputArray dst, InputArray kernel,
              Point anchor = Point(-1,-1
              int borderType = BORDER_CONSTANT,
              const Scalar& borderValue = morphologyDefaultBorderValue() );

src: the original image to be expanded, and the depth of the image must be CV_8U,CV_16U,CV_16S,CV_32F,CV_64F one of them

dst: the target image output after being expanded

kernel: the type of structure used in corrosion operation

Anchor: the position of the anchor point in the element structure. The default value is (- 1, - 1), which is in the center of the core

Iterations: the number of iterations of the expansion operation

borderType: method of image boundary processing

//Call the getStructuringElement function
     Mat element = getStructuringElement(MORPH_CROSS,Size(2 * 3+ 1, 2 * 3 + 1),Point(3, 3));
     //Call dilate function
     dilate(m_srcImage, m_dstImage, element);
Open operation 
MORPH_RECT MORPH_CROSS MORPH_ELLIPSE getStructuringElement morphologyEx MORPH_OPEN
 Closed operation 
MORPH_RECT MORPH_CROSS MORPH_ELLIPSE getStructuringElement morphologyEx MORPH_CLOSE
Morphological Grandient 
MORPH_RECT MORPH_CROSS MORPH_ELLIPSE getStructuringElement morphologyEx MORPH_GRADIENT

Top cap 
MORPH_RECT MORPH_CROSS MORPH_ELLIPSE getStructuringElement morphologyEx MORPH_TOPHAT
 Black hat 
MORPH_RECT MORPH_CROSS MORPH_ELLIPSE getStructuringElement morphologyEx MORPH_BLACKHAT
        void morphologyEx( InputArray src, OutputArray dst,
                                int op, InputArray kernel,
                                Point anchor = Point(-1,-1), int iterations = 1,
                                int borderType = BORDER_CONSTANT,
                                const Scalar& borderValue = morphologyDefaultBorderValue() );

src: the original image requiring morphology, and the depth of the image must be CV_8U,CV_16U,CV_16S,CV_32F,CV_64F one of them

dst: the target image output after morphological processing

op: operation type. The operation rules of various morphological operations are obtained by combining corrosion and expansion

The parameters kernel, anchor, borderType and borderValue have the same meaning as the erode function

 

 //Call the getStructuringElement function
   Mat element = getStructuringElement(MORPH_CROSS,Size(2 * 3+ 1, 2 * 3+ 1),Point(3, 3));
   //Call the morphologyEx function - morph_ Open - > open operation
   morphologyEx(m_srcImage, m_dstImage, MORPH_OPEN, element);

Original drawing

corrosion

expand

Open operation

Closed operation

morphology

Top cap

Black hat

Welcome to the official account "Qt vision".

 

Topics: C++ OpenCV Computer Vision