这里我们主要想讲解冈萨雷斯的数字图像处理这本书上膨胀腐蚀的定义及用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; }
结果:
膨胀就是如果结构元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;
结果:
腐蚀就是结构元B处非0,则A对应位置也全非0,且取全非0处最小值来代替当前像素点的值。
二:书中定义
膨胀: . 这里的B是结构元,B~是结构元的反射,反射不明白可以看书,就是旋转或者按中心点对称。如:
0 1 的反射就是 1 1
1 1 1 0
与opencv中唯一的区别就是必须得求反射,而opencv中则直接用给定的结构元B。
腐蚀:
案例:
作者:小村长 出处:http://blog.csdn.net/lu597203933 欢迎转载或分享,但请务必声明文章出处。 (新浪微博:http://weibo.com/2653613245/profile , 欢迎交流!)