opencv----Corrosion Expansion Open-Close Operation

Corrosion and expansion are the most basic morphological operations.

Corrosion and expansion are for white parts (highlighted parts).

Expansion means "realm expansion" of the highlighted part of the image. The effect image has a larger highlighted area than the original image. Corrosion means that the highlighted area in the original image is eaten up, and the effect image has a smaller highlighted area than the original image.

expand

Expansion is the operation of finding the local maximum value. Visually, it is to enlarge the bright part and reduce the dark part of the image.

#include<opencv2\opencv.hpp>   
#include<opencv2\highgui\highgui.hpp>

using namespace std;
using namespace cv;

//expand
int main()
{
    Mat img = imread("lol1.jpg");
    namedWindow("Original Map", WINDOW_NORMAL);
    imshow("Original Map", img);
    Mat out;
    //Get Custom Kernel
    Mat element = getStructuringElement(MORPH_RECT, Size(15, 15)); //The first parameter, MORPH_RECT, represents the convolution kernel of a rectangle, but you can also choose elliptical, crossed
    //Expansion operation
    dilate(img, out, element);
    namedWindow("Expansion operation", WINDOW_NORMAL);
    imshow("Expansion operation", out);
    waitKey(0);

}

You can see that the original bright part of the image is enlarged and the dark part is reduced.

corrosion

#include<opencv2\opencv.hpp>   
#include<opencv2\highgui\highgui.hpp>

using namespace std;
using namespace cv;

//corrosion
int main()
{
    Mat img = imread("lol1.jpg");
    namedWindow("Original Map", WINDOW_NORMAL);
    imshow("Original Map", img);
    Mat out;
    //Get Custom Kernel
    Mat element = getStructuringElement(MORPH_RECT, Size(15, 15)); //The first parameter, MORPH_RECT, represents the convolution kernel of a rectangle, but you can also choose elliptical, crossed
    //Corrosion Operation
    erode(img, out, element);
    namedWindow("Corrosion Operation", WINDOW_NORMAL);
    imshow("Corrosion Operation", out);
    waitKey(0);

}

You can see that the dark part of the image is enlarged and the bright part is reduced.

Opening operation: Corrosion and expansion to eliminate small objects

Closed operation: expands before corroding to exclude small black holes

Morphological gradient: The difference between the dilation chart and the top view used to preserve the outline of the edge of an object.

Cap: The difference between the original image and the open operation diagram used to separate patches that are lighter than adjacent patches.

Black Cap: The difference between the closed operation and the original image used to separate patches that are darker than neighboring points.

opencv has a good function getStructuringElement, we just need to pass the corresponding processing parameters to this function, then we can do the corresponding operation, it is very convenient to use.

The following lists the corresponding operation macro definitions.

#include<opencv2\opencv.hpp>   
#include<opencv2\highgui\highgui.hpp>

using namespace std;
using namespace cv;

//Advanced Morphological Processing
int main()
{
    Mat img = imread("lol1.jpg");
    namedWindow("Original Map", WINDOW_NORMAL);
    imshow("Original Map", img);
    Mat out;
    //Get Custom Kernel
    Mat element = getStructuringElement(MORPH_RECT, Size(15, 15)); //The first parameter, MORPH_RECT, represents the convolution kernel of a rectangle, but you can also choose elliptical, crossed
        
    //Advanced morphological processing, call this function is OK, specific operation to choose, modify the third parameter is OK.The morphological gradient processing is demonstrated here
    morphologyEx(img, out, MORPH_GRADIENT, element);
    namedWindow("Morphological Processing Operation", WINDOW_NORMAL);
    imshow("Morphological Processing Operation", out);
    waitKey(0);

}

Morphological gradient treatment

This is the result of the cap operation

Tags: OpenCV

Posted on Sat, 01 Feb 2020 12:08:13 -0500 by Giddy Rob