iOS 下安装opencv 3.4.1

iOS 下安装opencv 3.4.1

[TOC]

配置

建立xcode工程

下载opencv.framework

下载地址:https://www.opencv.org/releases.html

iOS 下安装opencv 3.4.1_第1张图片
image.png

将解压好的opencv2.framework拖动到工程中

iOS 下安装opencv 3.4.1_第2张图片
image.png

修改.m文件配置

需要将.m文件的Type设置为Objective-C++ Source,否则编译报错。


iOS 下安装opencv 3.4.1_第3张图片
image.png

引入头文件

需要在引入UIKit之前引入,否则编译报错。

源码

//
//  ViewController.m
//  OpencvDemo
//
//  Created by onefish on 2018/3/6.
//  Copyright © 2018年 onefish. All rights reserved.
//
#import 
#import 
#import 

#import "ViewController.h"



@interface ViewController ()
{
    cv::Mat cvImage;
}
@property (weak, nonatomic) IBOutlet UIImageView *imageView;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    CGRect rect = [UIScreen mainScreen].bounds;
    self.imageView.frame = rect;
    
    // Convert UIImage * to cv::Mat
    UIImage *image = [UIImage imageNamed:@"IMG_1166.JPG"];
    
    UIImageToMat(image, cvImage);
    if (!cvImage.empty()) {
        cv::Mat gray;
        // Convert the image to grayscale;
        cv::cvtColor(cvImage, gray, CV_RGBA2GRAY);
        // Apply Gaussian filter to remove small edges
        cv::GaussianBlur(gray, gray, cv::Size(5,5), 1.2,1.2);
        // Calculate edges with Canny
        cv::Mat edges;
        cv::Canny(gray, edges, 0, 60);
        // Fill image with white color
        cvImage.setTo(cv::Scalar::all(255));
        // Change color on edges
        cvImage.setTo(cv::Scalar(0,128,255,255),edges);
        // Convert cv::Mat to UIImage* and show the resulting image
        self.imageView.image = MatToUIImage(cvImage);
    }
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end

你可能感兴趣的:(iOS 下安装opencv 3.4.1)