读图像

时间:2012-02-07 18:54:40

标签: c image opencv

我正在尝试使用下面的代码使用OpenCV 2.1(仅在今天开始),C ++读取图像。 我看不出下面的问题是什么。谢谢你,如果你能提供一些帮助。

int main( int argc, char** argv )
{
int height,width,step,channels;

//Load the image and make sure that it loads correctly
IplImage* im = cvLoadImage("kermit.jpg", 1); 
if(!im )
{
    //Drop out if the image isn't found
    cout << "Failed to load: "<<"kermit.jpg"<<"\n";
return 0;
}
else
{
    cout<<"Image was loaded with success "<<"kermit.jpg"<<"\n";
return (0);
}

height=im->height;
width=im->width;
step=im->widthStep;
channels=im->nChannels;

cout<<"(height, width)"<<height<<width<<"\n";


cvNamedWindow("kermit.jpj",CV_WINDOW_AUTOSIZE);

cvShowImage( "kermit.jpg", im );
cvWaitKey(0);
cvDestroyWindow ("kermit.jpg");

return 0;
}

1 个答案:

答案 0 :(得分:1)

由于您没有告诉我们问题到底是什么,我想指出以下代码:

if(!im )
{
    //Drop out if the image isn't found
    cout << "Failed to load: "<<"kermit.jpg"<<"\n";
    return 0;
}
else
{
    cout<<"Image was loaded with success "<<"kermit.jpg"<<"\n";
    return (0);
}

这意味着如果加载失败则退出应用程序如果加载成功,则还退出。听起来不对,对吧?你不应该return成功。

您不需要else阻止,因此请将其删除并重试。其余代码似乎没问题。