【OpenCV】imread读取数据为空

之前遇到一个很郁闷的问题,因为从用OpenCV2.3.1改成OpenCV2.4.4,开始改用Mat和imread来代替Iplimage和cvLoadImage,出了点小问题:imread读入数据总是为空。

程序为:

Mat image = imread ("lena.jpg");
imshow("lena",image);
cvWaitKey(0);
刚开始以为是路径的问题,后来偶然采用Iplimage获取图像,可以很好的读取图片:
IplImage *src;
src = cvLoadImage("lena.jpg"); //这里将lena.jpg和lena.cpp文件放在同一个文件夹下
cvNamedWindow("lena",CV_WINDOW_AUTOSIZE);
cvShowImage("lena",src);
cvWaitKey(0);
cvDestroyWindow("lena");
cvReleaseImage(&src);

搜了搜网上的说法,终于发现了原因(原链接已经丢失,可以自己搜搜看),原来是OpenCV配置出现问题!

是这样的,之前配置时为了省事儿,无论是Debug还是Release中的附加依赖项,都添加了所有的lib,也就是没有分Debug和Release。但实际上OpenCv是要分的,文件名尾带d的为Debug库函数,不带d的为Release库函数。只需将其中库函数分开添加进附加依赖项即可:

Debug库:

opencv_calib3d244d.lib
opencv_contrib244d.lib
opencv_core244d.lib
opencv_features2d244d.lib
opencv_flann244d.lib
opencv_gpu244d.lib
opencv_highgui244d.lib
opencv_imgproc244d.lib
opencv_legacy244d.lib
opencv_ml244d.lib
opencv_nonfree244d.lib
opencv_objdetect244d.lib
opencv_photo244d.lib
opencv_stitching244d.lib
opencv_ts244d.lib
opencv_video244d.lib
opencv_videostab244d.lib

Release库:

opencv_calib3d244.lib
opencv_contrib244.lib
opencv_core244.lib
opencv_features2d244.lib
opencv_flann244.lib
opencv_gpu244.lib
opencv_highgui244.lib
opencv_imgproc244.lib
opencv_legacy244.lib
opencv_ml244.lib
opencv_nonfree244.lib
opencv_objdetect244.lib
opencv_photo244.lib
opencv_stitching244.lib
opencv_ts244.lib
opencv_video244.lib
opencv_videostab244.lib

这样修改后,再运行程序,就能够正常读取图片啦!

你可能感兴趣的:(OpenCV)