OpenCV—形态学运算定义与实现

2.腐蚀与膨胀运算

（1）腐蚀 erode

``````// Read input image
if (!image.data)
return 0;
// Display the image
cv::namedWindow("Image");
cv::imshow("Image",image);
// Erode the image
cv::Mat eroded;
cv::erode(image,eroded,cv::Mat());//cv::Mat()为空矩阵，此时采用默认3*3正方形结构元素
// Display the eroded image
cv::namedWindow("Eroded Image");
cv::imshow("Eroded Image",eroded);
// Erode the image with a larger s.e.定义更大的结构元素
cv::Mat element(7,7,CV_8U,cv::Scalar(1));
// Display the eroded image by large s.e.
cv::erode(image,eroded,element);
cv::namedWindow("Eroded Image (7x7)");
cv::imshow("Eroded Image (7x7)",eroded);
// Erode the image 3 times.腐蚀3次
cv::erode(image,eroded,cv::Mat(),cv::Point(-1,-1),3);//cv::Point(-1,-1)表示原点是矩阵的中心点
// Display the eroded image
cv::namedWindow("Eroded Image (3 times)");
cv::imshow("Eroded Image (3 times)",eroded);
cv::waitKey(0);``````

3×3正方形结构元素腐蚀结果

7×7正方形结构元素腐蚀结果

3×3正方形结构元素腐蚀3结果

（2）膨胀 dilate

``````// Dilate the image
cv::Mat dilated;
cv::dilate(image,dilated,cv::Mat());
// Display the dialted image
cv::namedWindow("Dilated Image");
cv::imshow("Dilated Image",dilated);``````

3×3正方形结构元素膨胀结果

（3）自定义结构元素

``````// 创建自定义结构元素
unsigned char m[9] = {
0,1,0,
1,1,1,
0,1,0
};
cv::Mat element1(3,3,CV_8U,m); //创建自定义矩阵element1
//显示该结构元素
int nr = element1.rows;
int nl = element1.cols;
for(int j = 0;j<nr;j++)
{
char *data = element1.ptr<char>(j);
for(int i = 0; i<nl; i++)
{
int value = data[i];
cout<<value<<" ";
}
cout<<endl;
}
// Display the eroded image by large s.e.
cv::erode(image,eroded,element1);
cv::namedWindow("Eroded Image (user define)");
cv::imshow("Eroded Image (user define)",eroded);``````

``````	cv::Mat_<uchar> cross(5,5);
cv::Mat_<uchar> diamond(5,5);
cv::Mat_<uchar> x(5,5);
cv::Mat_<uchar> square(5,5);

// Creating the cross-shaped structuring element
cross <<
0, 0, 1, 0, 0,
0, 0, 1, 0, 0,
1, 1, 1, 1, 1,
0, 0, 1, 0, 0,
0, 0, 1, 0, 0;

// Creating the diamond-shaped structuring element
diamond <<
0, 0, 1, 0, 0,
0, 1, 1, 1, 0,
1, 1, 1, 1, 1,
0, 1, 1, 1, 0,
0, 0, 1, 0, 0;

// Creating the x-shaped structuring element
x <<
1, 0, 0, 0, 1,
0, 1, 0, 1, 0,
0, 0, 1, 0, 0,
0, 1, 0, 1, 0,
1, 0, 0, 0, 1;

// Creating the square-shaped structuring element
square <<
1, 1, 1, 1, 1,
1, 1, 1, 1, 1,
1, 1, 1, 1, 1,
1, 1, 1, 1, 1,
1, 1, 1, 1, 1;
<span style="white-space:pre">	</span> //display x-shaped structuring element
cout<<endl<<"x-shaped structuring element"<<endl<<endl;
int xnr = x.rows;
int xnl = x.cols;
for(int j = 0;j<nr;j++)
{
char *data = x.ptr<char>(j);
for(int i = 0; i<nl; i++)
{
int value = data[i];
cout<<value<<" ";
}
cout<<endl;
}``````

3.开闭运算

（1）闭运算

``````cv::Mat element5(5,5,CV_8U,cv::Scalar(1));//5*5正方形，8位uchar型，全1结构元素
cv::Mat closed;
cv::morphologyEx(image, closed,cv::MORPH_CLOSE,element5);//高级形态学运算函数
// Display the opened image
cv::namedWindow("Closed Image");
cv::imshow("Closed Image",closed);
cv::waitKey(0);``````

5×5正方形结构元素运算结果

（2）开运算

``````cv::Mat element5(5,5,CV_8U,cv::Scalar(1));//5*5正方形，8位uchar型，全1结构元素
cv::Mat opened;
cv::morphologyEx(image, opened,cv::MORPH_OPEN,element5);
// Display the opened image
cv::namedWindow("Opened Image");
cv::imshow("Opened Image",opened);
cv::waitKey(0);``````

5×5正方形结构元素运算结果

http://blog.csdn.net/iracer/article/details/49057145

https://blog.csdn.net/iracer/article/details/116051674?spm=1001.2014.3001.5501