Basic concept
corrosion
Corrosion can be understood as the center of B (anchor point) walking along the inner boundary of A in a circle. For corrosion also highlight part , portions other than the region A <highlight pixel A , which is substituted in the outside.
Swell
Expansion can be understood as the center of B (anchor point) walking along the outer boundary of A in a circle. For expansion is highlight part , portions other than the region A <highlight pixel A , which is substituted in the outside.
Open operation
By for the first 腐蚀
operation, then 膨胀
the operation obtained. We are very useful when removing small objects (assuming that the object is bright and the foreground color is black), which is used to erase noise that is brighter than the background .
Closed operation
闭运算
It is a reverse operation of the opening operation, in particular to be 膨胀
then 腐蚀
operated , used to erase darker than the background noise .
Morphological gradient
Subtracting the corroded image from the dilated image can be used to extract the edges in the picture.
Top hat algorithm
The difference between the original image and the image after the opening operation is used to extract the bright small target foreground.
Black hat algorithm
The difference between the original image and the image after the operation, and the non-highlight small target foreground is taken out.
API introduction
Expansion and corrosion
//获取,卷积核
Mat structureElement = getStructuringElement(MORPH_RECT, Size(s, s), Point(-1, -1));
//指定 卷积核形状 大小 锚点位置(-1为中心)
dilate(src, dst, structureElement, Point(-1, -1), 1);
// 源图 结果 卷积核 锚点 迭代次数
erode(src, dst, structureElement);
//默认 锚点为中心点 迭代次数为 1
Other morphological operations
//同样先获得,卷积核:
// MORPH_RECT 矩形 MORPH_CROSS 十字形 MORPH_ELLIPSE 椭圆形
Mat structureElement = getStructuringElement(MORPH_RECT, Size(s, s), Point(-1, -1));
//调用morphologyEx
morphologyEx(src, dst, MORPH_BLACKHAT, structureElement);
// 原图,结果,运算类型 运算核
/*******************************************************************************
MORPH_CLOSE 闭
MORPH_OPEN 开
MORPH_GRADIENT 梯度
MORPH_TOPHAT 顶帽
MORPH_BLACKHAT 黑帽
MORPH_DILATE 膨胀
MORPH_ERODE 腐蚀
*********************************************************************************/
Code practice
#include <iostream>
#include <math.h>
#include <opencv2/opencv.hpp>
using namespace cv;
using namespace std;
Mat src,dst;
int element_size = 3;
int max_size = 25;
char OUTPUT[] = "OUTPUT_WINDOS";
void CallBack_Demo(int, void*)
{
int s = element_size * 2 + 1;
Mat structureElement = getStructuringElement(MORPH_RECT, Size(s, s), Point(-1, -1));
//膨胀与腐蚀
//dilate(src, dst, structureElement, Point(-1, -1), 1);
//erode(src, dst, structureElement);
//闭
//morphologyEx(src,dst,MORPH_CLOSE,structureElement);
//开
//morphologyEx(src, dst, MORPH_OPEN, structureElement);
//梯度
//morphologyEx(src, dst, MORPH_GRADIENT, structureElement);
//顶帽
//morphologyEx(src, dst,MORPH_TOPHAT , structureElement);
//黑帽
morphologyEx(src, dst, MORPH_BLACKHAT, structureElement);
imshow(OUTPUT, dst);
return;
}
int main(int argc, char* argv[])
{
//src = imread("src.jpg");
src = imread("1.PNG");
if (!src.data)
{
cout << "cannot open image" << endl;
return -1;
}
namedWindow("input image", WINDOW_AUTOSIZE);
imshow("input image",src);
namedWindow(OUTPUT,WINDOW_AUTOSIZE);
createTrackbar("Element Size:", OUTPUT, &element_size, max_size, CallBack_Demo);
CallBack_Demo(0, 0);
waitKey(0);
return 0;
}