Thinning
Simple thinning algorithm that uses structuring elements as it is decribed here
Structuring elements are applied using the 'hit - miss' operation till no furher changes are seen.
The structuring elements are described by using the code below:
Mat seLeft0 = (Mat_
Mat seRight0 = (Mat_
Mat seLeft90 = (Mat_
Mat seRight90 = (Mat_
Mat seLeft180 = (Mat_
Mat seRight180 = (Mat_
Mat seLeft270 = (Mat_
Mat seRight270 = (Mat_
morphologyEx(dst_bin, foo, MORPH_HITMISS, seLeft0);
Then result of operation, which is (foo) above, is substracted from input image (dst_bin) so that the input image is updated. Substraction is done by the line below.
dst_bin = dst_bin - foo;
You can refer to the figures below for input image and the resulting output (foo).
The effect of subtracting the foo's from the input (dst_bin) in the array of images below.
Above results are obtained by using the loop below.
int ctr{ 0 };
while (true)
{
ctr++;
cout << "Iteration " << ctr << endl;
morphologyEx(dst_bin, foo, MORPH_HITMISS, seLeft0);
dst_bin = dst_bin - foo;
morphologyEx(dst_bin, foo, MORPH_HITMISS, seRight0);
dst_bin = dst_bin - foo;
morphologyEx(dst_bin, foo, MORPH_HITMISS, seLeft90);
dst_bin = dst_bin - foo;
morphologyEx(dst_bin, foo, MORPH_HITMISS, seRight90);
dst_bin = dst_bin - foo;
morphologyEx(dst_bin, foo, MORPH_HITMISS, seLeft180);
dst_bin = dst_bin - foo;
morphologyEx(dst_bin, foo, MORPH_HITMISS, seRight180);
dst_bin = dst_bin - foo;
morphologyEx(dst_bin, foo, MORPH_HITMISS, seLeft270);
dst_bin = dst_bin - foo;
morphologyEx(dst_bin, foo, MORPH_HITMISS, seRight270);
if (countNonZero(foo) == 0) break;
dst_bin = dst_bin - foo;
}
ctr is just a counter that keeps track of number of iteration while the line below breaks the loop if no further changes are seen.
if (countNonZero(foo) == 0) break;
You can find the source code here