Skip to content

Instantly share code, notes, and snippets.

@lattejed
Created December 16, 2010 18:13
Show Gist options
  • Save lattejed/743755 to your computer and use it in GitHub Desktop.
Save lattejed/743755 to your computer and use it in GitHub Desktop.
- (void)fetchContours {
int currentPixel, currentDir;
RSPoint point, nextPoint, testPoint;
lastEntryPoint = RSPointMake(0,0);
lastPoint = RSPointMake(0,0);
lastFoundDir = 4;
marching = YES;
connected = NO;
for (int y=0; y<h; y++) {
for (int x=0; x<w; x++) {
// Is marching
if (YES == marching) {
RSContour currentPolygon;
currentPolygon.push_back(lastEntryPoint);
// Inner marching loop
while (YES) {
nextPoint = [self scan:lastPoint withComparator:currentPolygonColor];
// Break conditions
// Previously seen color found 4-connected or we've come full circle
if (YES == connected || nextPoint == lastEntryPoint) {
int pointsCount = currentPolygon.size();
for (int i=0; i<pointsCount; i++) {
point = currentPolygon.at(i);
[self setPointAsSeen:point];
}
if (NO == connected) {
// Add this polygon to our polygons
contours.push_back(currentPolygon);
}
marching = NO;
break;
}
else {
// Add this next point to our current polygon
currentPolygon.push_back(nextPoint);
lastPoint = nextPoint;
}
}
}
// Is scanning
else {
currentPixel = originalImage[y * w + x];
if (currentPixel > 0) {
// We have an 'unseen' pixel, first see if we're connected to a seen area
RSPoint thisPoint = {x,y};
testPoint = [self scan:thisPoint withComparatorSeen:currentPixel];
if (testPoint.x != -1) {
// If so, set that to 'seen'
[self setPointAsSeen:point];
}
else {
// Nothing connected, start a new polygon
lastEntryPoint = RSPointMake(x,y);
lastPoint = RSPointMake(x,y);
currentPolygonColor = currentPixel;
lastFoundDir = 4;
currentDir = -1;
marching = YES;
connected = NO;
}
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment