Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>cv::imread takes a reference to a constant std::string as its first parameter (filename).</p> <p>See: <a href="http://docs.opencv.org/modules/highgui/doc/reading_and_writing_images_and_video.html?highlight=imread#Mat%20imread%28const%20string&amp;%20filename,%20int%20flags%29" rel="nofollow">http://docs.opencv.org/modules/highgui/doc/reading_and_writing_images_and_video.html?highlight=imread#Mat%20imread%28const%20string&amp;%20filename,%20int%20flags%29</a></p> <p>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</p> <p>I recommend changing your code to something like this:</p> <pre><code>#include "opencv2/opencv.hpp" #include &lt;iostream&gt; int main( int argc, char** argv ) { using namespace cv; if( argc &lt; 2 ) { std::cout &lt;&lt; "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); } </code></pre> <p>btw: You can ommit default parameters in function calls. That's why they have default values. ;) So these calls would also do the job:</p> <pre><code>image = imread( imageName ); namedWindow( imageName ); namedWindow( "Gray image" ); waitKey(); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload