OpenCV之相机黑白处理

版权声明:漫漫长路,携手同行。 https://blog.csdn.net/WiKi_Su/article/details/78052401

Xcode的配置

导入头文件

#import <opencv2/videoio/cap_ios.h>

创建属性

@interface ViewController ()<CvVideoCameraDelegate>

@property (weak, nonatomic) IBOutlet UIImageView *imgV;
@property (nonatomic) CvVideoCamera *camera;
@property (nonatomic) BOOL  isColorCamera;

@end

创建相机

    _isColorCamera = YES;

    //创建相机并且设置载体
    _camera = [[CvVideoCamera alloc] initWithParentView:self.imgV];

    //设置摄像头的方向 (后置)
    _camera.defaultAVCaptureDevicePosition = AVCaptureDevicePositionBack;

    //设置相机回调处理
    _camera.delegate = self;

开启相机

    [_camera start];

彩色or黑白相机的选择

- (IBAction)caiseCamera:(id)sender {

    _isColorCamera = YES;
}


- (IBAction)fuguCamera:(id)sender {

    _isColorCamera = NO;
}

在CvVideoCameraDelegate回调中进行处理

- (void)processImage:(cv::Mat&)image{

    //默认是彩色相机
    if (!_isColorCamera) {
        //复古
        //1、彩色图片 -> 灰色图片
        Mat mat_image_gray ;
        cvtColor(image, mat_image_gray, COLOR_BGR2GRAY);

        //2、显示图片
        cvtColor(mat_image_gray, image, COLOR_GRAY2BGR);     
    }   
}

效果展示如下:

展开阅读全文

没有更多推荐了,返回首页