iOS 下安装opencv 3.4.1

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

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 176,131评论 25 709
  • DetectingFaces Demo 传送门相关书籍推荐instant-opencv-for-iosiOS Ap...
    hehtao阅读 5,871评论 0 5
  • ---赠朋友张赣摄影作品 微漾春水映镜台, 小景呈诗醉情怀。 滴水芦草才雨后, 几只欢鹅向客来。
    苍云法尔阅读 2,600评论 2 1
  • 直到我穿上黑色紧身牛仔裤,从寝室里踉跄出来,才察觉到原来立夏如此真实立体。黏糊糊的黑色液体原地蒸发着化脓了般的气泡...
    格格巫wudi阅读 2,548评论 0 0
  • 你,生若夏花爱若寒冬,死如枯木重视等不到春天的来临,你可知道她最轻的念想不过是和你一起仰望天堂。你可知道她最美的愿...
    浅默巷璃阅读 2,778评论 0 1

友情链接更多精彩内容