我运行此示例鳕鱼,并且出现运行时异常
#include "stdafx.h"
#include <iostream>
using namespace std;
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"
using namespace cv;
int _tmain(int argc, char** argv)
{
//IplImage* img = cvLoadImage( "Walk1001.jpg" ,1 );
IplImage* img =cvLoadImage( argv[1] );
if(!img)
cout << "Could not open or find the image" << endl ;
cvNamedWindow( "Example1", 1 );
cvShowImage( "Example1", img );
cvWaitKey(0);
cvReleaseImage( &img );
cvDestroyWindow( "Example1" );
return 0;
}
当我使用
IplImage* img = cvLoadImage( "Walk1001.jpg" ,1 );
而不是此IplImage* img =cvLoadImage( argv[1] );
程序运行正常时。但否则我出错了。与
argv
有什么关系。我遇到了很多程序,这些程序通过argv[]
语法加载图像!如何使用此数组(argv[]
)或其他?
最佳答案
要使用argv数组,您必须向程序提供参数(从cmdline或类似命令)
prog.exe Walk1001.jpg 19
现在argv拥有3个元素,[“prog.exe”,“Walk1001.jpg”,“19”]和argc == 3
在您的程序中,执行以下操作:
char * imgPath="Walk1001.jpg"; // having defaults is a good idea
if ( argc > 1 ) // CHECK if there's actual arguments !
{
imgPath = argv[1]; // argv[0] holds the program-name
}
int number = 24;
if ( argc > 2 ) // CHECK again, if there's enough arguments
{
number = atoi(argv[2]); // you get the picture..
}
旁注:您似乎是一个初学者(没什么问题!),opencv api多年来已经发生了变化,请不要使用
IplImage*
和cv * Functions(1.0 api),使用cv::Mat和cv:: namespace 中的函数。
using namespace cv;
int main(int argc, char** argv)
{
char * imgPath="Walk1001.jpg";
if ( argc > 1 )
{
imgPath = argv[1];
}
Mat img = imread( imgPath );
if ( img.empty() )
{
cout << "Could not open or find the image" << endl ;
return 1;
}
namedWindow( "Example1", 1 );
imshow( "Example1", img );
waitKey(0);
// no, you don't have to release Mat !
return 0;
}
关于visual-studio-2010 - 使用argv []的opencv示例代码运行时错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15356513/