Skip to content

Commit

Permalink
Tweaked the corner detectors.
Browse files Browse the repository at this point in the history
  • Loading branch information
BradLarson committed Sep 12, 2012
1 parent cdddaea commit 58f8826
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(
GPUImagePicture *blackAndWhiteBoxImage = [[GPUImagePicture alloc] initWithImage:inputImage];

[self testHarrisCornerDetectorAgainstPicture:blackAndWhiteBoxImage withName:@"WhiteBoxes"];

[self testNobleCornerDetectorAgainstPicture:blackAndWhiteBoxImage withName:@"WhiteBoxes"];
[self testShiTomasiCornerDetectorAgainstPicture:blackAndWhiteBoxImage withName:@"WhiteBoxes"];


// Testing erosion and dilation
GPUImageErosionFilter *erosionFilter = [[GPUImageErosionFilter alloc] initWithRadius:4];
[blackAndWhiteBoxImage removeAllTargets];
Expand Down Expand Up @@ -93,18 +93,18 @@ - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(

- (void)testCornerDetector:(GPUImageHarrisCornerDetectionFilter *)cornerDetector ofName:(NSString *)detectorName againstPicture:(GPUImagePicture *)pictureInput withName:(NSString *)pictureName;
{
cornerDetector.threshold = 0.4;
cornerDetector.sensitivity = 4.0;
cornerDetector.blurSize = 1.0;
[pictureInput removeAllTargets];

[pictureInput addTarget:cornerDetector];

GPUImageCrosshairGenerator *crosshairGenerator = [[GPUImageCrosshairGenerator alloc] init];
crosshairGenerator.crosshairWidth = 5.0;
crosshairGenerator.crosshairWidth = 10.0;
[crosshairGenerator setCrosshairColorRed:1.0 green:0.0 blue:0.0];
[crosshairGenerator forceProcessingAtSize:[pictureInput outputImageSize]];

[cornerDetector setCornersDetectedBlock:^(GLfloat* cornerArray, NSUInteger cornersDetected, CMTime frameTime) {
[crosshairGenerator renderCrosshairsFromArray:cornerArray count:cornersDetected frameTime:frameTime];
}];

GPUImageAlphaBlendFilter *blendFilter = [[GPUImageAlphaBlendFilter alloc] init];
[blendFilter forceProcessingAtSize:[pictureInput outputImageSize]];
[pictureInput addTarget:blendFilter];
Expand All @@ -113,19 +113,29 @@ - (void)testCornerDetector:(GPUImageHarrisCornerDetectionFilter *)cornerDetector
[crosshairGenerator addTarget:blendFilter];

[blendFilter prepareForImageCapture];
[pictureInput processImage];

NSUInteger currentImageIndex = 0;
for (UIImage *currentImage in cornerDetector.intermediateImages)
{
[self saveImage:currentImage fileName:[NSString stringWithFormat:@"%@-%@-%d.png", detectorName, pictureName, currentImageIndex]];
[cornerDetector setCornersDetectedBlock:^(GLfloat* cornerArray, NSUInteger cornersDetected, CMTime frameTime) {
NSLog(@"Number of corners: %d", cornersDetected);
[crosshairGenerator renderCrosshairsFromArray:cornerArray count:cornersDetected frameTime:frameTime];

currentImageIndex++;
}

UIImage *crosshairResult = [blendFilter imageFromCurrentlyProcessedOutput];
dispatch_async(dispatch_get_main_queue(), ^{
NSUInteger currentImageIndex = 0;
for (UIImage *currentImage in cornerDetector.intermediateImages)
{
[self saveImage:currentImage fileName:[NSString stringWithFormat:@"%@-%@-%d.png", detectorName, pictureName, currentImageIndex]];

currentImageIndex++;
}

NSLog(@"Save corner image");
UIImage *crosshairResult = [blendFilter imageFromCurrentlyProcessedOutput];

[self saveImage:crosshairResult fileName:[NSString stringWithFormat:@"%@-%@-Crosshairs.png", detectorName, pictureName]];
});
}];

[self saveImage:crosshairResult fileName:[NSString stringWithFormat:@"%@-%@-Crosshairs.png", detectorName, pictureName]];

[pictureInput processImage];
}

- (void)testHarrisCornerDetectorAgainstPicture:(GPUImagePicture *)pictureInput withName:(NSString *)pictureName;
Expand Down
65 changes: 35 additions & 30 deletions framework/Source/GPUImageHarrisCornerDetectionFilter.m
Original file line number Diff line number Diff line change
Expand Up @@ -141,45 +141,53 @@ - (id)initWithCornerDetectionFragmentShader:(NSString *)cornerDetectionFragmentS
simpleThresholdFilter = [[GPUImageFilter alloc] initWithFragmentShaderFromString:kGPUImageSimpleThresholdFragmentShaderString];
[self addFilter:simpleThresholdFilter];

__unsafe_unretained GPUImageHarrisCornerDetectionFilter *weakSelf = self;
#ifdef DEBUGFEATUREDETECTION
weakFilter = simpleThresholdFilter;
[simpleThresholdFilter setFrameProcessingCompletionBlock:^(GPUImageOutput *filter, CMTime frameTime){
UIImage *intermediateImage = [weakFilter imageFromCurrentlyProcessedOutput];
[weakIntermediateImages addObject:intermediateImage];
}];
#endif

// Sixth pass: compress the thresholded points into the RGBA channels
colorPackingFilter = [[GPUImageColorPackingFilter alloc] init];
[self addFilter:colorPackingFilter];


#ifdef DEBUGFEATUREDETECTION
__unsafe_unretained GPUImageHarrisCornerDetectionFilter *weakSelf = self;
weakFilter = colorPackingFilter;
[colorPackingFilter setFrameProcessingCompletionBlock:^(GPUImageOutput *filter, CMTime frameTime){
NSLog(@"Triggered response from compaction filter");

UIImage *intermediateImage = [weakFilter imageFromCurrentlyProcessedOutput];
[weakIntermediateImages addObject:intermediateImage];

[weakSelf extractCornerLocationsFromImageAtFrameTime:frameTime];
}];
#else
__unsafe_unretained GPUImageHarrisCornerDetectionFilter *weakSelf = self;
[colorPackingFilter setFrameProcessingCompletionBlock:^(GPUImageOutput *filter, CMTime frameTime) {
[simpleThresholdFilter setFrameProcessingCompletionBlock:^(GPUImageOutput *filter, CMTime frameTime) {
[weakSelf extractCornerLocationsFromImageAtFrameTime:frameTime];
}];
#endif

// Sixth pass: compress the thresholded points into the RGBA channels
// colorPackingFilter = [[GPUImageColorPackingFilter alloc] init];
// [self addFilter:colorPackingFilter];
//
//
//#ifdef DEBUGFEATUREDETECTION
// __unsafe_unretained GPUImageHarrisCornerDetectionFilter *weakSelf = self;
// weakFilter = colorPackingFilter;
// [colorPackingFilter setFrameProcessingCompletionBlock:^(GPUImageOutput *filter, CMTime frameTime){
// NSLog(@"Triggered response from compaction filter");
//
// UIImage *intermediateImage = [weakFilter imageFromCurrentlyProcessedOutput];
// [weakIntermediateImages addObject:intermediateImage];
//
// [weakSelf extractCornerLocationsFromImageAtFrameTime:frameTime];
// }];
//#else
// __unsafe_unretained GPUImageHarrisCornerDetectionFilter *weakSelf = self;
// [colorPackingFilter setFrameProcessingCompletionBlock:^(GPUImageOutput *filter, CMTime frameTime) {
// [weakSelf extractCornerLocationsFromImageAtFrameTime:frameTime];
// }];
//#endif

[derivativeFilter addTarget:blurFilter];
[blurFilter addTarget:harrisCornerDetectionFilter];
[harrisCornerDetectionFilter addTarget:nonMaximumSuppressionFilter];
[nonMaximumSuppressionFilter addTarget:simpleThresholdFilter];
[simpleThresholdFilter addTarget:colorPackingFilter];
// [simpleThresholdFilter addTarget:colorPackingFilter];

self.initialFilters = [NSArray arrayWithObjects:derivativeFilter, nil];
self.terminalFilter = colorPackingFilter;
// self.terminalFilter = colorPackingFilter;
self.terminalFilter = simpleThresholdFilter;

self.blurSize = 1.0;
self.sensitivity = 5.0;
Expand Down Expand Up @@ -215,29 +223,26 @@ - (void)extractCornerLocationsFromImageAtFrameTime:(CMTime)frameTime;

CFAbsoluteTime startTime = CFAbsoluteTimeGetCurrent();

unsigned int imageWidth = imageSize.width * 4.0;
unsigned int imageWidth = imageSize.width * 4;

for (unsigned int currentByte = 0; currentByte < imageByteSize; currentByte++)
unsigned int currentByte = 0;
unsigned int cornerStorageIndex = 0;
while (currentByte < imageByteSize)
{
GLubyte colorByte = rawImagePixels[currentByte];

// Red: -, -
// Green: +, -
// Blue: -, +
// Alpha: +. +

if (colorByte > 0)
{
// NSLog(@"(%d, %d): R: %d", scaledXCoordinate, scaledYCoordinate, redByte);
unsigned int xCoordinate = currentByte % imageWidth;
unsigned int yCoordinate = currentByte / imageWidth;

cornersArray[numberOfCorners * 2] = (CGFloat)(xCoordinate / 2) / imageSize.width;
cornersArray[numberOfCorners * 2 + 1] = (CGFloat)(yCoordinate * 2) / imageSize.height;
cornersArray[cornerStorageIndex++] = (CGFloat)(xCoordinate / 4) / imageSize.width;
cornersArray[cornerStorageIndex++] = (CGFloat)(yCoordinate) / imageSize.height;
numberOfCorners++;

numberOfCorners = MIN(numberOfCorners, 511);
}
currentByte +=4;
}

CFAbsoluteTime currentFrameTime = (CFAbsoluteTimeGetCurrent() - startTime);
Expand Down

0 comments on commit 58f8826

Please sign in to comment.