From 58f88263619487e17eb5ff77fbc8f73c9fb0fa7a Mon Sep 17 00:00:00 2001 From: Brad Larson Date: Wed, 12 Sep 2012 18:19:35 -0500 Subject: [PATCH] Tweaked the corner detectors. --- .../FeatureExtractionAppDelegate.m | 42 +++++++----- .../GPUImageHarrisCornerDetectionFilter.m | 65 ++++++++++--------- 2 files changed, 61 insertions(+), 46 deletions(-) diff --git a/examples/FeatureExtractionTest/FeatureExtractionTest/FeatureExtractionAppDelegate.m b/examples/FeatureExtractionTest/FeatureExtractionTest/FeatureExtractionAppDelegate.m index 11bb467d2..bc5c19186 100644 --- a/examples/FeatureExtractionTest/FeatureExtractionTest/FeatureExtractionAppDelegate.m +++ b/examples/FeatureExtractionTest/FeatureExtractionTest/FeatureExtractionAppDelegate.m @@ -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]; @@ -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]; @@ -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; diff --git a/framework/Source/GPUImageHarrisCornerDetectionFilter.m b/framework/Source/GPUImageHarrisCornerDetectionFilter.m index f56c4b4f3..474d5f473 100755 --- a/framework/Source/GPUImageHarrisCornerDetectionFilter.m +++ b/framework/Source/GPUImageHarrisCornerDetectionFilter.m @@ -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; @@ -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);