opencv在ios上的簡單實現[objective-c&c++]
阿新 • • 發佈:2019-02-07
網上找過很多資料,不是缺少庫的引用就是缺少備註表述
所以自己實現一下並記錄下來
//
// ViewController.m
// h
//
// Created by Friderick_Lee on 2017/12/16.
// Copyright © 2017年 Friderick_Lee. All rights reserved.
//
#import <opencv2/imgproc/types_c.h>
#import <opencv2/opencv.hpp>
#import <OpenCV/opencv2/imgcodecs/ios.h>
#import "ViewController.h"
using namespace cv;
@interface ViewController ()
{
cv::Mat cvImage;
}
@property (weak, nonatomic) IBOutlet UIImageView *imgView;
@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.imgView.frame = rect;
UIImage *image = [UIImage imageNamed:@"n.png"];
//將UIImge轉換成mat型別方便c++處理
UIImageToMat(image, cvImage);
if(!cvImage.empty()){
cv::Mat gray;
// 將影象轉換為灰度顯示
cv::cvtColor(cvImage,gray,CV_RGB2GRAY);
// 應用高斯濾波器去除小的邊緣
cv::GaussianBlur(gray, gray, cv::Size(5,5), 1.2,1.2);
// 計算與畫布邊緣
cv::Mat edges;
cv::Canny(gray, edges, 0, 50);
// 使用白色填充
cvImage.setTo(cv::Scalar::all(225));
// 修改邊緣顏色
cvImage.setTo(cv::Scalar(0,128,255,255),edges);
// 將Mat轉換為Xcode的UIImageView顯示
self.imgView.image = MatToUIImage(cvImage);
}
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end