Solved

Passing argument to a function

Posted on 2010-08-20
8
542 Views
Last Modified: 2012-08-13
Hello experts!

Have a look at my source code.

Two questions if I may:

1. How can I pass argument to this function when debugging using VC++
2. How can I pass argument to this function when running using console?

Thank you!

panJames

 
#include <cv.h>

#include <highgui.h>

#include <math.h>



using namespace cv;



int main(int argc, char** argv)

{

    Mat src, dst, color_dst;

    if( argc != 2 || !(src=imread(argv[1], 0)).data)

        return -1;



    Canny( src, dst, 50, 200, 3 );

    cvtColor( dst, color_dst, CV_GRAY2BGR );





    vector<Vec4i> lines;

    HoughLinesP( dst, lines, 1, CV_PI/180, 80, 30, 10 );

    for( size_t i = 0; i < lines.size(); i++ )

    {

        line( color_dst, Point(lines[i][0], lines[i][1]),

            Point(lines[i][2], lines[i][3]), Scalar(0,0,255), 3, 8 );

    }



    namedWindow( "Source", 1 );

    imshow( "Source", src );



    namedWindow( "Detected Lines", 1 );

    imshow( "Detected Lines", color_dst );



    waitKey(0);

    return 0;



}

Open in new window

#include <cv.h>

#include <highgui.h>

#include <math.h>



using namespace cv;



int main(int argc, char** argv)

{

    Mat src, dst, color_dst;

    if( argc != 2 || !(src=imread(argv[1], 0)).data)

        return -1;



    Canny( src, dst, 50, 200, 3 );

    cvtColor( dst, color_dst, CV_GRAY2BGR );





    vector<Vec4i> lines;

    HoughLinesP( dst, lines, 1, CV_PI/180, 80, 30, 10 );

    for( size_t i = 0; i < lines.size(); i++ )

    {

        line( color_dst, Point(lines[i][0], lines[i][1]),

            Point(lines[i][2], lines[i][3]), Scalar(0,0,255), 3, 8 );

    }



    namedWindow( "Source", 1 );

    imshow( "Source", src );



    namedWindow( "Detected Lines", 1 );

    imshow( "Detected Lines", color_dst );



    waitKey(0);

    return 0;



}

Open in new window

0
Comment
Question by:panJames
  • 3
  • 3
  • 2
8 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 33485886
Well what kind of argument do you want to pass to which function in the above?
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 33485981
Both programs seem to accept an image filename is the one and only argument, so you could call it like this eg. :

        my_application.exe my_image.ext
0
 

Author Comment

by:panJames
ID: 33485982
According to website I took example from:

http://opencv.willowgarage.com/documentation/cpp/feature_detection.html?highlight=hough#HoughLinesP

it should be file name.

/* This is a standalone program. Pass an image name as a first parameter
of the program. */

For me it looks like integer should be first...

panJames
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 33486010
>> For me it looks like integer should be first...

argv[0] is the executable name
argv[1] is the first parameter

argc contains the amount of items in the argv array (including the executable name)

So the (argc != 2) check makes sure that  argc is equal to 2 (the executable name plus one parameter), and the one parameter is then passed to the imread function.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 86

Assisted Solution

by:jkr
jkr earned 20 total points
ID: 33486028
Well, your call

HoughLinesP( dst, lines, 1, CV_PI/180, 80, 30, 10 );

pretty much matches the declaration:

void HoughLinesP(Mat& image, vector<Vec4i>& lines, double rho, double theta, int threshold, double minLineLength=0, double maxLineGap=0);

except that you are passing integers instead of doubles, so that should rather be

HoughLinesP( dst, lines, 1.0, CV_PI/180.0, 80.0, 30.0, 10.0 );
0
 

Author Comment

by:panJames
ID: 33486193
To Infinity08

I typed in console as you said:

  my_application.exe my_image.ext

And it works fine!

Two questions though:

1. In my main function

int main(int argc, char** argv)

first argument argc is integer, why don't I actually pass it?

2. How can I pass my arguments to main in VC++ for checking how it works during debuggin?

panJames



0
 
LVL 53

Accepted Solution

by:
Infinity08 earned 480 total points
ID: 33486238
>> first argument argc is integer, why don't I actually pass it?

The operating system takes care of that for you. All the parameters you pass on the command line will be placed in the argv array, and argc will contain the amount of items in that array.


>> 2. How can I pass my arguments to main in VC++ for checking how it works during debuggin?

I'll leave this one for someone who actually uses VC++. But there should be an option to set the command line arguments for debugging. jkr ?
0
 
LVL 86

Expert Comment

by:jkr
ID: 33486357
You can enter your arguments in the Project Settings, category 'Debug'.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now