起因

还是为自己准备想自己前进!想研究一下opencv 素材!

参考

github 一

github 二

iOS结合OpenCV做视频流处理或人脸识别

整合demo

中国版github

该DEMO整合了 IJKPlayer GPUIMAGE OpenCv!等直播 视频相关的类!

附上部分测试效果

/**
 openCv  每秒回调30次 代理
 */
- (void)processImage:(cv::Mat &)image{
    
    //@[@"默认",@"反色",@"描边",@"人面识别",@"静态背景物体追踪"];
    switch (self.indexType) {
        case 0:{
            break;
        }
        case 1:{
            cv::Mat image_copy;
            cv::cvtColor(image, image_copy, CV_BGRA2BGR);
        
            // invert image
            cv::bitwise_not(image_copy, image_copy);
            cv::cvtColor(image_copy, image, CV_BGR2BGRA);
            
            break;
        }
        case 2:{
            cv::Mat gray;
            cv::cvtColor(image, gray, CV_RGB2GRAY);
            cv::GaussianBlur(gray, gray, cv::Size(5,5), 1.2,1.2);
            cv::Mat edges;
            Canny(gray,edges,0,50);
            image.setTo(cv::Scalar::all(255));
            image.setTo(cv::Scalar(0,128,255,255),edges);
            
            break;
        }
        case 3:{
            // 转为灰度
            cv::Mat image_copy;
            cv::cvtColor(image, image_copy, CV_BGR2GRAY);
            
            // 检测人脸并储存
            std::vector<cv::Rect>faces;
            faceDetector.detectMultiScale(image_copy, faces,1.1,2,0|CV_HAAR_SCALE_IMAGE,cv::Size(30,30));
            
            // 在每个人脸上画一个红色四方形
            for(unsigned int i= 0;i < faces.size();i++)
            {
                const cv::Rect& face = faces[i];
                cv::Point tl(face.x,face.y);
                cv::Point br = tl + cv::Point(face.width,face.height);
                
                // 四方形的画法
                cv::Scalar magenta = cv::Scalar(0, 0, 255);
                cv::rectangle(image, tl, br, magenta, 1, 4, 0);
                
//            Parameters:
//                img – Image.
//                pt1 – Vertex of the rectangle.
//                pt2 – Vertex of the rectangle opposite to pt1 .
//                rec – Alternative specification of the drawn rectangle.
//                color – Rectangle color or brightness (grayscale image).
//                thickness – Thickness of lines that make up the rectangle. Negative values, like CV_FILLED , mean that the function has to draw a filled rectangle.
//                lineType – Type of the line. See the line() description.
//                shift – Number of fractional bits in the point coordinates.
                
            }
            break;
        }
        case 4:{
            //背景要固定,并且单一内容,限制条件大!
            cv::Mat image_copy;
            cv::Mat frameDelta;
            cv::Mat thresh;
            
            cv::cvtColor(image, image_copy, CV_BGR2GRAY);
//            cv::GaussianBlur(image_copy, image_copy, cv::Size(21,21), 0, 0);
            
            if (firstMatFrame.data == NULL) {
                firstMatFrame = image_copy;
            }
            
            cv::absdiff(firstMatFrame,image_copy,frameDelta);
            cv::threshold(frameDelta, thresh, 50, 255, cv::THRESH_BINARY);

            cv::dilate(thresh, thresh, NULL);
            cv::Rect rect = cv::boundingRect(thresh);
            cv::Point tl(rect.x,rect.y);
            cv::Point br = tl + cv::Point(rect.width,rect.height);
            // 四方形的画法
            cv::Scalar magenta = cv::Scalar(0, 0, 255);
            cv::rectangle(image, tl, br, magenta, 1, 4, 0);
            break;
        }
        default:
            break;
    }
}

1 引用问题
//必须放在最前
#ifdef __cplusplus
#import <opencv2/opencv.hpp>
#endif
2 c++和oc 混编需要改文件后缀为 .mm
3 openVC 的输出imageview 要在imageview 显示后才创建