iOS 下安装opencv 3.4.1
[TOC]
配置
建立xcode工程
下载opencv.framework
下载地址:https://www.opencv.org/releases.html
image.png将解压好的opencv2.framework拖动到工程中
image.png修改.m文件配置
需要将.m文件的Type设置为Objective-C++ Source,否则编译报错。
image.png
引入头文件
需要在引入UIKit之前引入,否则编译报错。
源码
//
// ViewController.m
// OpencvDemo
//
// Created by onefish on 2018/3/6.
// Copyright © 2018年 onefish. All rights reserved.
//
#import <opencv2/opencv.hpp>
#import <opencv2/imgproc/types_c.h>
#import <opencv2/imgcodecs/ios.h>
#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
网友评论