OpenCV morphological operation (open, close, gradient, top hat, black hat)

Basic concepts of morphology

Image morphology, that is, Mathematical morphology, is an image analysis discipline based on Glenn and topology. It is the basic theory of Mathematical morphology image processing; Common image morphological operations: corrosion, expansion, open operation, close operation, skeleton extraction, polar corrosion, hit miss transformation, top hat transformation, particle analysis, watershed transformation, morphological gradient, etc.

Corrosion and expansion are the most basic morphological operations.

Corrosion and expansion are for the white part (highlighted part).

Dilate is to "expand the field" of the highlighted part of the image, and the effect image has a larger highlighted area than the original image;

Erode is that the highlight area in the original image is eroded and the field is reduced. The effect image has smaller highlight area than the original image.

Open operation: first corrosion and then expansion, used to eliminate small objects

Closed operation: expand first and then corrode. It is used to exclude small black holes

Morphological gradient: it is the difference between the expansion map and the top view, which is used to retain the edge contour of the object.

Top hat: the difference between the original image and the operation diagram, which is used to separate some patches lit next to each other.

Black hat: the difference between the closed operation and the original image is used to separate the patches darker than the adjacent points.

Advanced morphological transformation function: MorphologyEx

Function prototype:

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()
)

Parameters:

  • src: source image Mat object

  • dst: target image Mat object

  • op: types of operations. We know from the source code that there are the following types:

    enum MorphTypes{
        MORPH_ERODE    = 0, //corrosion
        MORPH_DILATE   = 1, //expand
        MORPH_OPEN     = 2, //On operation
        MORPH_CLOSE    = 3, //Closed operation
        MORPH_GRADIENT = 4, //Gradient operation
        MORPH_TOPHAT   = 5, //Top hat operation
        MORPH_BLACKHAT = 6, //Black hat operation
        MORPH_HITMISS  = 7  
    };
    
  • kernel: the structure element used for expansion operation. If the value is Mat(), a 3 x 3 square structure element is used by default. You can use getStructuringElement() to create the structure element

  • anchor: reference point, whose default value is (- 1, - 1), indicating that it is located in the center of the kernel.

  • borderType: edge type; the default is BORDER_CONSTANT.

  • borderValue: edge value. Use its default value.

Morphological open operation

The opening operation (corrosion before expansion) can smooth the contour of the object, disconnect narrow discontinuities and eliminate small protrusions.

It has the function of eliminating small objects, separating objects from thin objects and smoothing the boundary of larger objects.

Function usage:

morphologyEx(src, dst, MORPH_OPEN, kernel);

Open operation case:

#include<iostream>
#include<opencv2\opencv.hpp>
#include<opencv2\highgui\highgui.hpp>
using namespace cv;
using namespace std;

int main()
{
	Mat img = imread("./morphologyEx.jpg");
	cout << img.size() << endl;
	imshow("Original drawing", img);

	Mat dst;
	//Get custom core
	Mat element = getStructuringElement(MORPH_RECT, Size(10, 10));
	//Morphological open operation
#if 1
	morphologyEx(img, dst, MORPH_OPEN, element);
#else
	erode(img, dst, element);
	dilate(dst, dst, element);
#endif
	imshow("Morphological open operation", dst);
	waitKey();
	return 0;
}

Note: morphological operations can use the advanced function morphologyEx.

Morphological closed operation

Closed operation (expansion before corrosion) can eliminate narrow discontinuities and small holes. The operation of expansion before corrosion is called closed operation.

It has the function of filling small holes in objects, connecting adjacent objects and smoothing boundaries.

Function usage:

morphologyEx(src, dst, MORPH_CLOSE, kernel);

Closed operation case:

#include<iostream>
#include<opencv2\opencv.hpp>
#include<opencv2\highgui\highgui.hpp>
using namespace cv;
using namespace std;

int main()
{
	Mat img = imread("./morphologyEx.jpg");
	cout << img.size() << endl;
	imshow("Original drawing", img);

	Mat dst;
	//Get custom core
	Mat element = getStructuringElement(MORPH_RECT, Size(10, 10));
	//Morphological closure operation
	morphologyEx(img, dst, MORPH_CLOSE, element);

	imshow("Morphological closure operation", dst);
	waitKey();
	return 0;
}

Morphological gradient operation

Morphological gradient operation can describe the intensity of image brightness change; When we want to highlight the periphery of the highlighted area, we can use morphological gradient to highlight the edge and preserve the edge contour of the object.

Morphological gradient operation case:

#include<iostream>
#include<opencv2\opencv.hpp>
#include<opencv2\highgui\highgui.hpp>
using namespace cv;
using namespace std;

int main()
{
	Mat img = imread("./morphologyEx.jpg");
	cout << img.size() << endl;
	imshow("Original drawing", img);

	Mat dst;
	//Get custom core
	Mat element = getStructuringElement(MORPH_RECT, Size(10, 10));
	//Morphological gradient operation
	morphologyEx(img, dst, MORPH_GRADIENT, element);

	imshow("Morphological gradient operation", dst);
	waitKey();
	return 0;
}

Top hat

The top cap is the difference image between the original drawing and the original drawing.

The opening operation enlarges the crack or local low brightness area. Therefore, the result obtained by subtracting the opened image from the original image highlights the brighter area than the area around the outline of the original image. This operation is related to the size of the selected core. TopHat operation is generally used to separate patches that are lit near neighbors. This operation can be used to extract the background.

Top hat case:

#include<iostream>
#include<opencv2\opencv.hpp>
#include<opencv2\highgui\highgui.hpp>
using namespace cv;
using namespace std;

int main()
{
	Mat img = imread("./morphologyEx.jpg");
	cout << img.size() << endl;
	imshow("Original drawing", img);

	Mat dst,dst_open;
	// Get custom core
	Mat element = getStructuringElement(MORPH_RECT, Size(5, 5));
	// Morphological hat
	morphologyEx(img, dst_open, MORPH_OPEN, element);
	morphologyEx(img, dst, MORPH_TOPHAT, element);

	imshow("Open operation", dst_open);
	imshow("Morphological hat", dst);
	waitKey();
	return 0;
}

Black hat

The black hat is the difference image between the closed operation result and the original image.

The result of the black hat operation highlights the darker area than the area around the contour of the original image, so the black hat operation is used to separate the patches darker than the adjacent points.

Black hat case:

#include<iostream>
#include<opencv2\opencv.hpp>
#include<opencv2\highgui\highgui.hpp>
using namespace cv;
using namespace std;

int main()
{
	Mat img = imread("./morphologyEx.jpg");
	cout << img.size() << endl;
	imshow("Original drawing", img);

	Mat dst, dst_close;
	// Get custom core
	Mat element = getStructuringElement(MORPH_RECT, Size(5, 5));
	// Morphological black hat
	morphologyEx(img, dst_close, MORPH_CLOSE, element);
	morphologyEx(img, dst, MORPH_BLACKHAT, element);

	imshow("Closed operation", dst_close);
	imshow("Morphological black hat", dst);
	waitKey();
	return 0;
}

Keywords: OpenCV Computer Vision image processing

Added by Jay_Seagrave on Fri, 12 Nov 2021 00:18:44 +0200