IOS OpenCV配置、使用

OpenCV下载地址:http://opencv.org


在使用的地方,把.m改成.mm

添加头文件

#ifdef __cplusplus

#import <opencv2/opencv.hpp>

// 图片转换

#import <opencv2/imgcodecs/ios.h>

#endif


- (IBAction)change:(id)sender {

    cv::Mat cvImage;

    UIImage *image = self.imageView.image;

    // Convert UIImage * to cv::Mat

    UIImageToMat(image, cvImage);

    

    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);

}


示例demo:https://github.com/MiftMy/XMOpenCV

你可能感兴趣的:(ios,opencv)