VS2010 下安装 OpenCV 2.3.1

下载OpenCV2.3.1 安装 (我的系统是windows 7 32位的)

我的安装路径是C:\Program Files\OpenCV2.3.1

打开VS2010

1.配置include 路径

新建项目TestOpenCV,然后右键项目->属性,如下图:

VS2010 下安装 OpenCV 2.3.1_第1张图片

在包含目录加入
C:\Program Files\OpenCV2.3.1\build\include
C:\Program Files\OpenCV2.3.1\build\include\opencv
C:\Program Files\OpenCV2.3.1\build\include\opencv2
2.配置lib路径
在库目录加入
C:\Program Files\OpenCV2.3.1\build\x86\vc10\lib
单击应用
选择 连接器-->输入
VS2010 下安装 OpenCV 2.3.1_第2张图片
在附加依赖项中  增加所要包含的库就行了,
在Debug下加入:

opencv_calib3d231d.lib
opencv_contrib231d.lib
opencv_core231d.lib
opencv_features2d231d.lib
opencv_flann231d.lib
opencv_gpu231d.lib
opencv_highgui231d.lib
opencv_imgproc231d.lib
opencv_legacy231d.lib
opencv_ml231d.lib
opencv_objdetect231d.lib
opencv_ts231d.lib
opencv_video231d.lib

在Release下加入:

opencv_calib3d231.lib
opencv_contrib231.lib
opencv_core231.lib
opencv_features2d231.lib
opencv_flann231.lib
opencv_gpu231.lib
opencv_highgui231.lib
opencv_imgproc231.lib
opencv_legacy231.lib
opencv_ml231.lib
opencv_objdetect231.lib
opencv_ts231.lib
opencv_video231.lib

3.配置环境变量
单击【计算机】->【属性】->单击左侧的【高级系统设置】->单击【环境变量】,然后在用户变量中加入:Path变量,
即变量名为:Path,变量值为:C:\Program Files\OpenCV2.3.1\build\x86\vc10\bin;
由于有些函数需要TBB,所以需要将tbb所在的目录也加入到环境变量Path中。TBB相关的DLL路径为:
C:\Program Files\OpenCV2.3.1\build\common\tbb\ia32\vc10
如下图所示将OpenCV和TBB的dll文件所在的目录系统环境变量Path中。加入后可能需要注销当前Windows用户(或重启)后重新登陆才生效。
VS2010 下安装 OpenCV 2.3.1_第3张图片
 
4.编程测试配置是否成功
在TestOpenCV.cpp 实现文件 里 加入以下测试代码:
#include "stdafx.h"
#include <opencv2/opencv.hpp>
#include <cv.h>
#include <highgui.h>
#include <iostream>
using namespace std;
using namespace cv;


int _tmain(int argc, _TCHAR* argv[])
{
    IplImage * src= cvLoadImage("E:\\DataTest\\Lena.jpg");
    
    IplImage* hsv = cvCreateImage( cvGetSize(src), 8, 3 );
    IplImage* h_plane = cvCreateImage( cvGetSize(src), 8, 1 );
    IplImage* s_plane = cvCreateImage( cvGetSize(src), 8, 1 );
    IplImage* v_plane = cvCreateImage( cvGetSize(src), 8, 1 );
    IplImage* planes[] = { h_plane, s_plane };

    /** H 分量划分为16个等级,S分量划分为8个等级 */
    int h_bins = 16, s_bins = 8;
    int hist_size[] = {h_bins, s_bins};

    /** H 分量的变化范围 */
    float h_ranges[] = { 0, 180 }; 

    /** S 分量的变化范围*/
    float s_ranges[] = { 0, 255 };
    float* ranges[] = { h_ranges, s_ranges };

    /** 输入图像转换到HSV颜色空间 */
    cvCvtColor( src, hsv, CV_BGR2HSV );
    cvCvtPixToPlane( hsv, h_plane, s_plane, v_plane, 0 );

    /** 创建直方图,二维, 每个维度上均分 */
    CvHistogram * hist = cvCreateHist( 2, hist_size, CV_HIST_ARRAY, ranges, 1 );
    /** 根据H,S两个平面数据统计直方图 */
    cvCalcHist( planes, hist, 0, 0 );

    /** 获取直方图统计的最大值,用于动态显示直方图 */
    float max_value;
    cvGetMinMaxHistValue( hist, 0, &max_value, 0, 0 );


    /** 设置直方图显示图像 */
    int height = 240;
    int width = (h_bins*s_bins*6);
    IplImage* hist_img = cvCreateImage( cvSize(width,height), 8, 3 );
    cvZero( hist_img );

    /** 用来进行HSV到RGB颜色转换的临时单位图像 */
    IplImage * hsv_color = cvCreateImage(cvSize(1,1),8,3);
    IplImage * rgb_color = cvCreateImage(cvSize(1,1),8,3);
    int bin_w = width / (h_bins * s_bins);
    for(int h = 0; h < h_bins; h++)
    {
        for(int s = 0; s < s_bins; s++)
        {
            int i = h*s_bins + s;
            /** 获得直方图中的统计次数,计算显示在图像中的高度 */
            float bin_val = cvQueryHistValue_2D( hist, h, s );
            int intensity = cvRound(bin_val*height/max_value);

            /** 获得当前直方图代表的颜色,转换成RGB用于绘制 */
            cvSet2D(hsv_color,0,0,cvScalar(h*180.f / h_bins,s*255.f/s_bins,255,0));
            cvCvtColor(hsv_color,rgb_color,CV_HSV2BGR);
            CvScalar color = cvGet2D(rgb_color,0,0);

            cvRectangle( hist_img, cvPoint(i*bin_w,height),
                cvPoint((i+1)*bin_w,height - intensity),
                color, -1, 8, 0 );
        }
    }
    
    cvNamedWindow( "Source", 1 );    
    cvShowImage( "Source", src );

    cvNamedWindow( "H-S Histogram", 1 );
    cvShowImage( "H-S Histogram", hist_img );
    
    
    cvWaitKey(0);
    return 0;
}

测试效果图:

VS2010 下安装 OpenCV 2.3.1_第4张图片

到此配置成功。

你可能感兴趣的:(opencv)