Skip to content

Instantly share code, notes, and snippets.

Created November 15, 2012 02:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/4076261 to your computer and use it in GitHub Desktop.
Save anonymous/4076261 to your computer and use it in GitHub Desktop.
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