OpenCV中C++函数imread读取图片的问题

  今天在用OpenCV实验Image Pyramid的时候发现一个奇怪的问题,就是利用C++函数imread读取图片的时候返回的结果总是空,而利用C函数cvLoadImage时却能读取到图像。代码如下:

//环境:VS2010 + OpenCV 2.3.1

#include "stdafx.h"

#include <cv.h>
#include <highgui.h>


#include <math.h>
#include <stdlib.h>
#include <stdio.h>

using namespace cv;

Mat src, dst, tmp;
char* window_name = "Pyramids Demo";

int _tmain(int argc, _TCHAR* argv[])
{
printf("\n Zoom In-Out demo \n");
printf("-------------------- \n");
printf("*[u]-> Zoom in \n");
printf("*[d]-> Zoom out \n");
printf("*[ESC]-> Close program \n\n");

src = imread("D:\\fruits.jpg");
if(!src.data)
{
printf("No data!--Exiting the program \n");
return -1;
}

tmp = src;
dst = tmp;
namedWindow(window_name,CV_WINDOW_AUTOSIZE);
imshow(window_name,dst);

while(true)
{
int c;
c = waitKey(10);
if((char)c == 27)
{
break;
}
if((char)c == 'u')
{
pyrUp(tmp,dst,Size(tmp.cols * 2,tmp.rows * 2));
printf("** Zoom In:Image x 2\n");
}
else if((char)c == 'd')
{
pyrDown(tmp,dst,Size(tmp.cols / 2,tmp.rows / 2));
printf("**Zoom Out:Image / 2\n");
}
imshow(window_name,dst);
tmp = dst;
}
return 0;
}

  程序很简单,就是直接调用Imgproc中的两个C++函数pyrUp和pyrDown来实现图像金字塔,程序的详细解释可参见http://www.opencv.org.cn/opencvdoc/2.3.2/html/doc/tutorials/imgproc/pyramids/pyramids.html。但在实现的过程中发现imread始终读取不了图像数据和cvLoadImage却可以。几经考证,发现的确是由于库关联的问题。也就是在Debug模式下应该选择带'd'的lib,在Release模式下就选择不带'd'的lib。而实际我们在配置OpenCV环境的时候往往图方便将Debug和Release的目录都一起加了进去,再修改起来也比较麻烦。所以这时候最简单的办法就是在程序的开始加上:

#pragma comment(lib,"opencv_highgui231d.lib")

来告诉程序将采用Debug版本的库函数。
  实验结果如下:

 ( x 2) (x 1)(x 0.5)(x 0.25)

posted @ 2012-03-26 18:38  J_Outsider  阅读(104521)  评论(12编辑  收藏  举报