From: http://blog.csdn.net/zzy7222872/article/details/6047446
以前用的是VC6.0+OpenCV1.0的组合,一直用的很好。一般的图像处理算法都可以实现,现在突然想搞一下立体视觉方面的东西,查看了OpenCV的手册,发现立体视觉上的大部分函数只有OpenCV1.0以上版本才有。于是乎准备鸟枪换炮上OpenCV2.0,一查资料,发现OpenCV2.0不能再VC6.0下跑,于是乎只好装VS2005,但是又怕装了以后和原来的VC6.0冲突,导致以前的工程都不能用。上网查了资料说装在不同路径下是没问题的。于是大胆安装在其他目录下,装好后果然不冲突,看来只要路径不同就没问题,微软的工程师也不是菜鸟啊。接下来搞定OpenCV2.0,我想在能用原来1.0版本的情况下也可以用2.0的版本,于是将OpenCV2.0装在了和原来版本不同的目录下,装完Ok,没有问题。接下来是配置。OpenCV2.0的配置没有原来1.0的简单,需要自己下载CMAKE来产生需要的dll和lib文件。一下参考了一篇帖子如下。
1.Opencv2.0下载地址:
http://www.opencv.org.cn/index.php/Download
安装时选择默认路径,安装后检查C:/OpenCV2.0/bin
是否已经被加入到环境变量PATH,如果没有,请加入。
加入后需要注销当前Windows用户(或重启)后重新登陆才生效。
2.配置IDE,打开VS2005,选择菜单:Tools -> Options,在弹出窗口中选择
Projects and Solutions -> VC++ Directories
首先,从“Show Directories for”的list box中选择“Include files”,
添加路径“C:/OpenCV2.0/include/opencv”。
3.添加库文件(library files),需要下载CMAKE,用来生产所需的库文件,cmake下载地址:
http://www.cmake.org/cmake/resources/software.html
打开cmake-gui。在第一个路径处选择opencv的安装路径,记得是CMakeLists.txt所在的那个路径。
第二个路径是 make得到的工程保存位置。
点击Configure并选择合适的编译器。选择完编译器之后需点击Configure两次,
最后点击Generate生成我们想要的工程文件(.lib .dll)。
4.打开make得到的工程保存位置中的OpenCV.sln文件,build solution后在工程目录下得到bin和lib的文件夹,
将bin/debug中的.dll文件复制到C:/OpenCV2.0/bin,将lib/debug中的.lib文件复制到C:/OpenCV2.0/lib,
5.添加库文件(library files),选择菜单:Tools -> Options,在弹出窗口中
选择Projects and Solutions -> VC++ Directories,首先,从“Show Directories for”的list box中
选择“Library files”,添加路径“C:/OpenCV2.0/lib”。选择“source files”,
添加路径“C:/OpenCV2.0/src/cv”,“C:/OpenCV2.0/src/cvaux”,“
C:/OpenCV2.0/src/cxcore”,“C:/OpenCV2.0/src/highgui”,“C:/OpenCV2.0/src/ml”。
6.建立project后要设置项目所需要的lib,
选择菜单Project -> Properties -> Configuration Properties -> Linker -> Input,
在additional dependencies中加入cxcore.lib cv.lib ml.lib cvaux.lib highgui.lib等需要的库。
注意加入的库文件要和第3步生成的文件名一致,假如在第3步中选择VS2005的编译器时,
生成的文件名含有200d(不知道其他的编译器会不会),此时加入的库文件应该
是cxcore200d.lib cv200d.lib ml200d.lib cvaux200d.lib highgui200d.lib。
每次新建一个project都要添加项目所需的lib。
按上面的提示配置完后拷贝了以OpenCV中文论坛上的2.0版本的helloworld程序,发现有fatal error C1010: unexpected end of file while looking的错误, 查了下是预编译头的问题,解决方法是在设置那C/C++的Category的选项框选择Precompiled Headers 然后再选择Not using precompiled headers,这样再编译一下,Ok通过。运行一下,终于看到了经典的lena照片。
现在情况如下,我在VC6.0平台下使用OpenCV1.0库,在VS2005下使用OpenCV2.0库,暂时运行正常,没有冲突。写段文章总结一下,防止以后忘了,想双平台运行的朋友也可以参考一下。如有Bug出现,也希望大家提出来。
=================================================================================
以下为个人整理的内容:
根据参考的那篇博文说:需要自己下载CMake来产生需要的dll和lib文件!
但是我是从网上下载了OpenCV2.4.6.0, 双击解压后,发现里面有现成的dll和lib文件,可以直接使用,不需要下载CMake来编译产生dll和lib文件。
本人实现了下,环境:vc2010 + OpenCV2.4.6.0
/*
环境:winXP + vc2010 + OpenCV2.4.6.0(http://jaist.dl.sourceforge.net/project/opencvlibrary/opencv-win/2.4.6/OpenCV-2.4.6.0.exe)
OpenCV2.4.6.0安装目录:D:\opensource\opencv
1. 添加头文件所在目录:
D:\opensource\opencv\modules\core\include
D:\opensource\opencv\modules\imgproc\include
D:\opensource\opencv\modules\highgui\include
注:以上三个目录可用“D:\opensource\opencv\build\include”一个代替!
2. 添加lib文件所在目录:
D:\opensource\opencv\build\x86\vc10\lib
3. 为项目添加使用到的lib文件:
opencv_core246d.lib;opencv_imgproc246d.lib;opencv_highgui246d.lib;
配置完成,编译成功!
运行时,可能会出现提示:缺少xxx.dll,这时只要将相关dll复制到可执行程序同一目录或将dll所在目录加入到Path环境变量中!
我采取的是将“D:\opensource\opencv\build\x86\vc10\bin”加入到Path环境变量中!
再次运行,OK!
*/
// Video Image PSNR and SSIM
#include <iostream> // for standard I/O
#include <string> // for strings
#include <iomanip> // for controlling float print precision
#include <sstream> // string to number conversion
#include <opencv2/imgproc/imgproc.hpp> // Gaussian Blur
#include <opencv2/core/core.hpp> // Basic OpenCV structures (cv::Mat, Scalar)
#include <opencv2/highgui/highgui.hpp> // OpenCV window I/O
using namespace std;
using namespace cv;
double getPSNR ( const Mat& I1, const Mat& I2);
Scalar getMSSIM( const Mat& I1, const Mat& I2);
static void help()
{
cout
<< "\n--------------------------------------------------------------------------" << endl
<< "This program shows how to read a video file with OpenCV. In addition, it tests the"
<< " similarity of two input videos first with PSNR, and for the frames below a PSNR " << endl
<< "trigger value, also with MSSIM."<< endl
<< "Usage:" << endl
<< "./video-source referenceVideo useCaseTestVideo PSNR_Trigger_Value Wait_Between_Frames " << endl
<< "--------------------------------------------------------------------------" << endl
<< endl;
}
int main(int argc, char *argv[])
{
help();
if (argc != 5)
{
cout << "Not enough parameters" << endl;
return -1;
}
stringstream conv;
const string sourceReference = argv[1], sourceCompareWith = argv[2];
int psnrTriggerValue, delay;
conv << argv[3] << argv[4]; // put in the strings
conv >> psnrTriggerValue >> delay;// take out the numbers
char c;
int frameNum = -1; // Frame counter
VideoCapture captRefrnc(sourceReference), captUndTst(sourceCompareWith);
if ( !captRefrnc.isOpened())
{
cout << "Could not open reference " << sourceReference << endl;
return -1;
}
if( !captUndTst.isOpened())
{
cout << "Could not open case test " << sourceCompareWith << endl;
return -1;
}
Size refS = Size((int) captRefrnc.get(CV_CAP_PROP_FRAME_WIDTH),
(int) captRefrnc.get(CV_CAP_PROP_FRAME_HEIGHT)),
uTSi = Size((int) captUndTst.get(CV_CAP_PROP_FRAME_WIDTH),
(int) captUndTst.get(CV_CAP_PROP_FRAME_HEIGHT));
if (refS != uTSi)
{
cout << "Inputs have different size!!! Closing." << endl;
return -1;
}
const char* WIN_UT = "Under Test";
const char* WIN_RF = "Reference";
// Windows
namedWindow(WIN_RF, CV_WINDOW_AUTOSIZE );
namedWindow(WIN_UT, CV_WINDOW_AUTOSIZE );
cvMoveWindow(WIN_RF, 400 , 0); //750, 2 (bernat =0)
cvMoveWindow(WIN_UT, refS.width, 0); //1500, 2
cout << "Frame resolution: Width=" << refS.width << " Height=" << refS.height
<< " of nr#: " << captRefrnc.get(CV_CAP_PROP_FRAME_COUNT) << endl;
cout << "PSNR trigger value " <<
setiosflags(ios::fixed) << setprecision(3) << psnrTriggerValue << endl;
Mat frameReference, frameUnderTest;
double psnrV;
Scalar mssimV;
for(;;) //Show the image captured in the window and repeat
{
captRefrnc >> frameReference;
captUndTst >> frameUnderTest;
if( frameReference.empty() || frameUnderTest.empty())
{
cout << " < < < Game over! > > > ";
break;
}
++frameNum;
cout <<"Frame:" << frameNum;
/ PSNR
psnrV = getPSNR(frameReference,frameUnderTest); //get PSNR
cout << setiosflags(ios::fixed) << setprecision(3) << psnrV << "dB";
MSSIM /
if (psnrV < psnrTriggerValue)
{
mssimV = getMSSIM(frameReference,frameUnderTest);
cout << " MSSIM: "
<< "R" << setiosflags(ios::fixed) << setprecision(3) << mssimV.val[2] * 100
<< "G" << setiosflags(ios::fixed) << setprecision(3) << mssimV.val[1] * 100
<< "B" << setiosflags(ios::fixed) << setprecision(3) << mssimV.val[0] * 100;
}
cout << endl;
// Show Image /
imshow( WIN_RF, frameReference);
imshow( WIN_UT, frameUnderTest);
c = (char)cvWaitKey(delay);
if (c == 27) break;
}
return 0;
}
double getPSNR(const Mat& I1, const Mat& I2)
{
Mat s1;
absdiff(I1, I2, s1); // |I1 - I2|
s1.convertTo(s1, CV_32F); // cannot make a square on 8 bits
s1 = s1.mul(s1); // |I1 - I2|^2
Scalar s = sum(s1); // sum elements per channel
double sse = s.val[0] + s.val[1] + s.val[2]; // sum channels
if( sse <= 1e-10) // for small values return zero
return 0;
else
{
double mse =sse /(double)(I1.channels() * I1.total());
double psnr = 10.0*log10((255*255)/mse);
return psnr;
}
}
Scalar getMSSIM( const Mat& i1, const Mat& i2)
{
const double C1 = 6.5025, C2 = 58.5225;
/***************************** INITS **********************************/
int d = CV_32F;
Mat I1, I2;
i1.convertTo(I1, d); // cannot calculate on one byte large values
i2.convertTo(I2, d);
Mat I2_2 = I2.mul(I2); // I2^2
Mat I1_2 = I1.mul(I1); // I1^2
Mat I1_I2 = I1.mul(I2); // I1 * I2
/*************************** END INITS **********************************/
Mat mu1, mu2; // PRELIMINARY COMPUTING
GaussianBlur(I1, mu1, Size(11, 11), 1.5);
GaussianBlur(I2, mu2, Size(11, 11), 1.5);
Mat mu1_2 = mu1.mul(mu1);
Mat mu2_2 = mu2.mul(mu2);
Mat mu1_mu2 = mu1.mul(mu2);
Mat sigma1_2, sigma2_2, sigma12;
GaussianBlur(I1_2, sigma1_2, Size(11, 11), 1.5);
sigma1_2 -= mu1_2;
GaussianBlur(I2_2, sigma2_2, Size(11, 11), 1.5);
sigma2_2 -= mu2_2;
GaussianBlur(I1_I2, sigma12, Size(11, 11), 1.5);
sigma12 -= mu1_mu2;
/ FORMULA
Mat t1, t2, t3;
t1 = 2 * mu1_mu2 + C1;
t2 = 2 * sigma12 + C2;
t3 = t1.mul(t2); // t3 = ((2*mu1_mu2 + C1).*(2*sigma12 + C2))
t1 = mu1_2 + mu2_2 + C1;
t2 = sigma1_2 + sigma2_2 + C2;
t1 = t1.mul(t2); // t1 =((mu1_2 + mu2_2 + C1).*(sigma1_2 + sigma2_2 + C2))
Mat ssim_map;
divide(t3, t1, ssim_map); // ssim_map = t3./t1;
Scalar mssim = mean( ssim_map ); // mssim = average of ssim map
return mssim;
}