Ask Your Question
0

How to pass values to main function (vs2015,opencv,c++)

asked 2016-09-08 04:50:15 -0600

scholar gravatar image

if i have downloaded the code from somewhere lets say its abc.cpp and its having main as :

int main(int argc, char* argv[])
{
    Mat image;
    if (argc>1)
        image = imread(argv[1]);
    else
    {
        cout << "    Usage: " << argv[0] << " <input_image> [<gt_word1> ... <gt_wordN>]" << endl;
        return(0);
    }}


how should i run this code ?? normally from the vs platform??? how to pass the image to be read????

how can we give values to argc or argv??? i have gone thru all the other similar questions here but dint found any solution to my problem??

edit retag flag offensive close merge delete

Comments

2

argv will contain any strings passed as command line arguments, and argc will contain the number of arguments. So either run your executable from the command line with the image path added afterwards, or set up the command line arguments in VS. I can't remember how, but Google can :D

AJW gravatar imageAJW ( 2016-09-08 05:56:27 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
4

answered 2016-09-08 12:27:18 -0600

Guyygarty gravatar image

Go to the project properties and enter the arguments under "Debugging > Command Arguments"

image description

guy

edit flag offensive delete link more

Comments

3

Just an addition: the arguments passed this way will only be usable during the chosen configuration (in the screenshot, Debug). Duplicate your arguments to the other configuratio (Release), o write them down for "all" configurations

LorenaGdL gravatar imageLorenaGdL ( 2016-09-08 12:38:15 -0600 )edit

do u mean i have to copy the path of the image to be read in the field 'command arguments' in both debug and release??? i have tried this, its not reading the image....

int main(int argc, char* argv[])
   {
    cout << endl << argv[0] << endl; 
    Mat image;
if (argc>1)
    image = imread(argv[1]);
else
{
    cout << "    Usage: " << argv[0] << " <input_image> [<gt_word1> ... <gt_wordN>]" << endl;
    return(0);
}

cout << "IMG_W=" << image.cols << endl;
cout << "IMG_H=" << image.rows<<endl;

}

scholar gravatar imagescholar ( 2016-09-09 00:27:01 -0600 )edit

output is :

C:\Users\research lab\Documents\Visual Studio 2015\Projects\XtraModulesDemo\Debug\XtraModulesDemo.exe

IMG_W=0 IMG_H=0 OpenCV Error: Assertion failed (scn == 3 || scn == 4) in cv::ipp_cvtColor, file C:\opencv3.1\sources\modules\imgproc\src\color.cpp, line 7456

scholar gravatar imagescholar ( 2016-09-09 00:33:57 -0600 )edit
1

Does the program crash before the first cout?

if not, what does the program think the values of argc and argv[1] are?

guy

Guyygarty gravatar imageGuyygarty ( 2016-09-10 06:37:05 -0600 )edit

"C:\Users\research lab\Documents\Visual Studio 2015\Projects\XtraModulesDemo\Debug\XtraModulesDemo.exe " this is the value of argv[1]..

scholar gravatar imagescholar ( 2016-09-12 00:55:24 -0600 )edit
1

Don't pass the path to the executable as argument. Visual Studio already handles this. You need to directly pass the image full path as first argument

LorenaGdL gravatar imageLorenaGdL ( 2016-09-12 05:05:12 -0600 )edit

okii got ur point.. thanx alot... i was passing the path of the image only.. but the issue was that in the path a space was there like 'research lab' so it was taking argv[1] upto research only..

scholar gravatar imagescholar ( 2016-09-12 05:32:08 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2016-09-08 04:50:15 -0600

Seen: 1,286 times

Last updated: Sep 08 '16