我按照OpenCV网站上的iOS视频处理教程进行了编译并安装在设备上。然而,当我点击"开始"按钮时,什么也看不见。显然,我的- (void)processImage:(Mat&)image
回调从未被调用。你能给我提示一下是什么问题吗?
Some things I check:
- 调用
-
actionStart
,并且两条日志行都打印YES -
self
对象在分配给self.videoCamera.delegate
时有效 -
imageView
有效且在屏幕上可见 -
videoCamera
在start
方法被调用时被初始化
我用iPhone 3GS进行测试。
这是我的ViewController.h的完整源代码:
#import <UIKit/UIKit.h>
#import <opencv2/highgui/cap_ios.h>
@interface ViewController : UIViewController <CvVideoCameraDelegate>
{
IBOutlet UIImageView* imageView;
IBOutlet UIButton* button;
CvVideoCamera* videoCamera;
}
- (IBAction)actionStart:(id)sender;
@property (nonatomic, strong) CvVideoCamera* videoCamera;
@end
和ViewController.mm:
@implementation ViewController
@synthesize videoCamera;
- (void)viewDidLoad
{
[super viewDidLoad];
self.videoCamera = [[CvVideoCamera alloc] initWithParentView:imageView];
self.videoCamera.delegate = self;
self.videoCamera.defaultAVCaptureDevicePosition = AVCaptureDevicePositionFront;
self.videoCamera.defaultAVCaptureSessionPreset = AVCaptureSessionPreset352x288;
self.videoCamera.defaultAVCaptureVideoOrientation = AVCaptureVideoOrientationPortrait;
self.videoCamera.defaultFPS = 30;
self.videoCamera.grayscaleMode = NO;
}
#pragma mark - UI Actions
- (IBAction)actionStart:(id)sender;
{
[self.videoCamera start];
NSLog(@"video camera running: %d", [self.videoCamera running]);
NSLog(@"capture session loaded: %d", [self.videoCamera captureSessionLoaded]);
}
#pragma mark - Protocol CvVideoCameraDelegate
- (void)processImage:(cv::Mat&)image
{
// Do some OpenCV stuff with the image
cv::Mat image_copy;
cvtColor(image, image_copy, CV_BGRA2BGR);
// invert image
bitwise_not(image_copy, image_copy);
cvtColor(image_copy, image, CV_BGR2BGRA);
}
@end
我在iPhone 4上测试过,效果不错。也许iPhone 3GS没有前置摄像头?然后你必须改变这一行:self.videoCamera.defaultAVCaptureDevicePosition = AVCaptureDevicePositionFront
到self.videoCamera.defaultAVCaptureDevicePosition = AVCaptureDevicePositionBack