Created
November 15, 2012 02:27
-
-
Save anonymous/4076261 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
int markConnectedComponent(Image &image, int seedRow, int seedCol, int ccLabel) | |
{ | |
// int r = image.getNumRows(); | |
// int c = image.getNumCols(); | |
int counter=0; //num found pixels | |
int popVal=0; //unused | |
stack<PixelLocation> pixelStack; | |
PixelLocation currPixel = {seedRow, seedCol}; | |
//not needed | |
//PixelLocation neighbors[] = {{currPixel.r+1, currPixel.c},{currPixel.r-1, currPixel.c}, {currPixel.r, currPixel.c+1}, {currPixel.r, currPixel.c-1}}; | |
//good code | |
const int seedVal = image.getPixel(seedRow, seedCol); | |
pixelStack.push(currPixel); | |
while (!pixelStack.empty()) | |
{ | |
//currPixel = pixelStack.pop(); | |
currPixel = pixelStack.pop(); | |
if (seedVal==image.getPixel(currPixel.r, currPixel.c)) | |
{ | |
counter++; //num pixels ++ | |
image.setPixel(currPixel.r, currPixel.c, ccLabel); | |
/* | |
This part is not correct. the values of the array in neighbors never changes | |
neighbors has the neighboring pixels for your starting pixel not for you current pixel! | |
your on the right track. | |
Try: | |
for every neighbor pixel to current pixel | |
if neighbor pixel is in range | |
stack.add(neighbor pixel) | |
also counter and neighbors have no relation! | |
*/ | |
if (image.isInBounds(neighbors[counter])) | |
{ | |
//pixel stack only takes pixel locations! not arrays! | |
pixelStack.push(neighbors); | |
} | |
} | |
} | |
return counter; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment