码迷,mamicode.com
首页 > 其他好文 > 详细

图像形态学操作—腐蚀膨胀深入

时间:2014-07-02 08:30:04      阅读:146      评论:0      收藏:0      [点我收藏+]

标签:opencv   膨胀   腐蚀   

这里我们主要想讲解冈萨雷斯的数字图像处理这本书上膨胀腐蚀的定义及用opencv实现的结果,然后对比它们之间的差异。

一:opencv实现

       在这之前可以看我的另外一篇blog:http://blog.csdn.net/lu597203933/article/details/17184439

膨胀:

案例代码:

int main()
{
	int a[8][8] = {
		{0,0,0,0,0,0,0,0}, 
		{0,0,5,1,0,0,1,1}, 
		{0,1,0,1,0,1,0,0},
		{0,1,1,2,1,0,1,0},
		{0,0,0,1,0,1,0,0},
		{0,1,0,0,0,0,0,1},
		{0,0,0,0,0,0,0,0},
		{0,0,0,0,0,0,0,0}
	};
	Mat S = Mat::zeros(8, 8, CV_8UC1);
	for(int j = 0; j < S.rows; j++)
	{
		for(int i = 0; i < S.cols; i++)
		{
			S.at<uchar>(j,i) = a[j][i];
		}
	}
	cout << "S = " << endl << " " << S << endl;
	Mat result;
	Mat dilated = Mat::zeros(3, 3, CV_8UC1);
	dilated.at<uchar>(0,1) = 1;
	//eroded.at<uchar>(0,2) = 1;
	dilated.at<uchar>(1,0) = 1;
	dilated.at<uchar>(1,1) = 2;          // 结构元素非0即1
	dilated.at<uchar>(1,2) = 1;
	dilated.at<uchar>(2,1) = 1;
	cout << "dilated = " << endl << " " << dilated << endl;	
	dilate(S, result, dilated);  
	cout << "result = " << endl << " " << result << endl;
	return 0;
}

结果:

bubuko.com,布布扣

膨胀就是如果结构元B有一处非0,而A对应位置非0,则求出A中与B中对应非0处所有像素最大值来代替当前像素点值。Opencv中默认的结构元是3*3的矩阵,全部非0。

腐蚀:

案例代码:

int main()
{
	int a[8][8] = {
		{0,0,0,0,0,0,0,0}, 
		{0,0,5,1,0,0,1,1}, 
		{0,1,0,1,0,1,0,0},
		{0,1,1,2,1,0,1,0},
		{0,0,0,1,0,1,0,0},
		{0,1,0,0,0,0,0,1},
		{0,0,0,0,0,0,0,0},
		{0,0,0,0,0,0,0,0}
	};
	Mat S = Mat::zeros(8, 8, CV_8UC1);
	for(int j = 0; j < S.rows; j++)
	{
		for(int i = 0; i < S.cols; i++)
		{
			S.at<uchar>(j,i) = a[j][i];
		}
	}
	cout << "S = " << endl << " " << S << endl;
	Mat result;
	Mat eroded = Mat::zeros(3, 3, CV_8UC1);
	eroded.at<uchar>(0,1) = 1;
	//eroded.at<uchar>(0,2) = 1;
	eroded.at<uchar>(1,0) = 1;
	eroded.at<uchar>(1,1) = 2;          // 结构元素非0即1
	eroded.at<uchar>(1,2) = 1;
	eroded.at<uchar>(2,1) = 1;
	cout << "eroded = " << endl << " " << eroded << endl;	
	erode(S, result, eroded);  
	cout << "result = " << endl << " " << result << endl;
	return 0;

结果:

bubuko.com,布布扣

腐蚀就是结构元B处非0,则A对应位置也全非0,且取全非0处最小值来代替当前像素点的值。

 

二:书中定义

膨胀: . bubuko.com,布布扣这里的B是结构元,B~是结构元的反射,反射不明白可以看书,就是旋转或者按中心点对称。如:

0 1   的反射就是 1 1

1 1                        1 0

与opencv中唯一的区别就是必须得求反射,而opencv中则直接用给定的结构元B。

腐蚀: bubuko.com,布布扣

案例:

bubuko.com,布布扣


作者:小村长  出处:http://blog.csdn.net/lu597203933 欢迎转载或分享,但请务必声明文章出处。 (新浪微博:http://weibo.com/2653613245/profile , 欢迎交流!)

图像形态学操作—腐蚀膨胀深入,布布扣,bubuko.com

图像形态学操作—腐蚀膨胀深入

标签:opencv   膨胀   腐蚀   

原文地址:http://blog.csdn.net/lu597203933/article/details/36222277

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!