visual-studio-2010 - 使用argv []的opencv示例代码运行时错误

标签 visual-studio-2010 opencv argv

我运行此示例鳕鱼,并且出现运行时异常

#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/

相关文章:

c++ - 动态转换在模板成员函数内部同时使用共享指针和弱指针,无需代码重复

python - 使用像素数组设置 opencv 图像/numpy 数组值

c - 如何可移植地使用 system 或 popen,但带有参数数组?

javascript - 使用 Node 运行程序时检查是否给出 ARGV

c++ - 将 argv 保存为 vector 或字符串

c++ - 错误映射 PBO cudaGraphicsResource

exception - 如何覆盖 Visual Studio Code Coverage 中的异常

c++ - Visual Studio 跳过代码 mfc C++

opencv - 打印 cv::Mat opencv

java - 检查图像是否有效(损坏)javaCV