#include <iostream>
#include <opencv2/highgui.hpp>
using namespace cv;
using namespace std;
// Driver code
int main(int argc, char** argv)
{
//----- COMMAND LINE -----
const String& filename = argv[1];
Mat image = imread(argv[1]);
//----- EXPLICIT WAY -----
//const String& filename = "C:/Users/letto/OneDrive/Things/sonoio.jpg";
//Mat image = imread(filename);
// Error Handling
if (image.empty()) {
cout << "Image File "
<< "Not Found" << endl;
// wait for any key press
cin.get();
return -1;
}
// Show Image inside a window with
// the name provided
imshow("Window Name", image);
// Wait for any keystroke
waitKey(0);
return 0;
}
With the code above I'm trying to open an image.
There are two ways I'm trying to do it:
- COMMAND LINE: I pass the image url as a command;
- EXPLICIT WAY: I write explicitly the image url.
The second method works perfectly. With the first method I get this exception:
Exception thrown at 0x00007FFAC1FFF551 (ucrtbased.dll) in OpenImg.exe: 0xC0000005: Access violation reading location 0x0000000000000000.
I'm using Visual Studio Code 2022 so this is the way I'm passing the url through the command line:
Where is the error? Help me find out please, thanks!
CodePudding user response:
You have set - according to the image attached - additional command line arguments to the compiler and not to the app you run.
To add command lines to the app, right click on the project (OpenImg) and choose Debugging -> Command Arguments.
(And, as mentioned by @user4581301, verifying that the argument exists by checking args would've showed that accessing argv[1] would've been out of bounds. Its a good habit to learn.)