• OpenCv的Java,C++开发环境配置


    1.OpenCV 下载及安装配置

    opencv的下载地址:http://opencv.org/downloads.html

    最新版本:opencv3.0.0

    注意:支持的visual studio2013

    我们可以下载稳定版本:opencv2.4.11

    安装:双击opencv-2.4.11解压到某一目录下即可

    配置:在系统环境变量Path中,添加相应的路径。

          32位添加:C:opencvopencv2.4.11uildx86vc10in

          64位添加:C:opencvopencv2.4.11uildx86vc10in 和 C:opencvopencv2.4.11uildx86vc10in

    2.VisualStudio 下 OpenCV调试

    新建一个控制台程序

    image

    按照导航一步步完成项目创建

    配置项目的include目录,指向opencv的安装目录,如下图所示

    image

    配置项目依赖的lib目录及依赖的库,如下图所示

    image

    image

    环境配置好后,编写测试代码

    #include "stdafx.h"
    #include <opencv2/opencv.hpp>
    #include <iostream>
    #include <fstream>
    #include <sstream>
    #include <math.h>
    
    void train_and_test_lda()
    {
        string fn_csv = string("at.txt");
        //string fn_csv = string("feret.txt");
        vector<Mat> allImages,train_images,test_images;
        vector<int> allLabels,train_labels,test_labels;
        try {
            read_csv(fn_csv, allImages, allLabels);
        } catch (cv::Exception& e) {
            cerr <<"Error opening file "<< fn_csv <<". Reason: "<< e.msg << endl;
            // 文件有问题,我们啥也做不了了,退出了
            exit(1);
        }
        if(allImages.size()<=1) {
            string error_message ="This demo needs at least 2 images to work. Please add more images to your data set!";
            CV_Error(CV_StsError, error_message);
        }
    
        for(int i=0 ; i<allImages.size() ; i++)
            equalizeHist(allImages[i],allImages[i]);
    
        int photoNumber = allImages.size();
        for(int i=0 ; i<photoNumber ; i++)
        {
            if((i%g_photoNumberOfOnePerson)<g_howManyPhotoForTraining)
            {
                train_images.push_back(allImages[i]);
                train_labels.push_back(allLabels[i]);
            }
            else
            {
                test_images.push_back(allImages[i]);
                test_labels.push_back(allLabels[i]);
            }
        }
    
        /*Ptr<FaceRecognizer> model = createEigenFaceRecognizer();//定义pca模型  
        model->train(train_images, train_labels);//训练pca模型,这里的model包含了所有特征值和特征向量,没有损失  
        model->save("eigenface.yml");//保存训练结果,供检测时使用  */
    
        Ptr<FaceRecognizer> fishermodel = createFisherFaceRecognizer();  
        fishermodel->train(train_images,train_labels);//用保存的降维后的图片来训练fishermodel,后面的内容与原始代码就没什么变化了  
        fishermodel->save("fisherlda.yml");
        int iCorrectPrediction = 0;
        int predictedLabel;
        int testPhotoNumber = test_images.size();
        for(int i=0;i<testPhotoNumber;i++)
        {
            predictedLabel = fishermodel->predict(test_images[i]);
            if(predictedLabel == test_labels[i])
                iCorrectPrediction++;
        }
        string result_message = format("Test Number = %d / Actual Number = %d.", testPhotoNumber, iCorrectPrediction);
        cout << result_message << endl;
        cout<<"accuracy = "<<float(iCorrectPrediction)/testPhotoNumber<<endl;
    }
    
    int main() {
    
        cout<<"lda = "<<endl;
        train_and_test_lda();
        return 0 ;
    }

    编译通过后,正常运行说明opencv环境配置正确。

    3.Eclipse下OpenCV开发环境配置

    在eclipse中,新建java project,如下图所示

    image

    新建成功后,修改项目的build path,增加外部jar,如下图所示

    image

    选择C:opencvopencv2.4.11uildjavaopencv-2411.jar

    image

    点开opencv-2411.jar,选择Native library location, 点击edit

    32位系统指向:C:opencvopencv2.4.11uildjavax86

    64位系统指向:C:opencvopencv2.4.11uildjavax64

    编写测试代码:

    import org.opencv.core.Core;
    import org.opencv.core.CvType;
    import org.opencv.core.Mat;
    
    public class TestOpencv {
        public static void main( String[] args )
           {
              System.loadLibrary( Core.NATIVE_LIBRARY_NAME );
              Mat mat = Mat.eye( 3, 3, CvType.CV_8UC1 );
              System.out.println( "mat = " + mat.dump() );
           }
    }

    运行输出如下,即环境配置正确:

    image

  • 相关阅读:
    【tips】Clion添加Qt gui绘制快捷方式
    conda配置安装pytorch tensorflow-gpu
    用当前最新版vs2019编译opencv最新版4.3.0遇到的问题---
    cuda报错: nvcc fatal : Host compiler targets unsupported OS
    C++中结构体与类的区别(struct与class的区别)
    cmake
    Windows 下配置Boost MPI
    VC----MFC对象的创建总结
    VC++、MFC最好的开源项目
    机械设计人员怎么学习电控?
  • 原文地址:https://www.cnblogs.com/zhulongchao/p/4646570.html
Copyright © 2020-2023  润新知