OpenCV iOS 摄像头特效(2

1.头文件加入接口

#import 
@interface ViewController : UIViewController

2.创建变量

@interface ViewController (){
    CvVideoCamera* videoCamera;
    UIImageView* imageView;
}

3.摄像头开始传入画面

{
    imageView= [[UIImageView alloc]initWithFrame:self.view.bounds];
    [self.view addSubview:imageView];
    imageView.layer.borderColor = [UIColor blackColor].CGColor;
    imageView.layer.borderWidth = 1;
    imageView.contentMode = UIViewContentModeScaleAspectFit;
    
    videoCamera = [[CvVideoCamera alloc] initWithParentView:imageView];
    videoCamera.delegate = self;//设置代理
    videoCamera.defaultAVCaptureDevicePosition = AVCaptureDevicePositionBack;//用背面摄像头
    videoCamera.defaultAVCaptureSessionPreset = AVCaptureSessionPresetLow; //截取图片大小
    videoCamera.defaultAVCaptureVideoOrientation = AVCaptureVideoOrientationPortrait;//竖屏
    videoCamera.defaultFPS = 30;
    videoCamera.grayscaleMode = NO;
    
    [videoCamera start];
}

4.接口实现

- (void)processImage:(cv::Mat&)image{
    Mat image_copy;
    cvtColor(image, image_copy, CV_BGRA2BGR);//提出无alpha通道的图片
    bitwise_not(image_copy, image_copy);//反向图片
    cvtColor(image_copy, image, CV_BGR2BGRA);//拷贝回去
}

不需要将图片再给到uiimageview,这里的image是引用,所以变化是会影响外部的

同样处理这里可以运用上一篇文章中用到处理图片的函数实现视频边缘处理

参考:
https://docs.opencv.org/2.4/doc/tutorials/ios/video_processing/video_processing.html#opencviosvideoprocessing
https://www.jianshu.com/p/eec47a44dd20
https://www.cnblogs.com/isItOk/p/5791576.html

你可能感兴趣的:(OpenCV iOS 摄像头特效(2)