Here we mainly want to explain the definition of the expansion and corrosion in the book of Digital Image Processing of Gonzalez and the results achieved using opencv, and then compare the differences between them.
I. opencv implementation
Before that, let's look at another blog: http://blog.csdn.net/lu597203933/article/details/17184439.
Expansion:
Case code:
Int main () {int A [8] [8] =, ,}, {,}, {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 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> () = 2; // If the structure element is not 0, it is 1dilated. at <uchar> (1, 2) = 1; dilated. at <uchar> () = 1; cout <"dilated =" <Endl <"" <dilated <Endl; dilate (S, result, dilated ); cout <"result =" <Endl <"" <result <Endl; return 0 ;}
Result:
Expansion means that if Structure Element B has a place other than 0, and the position of a is not 0, the maximum value of all pixels corresponding to non-0 in A and B is obtained to replace the current pixel value. The default structure element in opencv is a 3*3 matrix, all of which are not 0.
Corrosion:
Case code:
Int main () {int A [8] [8] =, ,}, {,}, {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 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> () = 2; // If the structure element is not 0, it is 1eroded. 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;
Result:
Corrosion means that the structure element B is not 0, then the corresponding position of A is not 0, and the minimum value of all non-0 is used to replace the value of the current pixel.
Ii. definitions in the book
Expansion:. Here B is the structure element, B ~ It is the reflection of the structure element. If you do not understand the reflection, you can read the book, that is, rotating or symmetrical according to the center point. For example:
The reflection of 0 1 is 1.
1 1 1 0
The only difference from opencv is that reflection must be obtained, while opencv directly uses the given Structure Element B.
Corrosion:
Case:
Small village chief source: http://blog.csdn.net/lu597203933 welcome to reprint or share, but please be sure to declare the source of the article. (Sina Weibo: http://weibo.com/2653613245/profile, welcome to exchange !)