zoukankan      html  css  js  c++  java
  • OpenCV 安装与调试

    Visual Studio 是微软提供的面向任何开发者的同类最佳工具。

    OpenCV(开源计算机视觉库)是一个开源的计算机视觉和机器学习软件库。

    目前最新版本:Visual Studio 2019、OpenCV 4.1.1

    本文先介绍如何在本地计算机 Windows 7 x64 上安装 OpenCV,然后介绍如何在 VS2019 中配置 OpenCV 开发环境,最后创建一个从图片中提取人脸的 c++ 程序。

    1、安装 OpenCV

    在官网下载你需要的 exe 版本(这里选择的 opencv-4.1.1-vc14_vc15.exe),然后双击它(即安装),实质就是解压一下,解压完出来一个文件夹(如:D:UsersDownloads)。

    解压后的文件夹内容如下:

    其中 build 是 OpenCV 使用时要用到的一些库文件,而 sources 中则是 OpenCV 官方为我们提供的一些 demo 示例源码。

    (说明:如果要在未安装 OpenCV 的机器上运行调用了 OpenCV 接口的程序,只需把 OpenCV  安装目录下把相应的 lib 文件复制到程序运行目录下即可,类似 MFC 程序的部署。如:D:UsersDownloadsopencvuildx64vc15libopencv_world411.dll)

    最后依次选择计算机—>属性—>高级系统设置—>环境变量,找到Path变量,选中并点击编辑,添加 D:UsersDownloadsopencvuildx64vc15in; 

    现在搭建好了运行 OpenCV 程序的环境,接下来就结合 VS2019 来编写需要的程序。(VS2019 的安装步骤可以参考官网上的安装说明,注意要选择 c++ 模块)

    二、在 VS2019 中配置 OpenCV

    1.添加空项目(c++)

    右键点击项目->添加->添加新项,选择 main.cpp 文件,如下图:

    在视图菜单中选择其他窗口->属性管理器,如下图:

    然后右击 Debug | 64,选择属性,进行参数配置,如下图:

     

    首先配置包含目录(即添加 OpenCV 头文件所在的目录),如果“计算的值”下面包含了以前的文件目录,并需要删除,可以先找到相应的配置文件,然后进行清理。

    如:C:UserszhangsanAppDataLocalMicrosoftMSBuildv4.0Microsoft.Cpp.x64.user.props

    类似地配置好库目录,如下图:

    接下来还需配置链接器的依赖项(调试模式则选择 opencv_world411d.lib,发布模式则选择 opencv_world411.lib),如下图:

    其中的库文件的位置在 OpenCV 的安装目录下:

     三、从图片中提取人脸的程序 

    #include <iostream>
    #include <opencv2/opencv.hpp>
    #include <opencv2/calib3d/calib3d.hpp>
    
    using namespace std;
    using namespace cv;
    
    int main()
    {
        String facefile = "D:\Users\wzl\Downloads\opencv\build\etc\haarcascades\haarcascade_frontalface_alt.xml";
        String eyefile = "D:\Users\wzl\Downloads\opencv\build\etc\haarcascades\haarcascade_eye.xml";
        Mat image, image_gray;      //定义两个Mat变量,用于存储每一帧的图像
    
        image = imread("F:\test.jpg");
        //imshow("原图", image);
    
        cvtColor(image, image_gray, COLOR_RGB2GRAY);//转为灰度图
        equalizeHist(image_gray, image_gray);//直方图均衡化,增加对比度方便处理
    
        CascadeClassifier eye_Classifier;  //载入分类器
        CascadeClassifier face_cascade;    //载入分类器
    
        //加载分类训练器,OpenCv官方文档提供的xml文档,可以直接调用
        if (!eye_Classifier.load(eyefile))  //需要将xml文档放在自己指定的路径下
        {
            cout << "Load haarcascade_eye.xml failed!" << endl;
            return 0;
        }
        if (!face_cascade.load(facefile))
        {
            cout << "Load haarcascade_frontalface_alt failed!" << endl;
            return 0;
        }
    
        //vector 是个类模板 需要提供明确的模板实参 vector<Rect>则是个确定的类 模板的实例化
        vector<Rect> eyeRect;
        vector<Rect> faceRect;
    
        //检测关于眼睛部位位置
        //eye_Classifier.detectMultiScale(image_gray, eyeRect, 1.1, 2, 0 | 1, Size(30, 30));
        //for (size_t eyeIdx = 0; eyeIdx < eyeRect.size(); eyeIdx++)
        //{
        //    rectangle(image, eyeRect[eyeIdx], Scalar(0, 0, 255)); //用矩形画出检测到的位置
        //}
    
        //检测关于脸部位置
        face_cascade.detectMultiScale(image_gray, faceRect, 1.1, 2, 0 | 1, Size(50, 50));
            
        for (size_t i = 0; i < faceRect.size(); i++)
        {
            //用矩形画出检测到的位置(一个脸对应一个矩形框)
            rectangle(image, faceRect[i], Scalar(0, 0, 255));     
    
            //保存脸部图片
            Mat face = image(Rect(faceRect[i]));
            Mat faceLittle;
            resize(face, faceLittle, Size(100, 100));
    
            Mat faceLittle_gray;
            cvtColor(faceLittle, faceLittle_gray, COLOR_RGB2GRAY);//转为灰度图
            equalizeHist(faceLittle_gray, faceLittle_gray);//直方图均衡化,增加对比度方便处理
    
            imwrite("F:\testface.jpg", faceLittle_gray);
        }    
        
        imshow("人脸识别图", image); //显示当前帧
        
        waitKey(0);
    }

    以上程序的允许结果如下图:

     

    通过摄像机来提取人脸照片:

    #include <opencv2/opencv.hpp>
    #include <opencv2/core.hpp>
    #include <opencv2/imgproc.hpp>
    #include <opencv2/highgui.hpp>
    #include <iostream>
    #include <string>
    #include <stdlib.h>
    
    using namespace std;
    using namespace cv;
    
    void faceDetect()
    {
        String facefile = "D:\Users\Downloads\opencv\build\etc\haarcascades\haarcascade_frontalface_alt.xml";
        //脸部识别分类器
        CascadeClassifier faceCascader;
        if (!faceCascader.load(facefile)) {
            printf("无法加载脸部特征文件:%s", facefile);
            return;
        }
        namedWindow("摄像头");
        VideoCapture capture(0);//打开摄像头
        Mat frame;
        Mat gray;
        vector<Rect> faces;
        int sn = 0;
        //实时读取摄像头的图像帧
        while (capture.read(frame)) {
            cvtColor(frame, gray, COLOR_RGB2GRAY);
            equalizeHist(gray, gray);
            faceCascader.detectMultiScale(gray, faces, 1.2, 3, 0, Size(30, 30));
            for (size_t faceSize = 0; faceSize < faces.size(); faceSize++)
            {
                Rect roi;
                roi.x = faces[static_cast<int>(faceSize)].x;
                roi.y = faces[static_cast<int>(faceSize)].y;
                roi.width = faces[static_cast<int>(faceSize)].width;
                roi.height = faces[static_cast<int>(faceSize)].height;
    
                Mat faceROI = frame(roi);
    
                //在人脸区域画一个矩形
                rectangle(frame, faces[static_cast<int>(faceSize)], Scalar(0, 0, 255), 2, 8, 0);
                sn++;
    
                //将sn整型值转为字符串
                stringstream stream;
                stream << sn;
    
                //生产一个新的文件名
                String snStr = "F:\testface\img-" + stream.str() + ".jpg";
                cout << snStr << endl;
                imwrite(snStr, faceROI);
            }
    
            imshow("摄像头", frame);
    
            //必须加时延,否则无法显示图像
            char key = waitKey(30);
    
            //按ESC键退出
            if (key == 27) {
                break;
            }
        }
    }
  • 相关阅读:
    C# richtextbox 自动下拉到最后 方法 & RichTextBox读取txt中文后出现乱码
    SQL基础问题整理
    xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!
    xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!
    xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!
    xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!
    xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!
    Springcloud +redis集群
    详述一次大量删除导致MySQL慢查的分析
    2019年开源数据库报告发布:MySQL仍卫冕!
  • 原文地址:https://www.cnblogs.com/hellowzl/p/11428206.html
Copyright © 2011-2022 走看看