我希望在GPUImage FilterShowcase中获得与“Harris Corner detection”相同的效果,但使用GPUImagePicture
代替GPUImageVideoCamera
。
我无法让过滤器链工作。这是我目前的代码:
-(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;
}
如果我使用视频Feed作为来源,它可以正常工作,它看起来像这样:
-(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];
}
我真的不确定GPUImagePicture *crosshairImageSource = [[GPUImagePicture alloc] initWithImage:[crosshairGenerator imageFromCurrentlyProcessedOutput]];
和/或我是否应该在添加的每个过滤器之后使用[stillImageSource processImage];
。
有人能指出我正确的方向吗?
由于
*更新*
通过查看GPUImage文件夹中的FeatureExtractionTest,我设法让它有点工作,但它找不到任何角落。输出为Number of corners: 0
。我一直在玩threshold
,sensitivity
和blurSize
值。
这是新代码:
- (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];
}
这是我使用的测试图像:
*更新2 * 这似乎是一个iOS7问题。管理以使我的代码在iOS6设备上运行。将进一步调查。
*更新3 * 使用iOS7确实是一个错误。提交到此处:https://github.com/BradLarson/GPUImage/issues/1205