preface

  • OpenCV (Open Source Computer Vision Library) is an open source computer vision and machine learning software library that can complete image processing, face recognition, red eye removal, tracking moving objects, etc. More detailed instructions can be found on the OpenCV website

The library features more than 2,500 optimization algorithms, including a full set of classic and latest computer vision and machine learning algorithms. These algorithms can be used to detect and recognize faces to identify objects, classifying the human behavior of video, tracking the movement of the camera, track movement of the object, to extract the 3 d model of the object, from the stereo camera generated 3 d point cloud, the image stitching together to produce high-resolution images of the whole scene, search for similar images from image database, Eliminating red eyes from images taken with a flash, following eye movements, recognizing landscapes and building markers to overlay them with augmented reality. OpenCV has a community of more than 47,000 users with an estimated 18 million downloads. The library is widely used by companies, research groups and government agencies.

In addition to well-known companies using the library such as Google, Yahoo, Microsoft, Intel, IBM, Sony, Honda, And Toyota, many start-ups (such as Applied Minds, VideoSurf, and Zeitera) are using OpenCV extensively. OpenCV deployments include stitching together street View images, detecting incursions in Israeli surveillance videos, monitoring mining equipment in China, helping robots navigate and pick up objects at Willow Garage, detecting drowning accidents in European swimming pools, in Spain and New York, and examining debris on runways in Turkey, Check labels on products in factories around the world and then do a quick face test in Japan.

It has C ++, Python, Java, and MATLAB interfaces, and supports Windows, Linux, Android, and Mac OS. OpenCV is primarily oriented towards real-time visual applications and leverages MMX and SSE instructions when available. Fully functional CUDA and OpenCL interfaces are being actively developed. There are more than 500 algorithms and about 10 times as many functions that make up or support them. OpenCV is written natively in C ++ and has a templated interface that works seamlessly with STL containers.

Today we are going to talk about how to configure and use the Opencv framework on iOS

Import the project

1. Manual import

You can download the corresponding framework from the official website and throw it directly into the Xcode project

Two, Pod import

Pod 'OpenCV', '~ > 4.0.0'Copy the code

Iii. Use of documents

Use the C++ method in OpenCV implementation file suffix change to.mm, you can start using OpenCV methods

4. Import header files

#if __has_include(<opencv2/imgcodecs/ios.h>)

#include <iostream>
#import <opencv2/imgcodecs/ios.h>
#import <opencv2/core.hpp>
#import <opencv2/highgui.hpp>
#import <opencv2/imgproc.hpp>

using namespace cv;
using namespace std;
@implementation UIImage (KJOpencv)

...

@end
#endif
Copy the code

UIImage to C++ image

In OpenCV we also use CV ::Mat to represent an image, whereas in iOS we use UIImage to represent an image, so we need some way to convert it

cv::Mat src;
UIImageToMat(self,src,true);
Copy the code

6. Convert the resulting C++ image to UIImage

/* convert a c++ image to UIImage */ NS_INLINE UIImage * kMatToUIImage(const CV :: mat&image) {NSData *data = [NSData dataWithBytes:image.data length:image.elemSize()*image.total()]; CGColorSpaceRef colorSpace; if (image.elemSize() == 1) { colorSpace = CGColorSpaceCreateDeviceGray(); }else{ colorSpace = CGColorSpaceCreateDeviceRGB(); } CGDataProviderRef provider = CGDataProviderCreateWithCFData((__bridge CFDataRef)data); bool alpha = image.channels() == 4; / / kCGImageAlphaPremultipliedLast keep transparency CGBitmapInfo bitmapInfo = (alpha? kCGImageAlphaPremultipliedLast : kCGImageAlphaNone) | kCGBitmapByteOrderDefault; CGImageRef imageRef = CGImageCreate(image.cols, image.rows, 8, 8 * image.elemSize(), image.step.p[0], colorSpace, bitmapInfo, provider, NULL, false, kCGRenderingIntentDefault); UIImage *newImage = [UIImage imageWithCGImage:imageRef]; CGImageRelease(imageRef); CGDataProviderRelease(provider); CGColorSpaceRelease(colorSpace); return newImage; }Copy the code

Serial article association

1, how to use OpenCV in iOS project 2, iOS using OpenCV to adjust the image brightness and contrast (1) 3, iOS using OpenCV image fusion (2) 4, iOS using OpenCV filter processing (3)

Then I will slowly added Opencv related articles, temporarily to common image processing and image algorithms encapsulation, friends in need can be to pod ‘KJExtensionHandler/Opencv’

How to use OpenCV in iOS project introduction to this end, there are related to supplement, writing articles is not easy, but also please click **Little stars* * portal