问题
I want to achieve the same effect as in "Harris Corner detection" in GPUImage FilterShowcase but with a GPUImagePicture
instead of a GPUImageVideoCamera
.
I can't get the filter chain to work. Here's my current code:
-(UIImage*)addHarrisCornerToImage:(UIImage*)inputImage
{
GPUImagePicture *stillImageSource = [[GPUImagePicture alloc] initWithImage:inputImage];
GPUImageHarrisCornerDetectionFilter *cornerDetectionFilter = [[GPUImageHarrisCornerDetectionFilter alloc] init];
[(GPUImageHarrisCornerDetectionFilter *)_filter setThreshold:0.20];
GPUImageCrosshairGenerator *crosshairGenerator = [[GPUImageCrosshairGenerator alloc] init];
crosshairGenerator.crosshairWidth = 15.0;
[crosshairGenerator forceProcessingAtSize:CGSizeMake(480.0, 640.0)];
[(GPUImageHarrisCornerDetectionFilter *)_filter setCornersDetectedBlock:^(GLfloat* cornerArray, NSUInteger cornersDetected, CMTime frameTime) {
[crosshairGenerator renderCrosshairsFromArray:cornerArray count:cornersDetected frameTime:frameTime];
}];
GPUImageAlphaBlendFilter *blendFilter = [[GPUImageAlphaBlendFilter alloc] init];
[blendFilter forceProcessingAtSize:CGSizeMake(480.0, 640.0)];
GPUImageGammaFilter *gammaFilter = [[GPUImageGammaFilter alloc] init];
[stillImageSource addTarget:gammaFilter];
[gammaFilter addTarget:blendFilter];
[crosshairGenerator addTarget:blendFilter];
GPUImagePicture *crosshairImageSource = [[GPUImagePicture alloc] initWithImage:[crosshairGenerator imageFromCurrentlyProcessedOutput]];
[crosshairImageSource addTarget:cornerDetectionFilter];
[stillImageSource addTarget:cornerDetectionFilter];
[stillImageSource processImage];
UIImage *imageWithCorners = [cornerDetectionFilter imageFromCurrentlyProcessedOutput];
return imageWithCorners;
}
If I use the video feed as source, it works fine, and it looks like this:
-(void)addHarrisCorner
{
_videoCamera = [[GPUImageVideoCamera alloc] initWithSessionPreset:AVCaptureSessionPreset640x480 cameraPosition:AVCaptureDevicePositionBack];
_videoCamera.outputImageOrientation = UIInterfaceOrientationPortrait;
GPUImageView *filteredVideoView = [[GPUImageView alloc] initWithFrame:CGRectMake(10, 150, 300, 300)];
_filter = [[GPUImageHarrisCornerDetectionFilter alloc] init];
[(GPUImageHarrisCornerDetectionFilter *)_filter setThreshold:0.20];
GPUImageCrosshairGenerator *crosshairGenerator = [[GPUImageCrosshairGenerator alloc] init];
crosshairGenerator.crosshairWidth = 15.0;
[crosshairGenerator forceProcessingAtSize:CGSizeMake(480.0, 640.0)];
[(GPUImageHarrisCornerDetectionFilter *)_filter setCornersDetectedBlock:^(GLfloat* cornerArray, NSUInteger cornersDetected, CMTime frameTime) {
[crosshairGenerator renderCrosshairsFromArray:cornerArray count:cornersDetected frameTime:frameTime];
}];
GPUImageAlphaBlendFilter *blendFilter = [[GPUImageAlphaBlendFilter alloc] init];
[blendFilter forceProcessingAtSize:CGSizeMake(480.0, 640.0)];
GPUImageGammaFilter *gammaFilter = [[GPUImageGammaFilter alloc] init];
[_videoCamera addTarget:gammaFilter];
[gammaFilter addTarget:blendFilter];
[crosshairGenerator addTarget:blendFilter];
[blendFilter addTarget:filteredVideoView];
[_videoCamera addTarget:_filter];
[_filter addTarget:filteredVideoView];
[_videoCamera startCameraCapture];
[[self view] addSubview:filteredVideoView];
}
I'm really not sure about GPUImagePicture *crosshairImageSource = [[GPUImagePicture alloc] initWithImage:[crosshairGenerator imageFromCurrentlyProcessedOutput]];
and/or if I should use [stillImageSource processImage];
after every filter I add.
Could someone point me in the right direction?
Thanks
* UPDATE *
By looking at the FeatureExtractionTest in the GPUImage folder I managed to it to work somewhat, but it doesn't find any corners. The output is Number of corners: 0
. I've been playing around with threshold
, sensitivity
and blurSize
values.
Here's the new code:
- (void)testCornerDetectorWithPicture:(UIImage *)image
{
// GPUImagePicture *pictureInput = [[GPUImagePicture alloc] initWithImage:image];
GPUImagePicture *pictureInput = [[GPUImagePicture alloc] initWithImage:[UIImage imageNamed:@"testCorner.png"]];
_harrisCornerFilter = [[GPUImageHarrisCornerDetectionFilter alloc] init];
_harrisCornerFilter.threshold = 0.2;
_harrisCornerFilter.sensitivity = 5.0;
_harrisCornerFilter.blurSize = 1.0;
[pictureInput removeAllTargets];
[pictureInput addTarget:_harrisCornerFilter];
__weak typeof(self) weakSelf = self;
[_harrisCornerFilter setCornersDetectedBlock:^(GLfloat* cornerArray, NSUInteger cornersDetected, CMTime frameTime) {
GPUImageCrosshairGenerator *crosshairGenerator = [[GPUImageCrosshairGenerator alloc] init];
crosshairGenerator.crosshairWidth = 10.0;
[crosshairGenerator setCrosshairColorRed:1.0 green:0.0 blue:0.0];
[crosshairGenerator forceProcessingAtSize:[pictureInput outputImageSize]];
GPUImageAlphaBlendFilter *blendFilter = [[GPUImageAlphaBlendFilter alloc] init];
[blendFilter forceProcessingAtSize:[pictureInput outputImageSize]];
[pictureInput addTarget:blendFilter];
[crosshairGenerator addTarget:blendFilter];
[blendFilter prepareForImageCapture];
NSLog(@"Number of corners: %d", cornersDetected);
[crosshairGenerator renderCrosshairsFromArray:cornerArray count:cornersDetected frameTime:frameTime];
dispatch_async(dispatch_get_main_queue(), ^{
UIImage *crosshairResult = [blendFilter imageFromCurrentlyProcessedOutput];
[weakSelf useImageWithCornersWithImage:crosshairResult];
});
}];
[pictureInput processImage];
}
And this is the test image I use:
* UPDATE 2 * This seems to be an iOS7 issue. Managed to get my code to work on an iOS6 device. Will investigate further.
* UPDATE 3 * It is indeed a bug when using iOS7. Filed here: https://github.com/BradLarson/GPUImage/issues/1205
来源:https://stackoverflow.com/questions/18914461/gpuimage-how-to-chain-filters-with-gpuimagepicture