dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9

Thread: Using "argv" to pass image files C++

  1. #1
    Join Date
    Oct 2015
    Posts
    18

    Using "argv" to pass image files C++

    Hello to you all. Using Visual Studio 2012, I am currently testing a program of image segmentation and need to somehow pass a test image file in the main program as shown below. Been searching for a while. I would appreciate an example of howto use or replace the "argv" with the test image file and consequently produce an output file. Much appreciate your guidance!

    http://cs.brown.edu/~pff/segment/[^]

    Code:
    int main(int argc, char **argv) {
      if (argc != 6) {
        fprintf(stderr, "usage: %s sigma k min input(ppm) output(ppm)\n", argv[0]);
        return 1;
      }
      
      float sigma = atof(argv[1]);
      float k = atof(argv[2]);
      int min_size = atoi(argv[3]);
    	
      printf("loading input image.\n");
      image<rgb> *input = loadPPM(argv[4]);
    	
      printf("processing\n");
      int num_ccs; 
      image<rgb> *seg = segment_image(input, sigma, k, min_size, &num_ccs); 
      savePPM(seg, argv[5]);
     
      printf("got %d components\n", num_ccs);
      printf("done! uff...thats hard work.\n");
     
      return 0;
    }

  2. #2
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,605

    Re: Using "argv" to pass image files C++

    Code:
    image<rgb> *input = loadPPM(argv[4]);
    As far as I understand, the fourth command line parameter must be a path to a file. Are you aware of what "command line parameter" is?

    PS. Your question seems hardly having anything to do neither with images nor Visual Studio 2012. So please avoid providing unnecessary details in future that might obscure the essence of your issue.
    Best regards,
    Igor

  3. #3
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,605

    Re: Using "argv" to pass image files C++

    Quote Originally Posted by DavyS View Post
    I would appreciate an example of howto use ... the "argv"
    Code:
    #include <stdio.h>
    
    int main(int argc, char** argv)
    {
        for (int idx = 0; idx < argc; idx++)
            printf("[%d] %s\n", idx, argv[idx]);
    
        return 0;
    }
    Code:
    J:\Temp\102>102.exe first second "the third" fourth
    [0] J:\Temp\102\102.exe
    [1] first
    [2] second
    [3] the third
    [4] fourth
    Best regards,
    Igor

  4. #4
    Join Date
    Oct 2015
    Posts
    18

    Re: Using "argv" to pass image files C++

    Quote Originally Posted by Igor Vartanov View Post
    Code:
    #include <stdio.h>
    
    int main(int argc, char** argv)
    {
        for (int idx = 0; idx < argc; idx++)
            printf("[%d] %s\n", idx, argv[idx]);
    
        return 0;
    }
    Code:
    J:\Temp\102>102.exe first second "the third" fourth
    [0] J:\Temp\102\102.exe
    [1] first
    [2] second
    [3] the third
    [4] fourth
    Thanks for your input.

  5. #5
    Join Date
    Oct 2015
    Posts
    18

    Re: Using "argv" to pass image files C++

    [QUOTE=Igor Vartanov;2188839]
    Code:
    image<rgb> *input = loadPPM(argv[4]);
    As far as I understand, the fourth command line parameter must be a path to a file. Are you aware of what "command line parameter" is?
    QUOTE]

    I right-clicked on my project in Solution Explorer > Configuration Properties> Debugging.
    In Command Arguments I typed in:

    Code:
    "C:\ Projects\segment\Debug>segment.exe" 0.5 500 20 "C:\ Projects\segment\segment\image.ppm" 
    "C:\ Projects\segment\segment\output.ppm"
    trying to make sure I didn't leave any unnecessary spaces. However, argc = 7 when outputted though it should be 6.

    I am trying to follow the code below, so argc should be 6 but it's currently 7. I would appreciate any guidance on how to resolve this problem, thanks.

    Code:
    	
    if (argc != 6) 
    	{
    		fprintf(stderr, "usage: %s sigma k min input(ppm) output(ppm)\n", argv[0]);
    		system("pause");
    		return 1;
    }

  6. #6
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,027

    Re: Using "argv" to pass image files C++

    Use Igor's code with your arguments to display the contents of each of the argv elements to see what is happening.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2019 (16.5.1)

  7. #7
    Join Date
    Oct 2015
    Posts
    18

    Re: Using "argv" to pass image files C++

    Quote Originally Posted by 2kaud View Post
    Use Igor's code with your arguments to display the contents of each of the argv elements to see what is happening.
    Thanks. I noticed that the first part "C:\ Projects\segment\Debug>segment.exe" was unnecessary as it were increasing the value of argc by 1.

    argv[0] returned the *.exe without it being manually typed into Command Arguments.

  8. #8
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,027

    Re: Using "argv" to pass image files C++

    argc is almost always at least 1 as argv[0] is the name of the program. Arguments passed start at argv[1], argv[2] etc. So if one argument is passed then argc would be 2 or 3 for 2 arguments etc. The number of arguments is one less than argc.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2019 (16.5.1)

  9. #9
    Join Date
    Oct 2015
    Posts
    18

    Re: Using "argv" to pass image files C++

    Quote Originally Posted by 2kaud View Post
    argc is almost always at least 1 as argv[0] is the name of the program. Arguments passed start at argv[1], argv[2] etc. So if one argument is passed then argc would be 2 or 3 for 2 arguments etc. The number of arguments is one less than argc.
    Thank you. Thank you for your guidance.
    My program is working now.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)