首页 > 代码库 > vs2008调用opencv2.4.9的imread()函数失败解决办法
vs2008调用opencv2.4.9的imread()函数失败解决办法
这两天在看opencv的C++接口函数,刚开始就出现问题:
一个简单的显示图像的程序,就是运行不成功:
#include <opencv2/core/core.hpp> #include <opencv2/highgui/highgui.hpp> #include <string> using std::string; using namespace cv; int main() { string str="E:\\test\\LENA.BMP"; Mat img1= imread(str); namedWindow("LENA"); imshow("LENA",img1); waitKey(); return 1; }
开始还怀疑是不是自己太久没写代码,忘了文件绝对路径的写法,查了下,斜杠\是保留符号,所以要正确编译的话需要两个斜杠\\,并没有错。
用绝对路径的话就imread()这一步内存溢出:
图像直接放工程目录下的话读出来的图像就是空的:
debug加的库:
opencv_calib3d249d.lib
opencv_contrib249d.lib
opencv_core249d.lib
opencv_features2d249d.lib
opencv_flann249d.lib
opencv_gpu249d.lib
opencv_highgui249d.lib
opencv_imgproc249d.lib
opencv_legacy249d.lib
opencv_objdetect249d.lib
opencv_ts249d.lib
opencv_video249d.lib
opencv_nonfree249d.lib
opencv_ocl249d.lib
opencv_photo249d.lib
opencv_stitching249d.lib
opencv_superres249d.lib
opencv_videostab249d.lib
Release加的库:
opencv_calib3d249.lib
opencv_contrib249.lib
opencv_core249.lib
opencv_features2d249.lib
opencv_flann249.lib
opencv_gpu249.lib
opencv_highgui249.lib
opencv_imgproc249.lib
opencv_legacy249.lib
opencv_ml249d.lib
opencv_objdetect249.lib
opencv_ts249.lib
opencv_video249.lib
opencv_nonfree249.lib
opencv_ocl249.lib
opencv_photo249.lib
opencv_stitching249.lib
opencv_superres249.lib
opencv_videostab249.lib
2、vs2008不支持opencv2.4.9
查看添加包含库文件路径发现:D:\Program Files\opencv2.4.9\build\x86\vc10\lib,我采用的是vc10版本的库,而opencv2.4.9中没有vc9版本的。用vs2010重新新建了一个项目,结果运行成功,奔溃。。。
先前用的都是c接口的函数,估计没有大的影响,结果这次用c++接口函数则出现了这么个问题,还好只是这个简单程序调试时就发现了这个隐患,要不然后面搞大工程时遇上就麻烦了。
如果要在vs2008上使用opencv2.4.9,需要用cMake自己重新编译一个库。