I am working on an image project, and I'm trying to make a connected components algorithm. This is what I have come up with:
Mat OPG3::cc(Mat imgCC) {
int p = 1;
int lable = 1;
copyMakeBorder(imgCC, imgCC, p, p, p, p, BORDER_CONSTANT, 0);
for (int imgRow = p; imgRow < imgCC.rows - p; imgRow++) {
for (int imgCol = p; imgCol < imgCC.cols - p; imgCol++) {
if (imgCC.at < uchar >(imgRow,imgCol) == 255) {
imgCC.at<uchar>(imgRow, imgCol) = lable;
recursive(imgRow, imgCol - 1, lable, p, imgCC);
recursive(imgRow + 1, imgCol, lable, p, imgCC);
recursive(imgRow, imgCol + 1, lable, p, imgCC);
recursive(imgRow - 1, imgCol, lable, p, imgCC);
++lable;
}
}
}
return imgCC;
}
void OPG3::recursive(int i, int j, int lable, int p, Mat& imgCC) {
if (imgCC.at < uchar >(i, j) == 255) {
imgCC.at<uchar>(i, j) = lable;
recursive(i, j - 1, lable, p, imgCC);
recursive(i + 1, j, lable, p, imgCC);
recursive(i, j + 1, lable, p, imgCC);
recursive(i - 1, j, lable, p, imgCC);
}
}
When I run the code, it stops in the middle of the execution with some images, but with outhers it executes correctly.
Is it possible that the algorithm in some cases makes too many function calls?