为什么openCV的样本在visual studio中不起作用

时间:2015-01-30 04:51:07

标签: c++ visual-studio opencv

我试图在visual studio 2012中使用opencv(2.4.10)样本来获取c ++。 例如,我尝试运行display_image.cpp(来自opencv \ sources \ samples \ cpp \ tutorial_code \ introduction \ display_image)。我使用像E:\ image \ can.jpg这样的图像的命令参数进行调试,但它没有显示图像,所以我改变了下面的代码的一部分,它显示了图像。 源:

Mat image;
image = imread(argv[1], CV_LOAD_IMAGE_COLOR);
更改后

//Mat image;
//image = imread(argv[1], CV_LOAD_IMAGE_COLOR); // Read the file
IplImage *img = cvLoadImage(argv[1], 0);
Mat image(img)  ;

这就是我可以处理它但现在我想运行SURF_detector.cpp(来自opencv \ sources \ samples \ cpp \ tutorial_code \ features2D)需要拖曳图像进行调试所以我改变了代码所在的代码在顶部(处理读取图像),但代码尝试处理SURF算法,它给我一个错误,如下图所示:

enter image description here

来源如下:

/**
* @file SURF_detector
* @brief SURF keypoint detection + keypoint drawing with OpenCV functions
* @author A. Huaman
*/

#include <stdio.h>
#include <iostream>
#include "opencv2/core/core.hpp"
#include "opencv2/features2d/features2d.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/nonfree/features2d.hpp"

using namespace cv;
using namespace std;

void readme();

/**
* @function main
* @brief Main function
*/

int main( int argc,      // Number of strings in array argv
        char *argv[],   // Array of command-line argument strings
        char *envp[] )  // Array of environment variable strings
 {
//if( argc != 3 )
//{ readme(); return -1; }

//Mat img_1 = imread(argv[1], CV_LOAD_IMAGE_GRAYSCALE );
//Mat img_2 = imread(argv[2], CV_LOAD_IMAGE_GRAYSCALE );

IplImage *img1 = cvLoadImage(argv[1], 0);
Mat img_1(img1);
IplImage *img2 = cvLoadImage(argv[2], 0);
Mat img_2(img2);

if( !img_1.data || !img_2.data )
 { std::cout<< " --(!) Error reading images " << std::endl; return -1; }

 //-- Step 1: Detect the keypoints using SURF Detector
 int minHessian = 400;

 SurfFeatureDetector detector( minHessian );

 std::vector<KeyPoint> keypoints_1, keypoints_2;

 detector.detect( img_1, keypoints_1 );
 detector.detect( img_2, keypoints_2 );

 //-- Draw keypoints
 Mat img_keypoints_1; Mat img_keypoints_2;

 drawKeypoints( img_1, keypoints_1, img_keypoints_1, Scalar::all(-1), DrawMatchesFlags::DEFAULT );
 drawKeypoints( img_2, keypoints_2, img_keypoints_2, Scalar::all(-1), DrawMatchesFlags::DEFAULT );

 //-- Show detected (drawn) keypoints
 imshow("Keypoints 1", img_keypoints_1 );
 imshow("Keypoints 2", img_keypoints_2 );

 waitKey(0);

 return 0;
}

/**
* @function readme
*/
void readme()
 { std::cout << " Usage: ./SURF_detector <img1> <img2>" << std::endl; }

我如何修复它??????

1 个答案:

答案 0 :(得分:0)

假设你有opencv instllated。假设您要运行SURF_descriptor.cpp

此示例需要以下.dll(发布模式;对于调试模式,在点之前添加d)

opencv_core2410.dll 
opencv_features2d2410.dll
opencv_flann2410.dll
opencv_highgui2410.dll
opencv_imgproc2410.dll
opencv_ml2410.dll
opencv_nonfree2410.dll
opencv_objdetect2410.dll
opencv_ocl2410.dll

现在您需要告诉编译器头文件在哪里。在我的情况下

D:\CPP_Libraries\opencv_2.4.10\build\include

接下来,您需要告诉链接器.libs在哪里。在我的情况下

D:\CPP_Libraries\opencv_2.4.10\build\x86\vc12\lib

现在打开visual studio命令提示符并键入(这是用于发布模式)

cl /EHsc SURF_descriptor.cpp /Fetest.exe /I D:\CPP_Libraries\opencv_2.4.10\build\include /link /LIBPATH:D:\CPP_Libraries\opencv_2.4.10\build\x86\vc12\lib opencv_core2410.lib opencv_highgui2410.lib opencv_features2d2410.lib  opencv_nonfree2410.lib

上述行会生成test.exe 要运行它,您需要两张图片。我正在使用baboon.jpg

enter image description here

从cmd中输入

>test baboon.jpg baboon.jpg

结果是

enter image description here