0
votes

I am running a OpenCV tutorial where I am stuck at a problem displaying a message ! The program intention is to modify the image into gray image.

Here is the screenshot of the error.

     Debug assertion Failed!
     Program:..ments\Visual Studio 2008\Projects\new_demo\Debug\new_demo.exe
     File:f:\dd\vctools\crt_bld\self_x86\crt\src\xstring
     Line:1094
     Expression: Invalid null pointer
     Abort    Retry    Ignore

and this is my code new_demo.cpp



    #include "cv.h"
    #include "highgui.h"

    using namespace cv;

    int main( int argc, char** argv )
    {
     char* imageName = argv[1];

     Mat image;
     image = imread( imageName, 1 );

     if( argc != 2 || !image.data )
     {
       printf( " No image data \n " );
       return -1;
     }

     Mat gray_image;
     cvtColor( image, gray_image, CV_RGB2GRAY );

     imwrite( "new.jpg", gray_image );

     namedWindow( imageName, CV_WINDOW_AUTOSIZE );
     namedWindow( "Gray image", CV_WINDOW_AUTOSIZE );

     imshow( imageName, image );
     imshow( "Gray image", gray_image );

     waitKey(0);

     return 0;
    }

Help me with the solution?

1
Which line in your code throws the exception? - Osiris
there is no error in the code while building when i try to debug that pop up comes out! - Rahul

1 Answers

1
votes

cv::imread takes a reference to a constant std::string as its first parameter (filename).

See: http://docs.opencv.org/modules/highgui/doc/reading_and_writing_images_and_video.html?highlight=imread#Mat%20imread%28const%20string&%20filename,%20int%20flags%29

You call it with your char* imageName and you initialize it with argv[1] before you check if argc > 1. So if you run your program without an additional command line parameter, you try to construct a string from a nullpointer. You are lucky that the c++ standard (§ 3.6.1) says that argv[argc] shall be 0, otherwise you would have undefined behaviour already at this point by accessing and array outside its bounds. But now you are trying to construct a string from a nullpointer, which the standard (§ 21.4.2) forbids. ("Requires: s shall not be a null pointer.") See also: "http://en.cppreference.com/w/cpp/string/basic_string/basic_string

I recommend changing your code to something like this:

#include "opencv2/opencv.hpp"
#include <iostream>

int main( int argc, char** argv )
{
    using namespace cv;

    if( argc < 2 )
    {
        std::cout << "Please pass image file name as cmd param.\n"
        return -1;
    }

    Mat image;
    std::string imageName( argv[1] );
    image = imread( imageName , 1 );

    if( !image.data )
    {
        printf( " No image data \n " );
        return -1;
    }

    Mat gray_image;
    cvtColor( image, gray_image, CV_RGB2GRAY );

    imwrite( "new.jpg", gray_image );

    namedWindow( imageName, CV_WINDOW_AUTOSIZE );
    namedWindow( "Gray image", CV_WINDOW_AUTOSIZE );

    imshow( imageName, image );
    imshow( "Gray image", gray_image );

    waitKey(0);
}

btw: You can ommit default parameters in function calls. That's why they have default values. ;) So these calls would also do the job:

image = imread( imageName );
namedWindow( imageName );
namedWindow( "Gray image" );
waitKey();