GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 18-Oct-2007, 03:29
TommyC TommyC is offline
Junior Member
 
Join Date: Jul 2007
Location: Malaysia
Posts: 73
TommyC is on a distinguished road

How to read images with argc & argv?


Hi all,

i have using argc and argv to read an image as below:
CPP / C++ / C Code:
int main(argc,argv)
int argc
;
char *argv[];
{
:
:
:
	for(total = 0; total<NUM; total ++){
		read_data_grey2(argv[1],gry2); /*argv[1]->filename gry2-                                                                     >array for output data*/
                                              :
                                              :
             }
}
This is the coding i use to read one image.
How can i modify these codes in order to read more images?
Any idea will be appreciated. Thanks.
  #2  
Old 18-Oct-2007, 07:40
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 6,153
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: How to read images with argc & argv?


Quote:
Originally Posted by TommyC
i have using argc and argv to read an image as below:

CPP / C++ / C Code:
/* showargs.c: prints argc and all argv values */
#include <stdio.h>
int main(int argc, char **argv)
{
    int i;
    printf("Argc = %d\n", argc);
    for (i = 0; i < argc; i++) {
        /* I will just print them out. You can use the
         * argv[i] as a file name for fopen,  or strcpy 
         * an argv[i] to a char array and do stuff.
         *
         * Or whatever...
         */
        printf("argv[%d] = <%s>\n", i, argv[i]);
    }
    return 0;
}


Output from a couple of runs:
Code:
F:\temp >showargs Argc = 1 argv[0] = <showargs> F:\temp >showargs abc.bmp Argc = 2 argv[0] = <showargs> argv[1] = <abc.bmp> F:temp>showargs this is a test Argc = 5 argv[0] = <showargs> argv[1] = <this> argv[2] = <is> argv[3] = <a> argv[4] = <test>

(Note that for some compilers, argv[0] will show the complete path, not just the program name itself.)

Regards,

Dave
  #3  
Old 18-Oct-2007, 08:01
TommyC TommyC is offline
Junior Member
 
Join Date: Jul 2007
Location: Malaysia
Posts: 73
TommyC is on a distinguished road

Re: How to read images with argc & argv?


I have tried similar coding as shown. If i have 100 image files then i have to type all before executing the program? Is there any alternatives to run all the files by a short name?
  #4  
Old 18-Oct-2007, 08:21
seprich seprich is offline
Member
 
Join Date: Jun 2007
Posts: 110
seprich has a spectacular aura aboutseprich has a spectacular aura about

Re: How to read images with argc & argv?


Quote:
Originally Posted by TommyC
I have tried similar coding as shown. If i have 100 image files then i have to type all before executing the program? Is there any alternatives to run all the files by a short name?
If the name of your program is "my_processing" then writing:
Code:
my_processing *
will take care of all of the 100+ images.
The shell in your console will make the conversion from "my_processing *" into "my_processing image1.png image2.png ..." (just example of course).
So your application gets the complete list as arguments.
Was this what you were asking?
  #5  
Old 18-Oct-2007, 08:35
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 6,153
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: How to read images with argc & argv?


Quote:
Originally Posted by TommyC
I have tried similar coding as shown. If i have 100 image files then i have to type all before executing the program? Is there any alternatives to run all the files by a short name?

There are no standard C or C++ library functions that deal with "wild cards" in file names.

In UNIX or Linux the command line shell (sh, bash, csh, tcsh, whatever...) will expand wild cards for you. So if your Linux directory contains, say xx.bmp and yy.bmp, then the program that I showed will give you something like the following output:

Code:
$ ./showargs *.bmp Argc = 3 argv[0] = <showargs> argv[1] = <xx.bmp> argv[2] = <yy.bmp>

In other words, the user program is presented with a command line for which all files with the names anything_at_all.bmp have been separated into an argument list of actual file names.

This also works on my Windows XP platform with cygwin when I compile with gcc. (But not with Borland and Microsoft compilers.)

This behavior is not part of the C language standard specification; it is operating system and compiler implementation dependent.

For other compilers and operating systems, there may be functions that let you find all files in a directory having a pattern that includes wild cards. In other words you can make some kind of loop that finds the files one at a time.

There may be other ways that you could consider: Like getting a directory listing of the desired file names in a file before you invoke the program that is going to use the files, then having that program read the names from your directory file. Stuff like that.



Regards,

Dave
  #6  
Old 18-Oct-2007, 09:25
TommyC TommyC is offline
Junior Member
 
Join Date: Jul 2007
Location: Malaysia
Posts: 73
TommyC is on a distinguished road

Re: How to read images with argc & argv?


Seprich: I am using microsoft platform but i compile my program via command prompt. Do u mind to elaborate more on how to do it? I cannot fully understand your explanation through my standard in C programming. How can i do it if let say my program named: susan21 n the images named as image1.pgm image2.pgm........

Dave:Actually i have tried another method exactly the way as u mentioned in your last two lines. I want to try whether there is any alternatives to do these by using argv,argc commands. I am using MS Visual Studio and compile in command prompt. So any ideas?
Anyway thanks for the guidances, guys.
  #7  
Old 18-Oct-2007, 10:13
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 6,153
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: How to read images with argc & argv?


Quote:
Originally Posted by TommyC
I am using MS Visual Studio

The Windows API functions FindFirstFile and FindNextFile can be used in console programs.

http://msdn2.microsoft.com/en-us/library/aa364418.aspx
http://msdn2.microsoft.com/en-us/library/aa364428.aspx

Here's an example
CPP / C++ / C Code:
/*
 * show_wild_cards.c
 *
 * Derived from the directory listing program on msdn.
 * This expands a single argument with wild card(s) into
 * individual file names.
 *
 * Tested with Visual C++ and Borland bcc32 on Windows XP
 *
 * Non-standard library functions and data declarations may
 * be different for different compilers and even for different
 * versions of the same compiler.
 *
 * In other words: IWFM---YMMV
 * (It Works For Me---Your Mileage May Vary)
 *
 * davekw7x
 */

#include <windows.h>
#include <stdio.h>
#include <stdlib.h>


int main(int argc, char **argv)
{
    WIN32_FIND_DATA FindFileData;
    HANDLE hFind;
    DWORD dwError;

    char path[BUFSIZ] = ".\\";  /* BUFSIZ is #defined in <stdio.h> */
    printf("argc = %d\n", argc);
    if (argc < 2) {
        printf("Usage: %s filename_maybe_with_wild_cards\n", argv[0]);
        exit(EXIT_FAILURE);
    }
    printf("File specification is <%s>\n", argv[1]);
    if (strlen(argv[1]) > BUFSIZ - 3) {
        printf("File name is too long.\n");
        exit(EXIT_FAILURE);
    }
    strcat(path, argv[1]);

    /* Find the first file in the directory. */
    hFind = FindFirstFile(path, &FindFileData);

    if (hFind == INVALID_HANDLE_VALUE) {
        printf("No files found. Error is %lu\n", GetLastError());
        exit(EXIT_FAILURE);
    }
    else {
        printf("First file name is: %s\n", FindFileData.cFileName);

        /* List all the other files in the directory. */
        while (FindNextFile(hFind, &FindFileData) != 0) {
            printf("Next  file name is: %s\n", FindFileData.cFileName);
        }

        dwError = GetLastError();
        FindClose(hFind);

        if (dwError != ERROR_NO_MORE_FILES) {
            printf("FindNextFile error. Error is %lu\n", dwError);
            exit(EXIT_FAILURE);
        }
    }
    return EXIT_SUCCESS;
}


Output from program compiled with various versions of Visual C++ and Borland bcc32 for a directory containing xx.bmp and yy.bmp:
Code:
F:\temp>show_wild_cards *.bmp argc = 2 File specification is <*.bmp> First file name is: xx.bmp Next file name is: yy.bmp

Regards,

Dave
Last edited by davekw7x : 18-Oct-2007 at 11:09.
  #8  
Old 29-Oct-2007, 08:16
TommyC TommyC is offline
Junior Member
 
Join Date: Jul 2007
Location: Malaysia
Posts: 73
TommyC is on a distinguished road

Re: How to read images with argc & argv?


Sorry for late reply due to outstation.
Davekw7x: Maybe i am not up to the standard to understand your coding given.
However, this is the original piece of coding.
CPP / C++ / C Code:
/* {{{ Copyright etc. */

/**********************************************************************\

  SUSAN Version 2l by Stephen Smith
  Oxford Centre for Functional Magnetic Resonance Imaging of the Brain,
  Department of Clinical Neurology, Oxford University, Oxford, UK
  (Previously in Computer Vision and Image Processing Group - now
  Computer Vision and Electro Optics Group - DERA Chertsey, UK)
  Email:    [email]steve@fmrib.ox.ac.uk[/email]
  WWW:      [url]http://www.fmrib.ox.ac.uk/~steve[/url]

  (C) Crown Copyright (1995-1999), Defence Evaluation and Research Agency,
  Farnborough, Hampshire, GU14 6TD, UK
  DERA WWW site:
  [url]http://www.dera.gov.uk/[/url]
  DERA Computer Vision and Electro Optics Group WWW site:
  [url]http://www.dera.gov.uk/imageprocessing/dera/group_home.html[/url]
  DERA Computer Vision and Electro Optics Group point of contact:
  Dr. John Savage, [email]jtsavage@dera.gov.uk[/email], +44 1344 633203

  A UK patent has been granted: "Method for digitally processing
  images to determine the position of edges and/or corners therein for
  guidance of unmanned vehicle", UK Patent 2272285. Proprietor:
  Secretary of State for Defence, UK. 15 January 1997

  This code is issued for research purposes only and remains the
  property of the UK Secretary of State for Defence. This code must
  not be passed on without this header information being kept
  intact. This code must not be sold.

\**********************************************************************/

/* }}} */
/* {{{ defines, includes and typedefs */

/* ********** Optional settings */

#ifndef PPC
typedef int        TOTAL_TYPE; /* this is faster for "int" but should be "float" for large d masks */
#else
typedef float      TOTAL_TYPE; /* for my PowerPC accelerator only */
#endif

/*#define FOPENB*/           /* uncomment if using djgpp gnu C for DOS or certain Win95 compilers */
#define SEVEN_SUPP           /* size for non-max corner suppression; SEVEN_SUPP or FIVE_SUPP */
#define MAX_CORNERS   15000  /* max corners per frame */

/* ********** Leave the rest - but you may need to remove one or both of sys/file.h and malloc.h lines */

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
//#include <sys/file.h>    /* may want to remove this line */
//#include <malloc.h>      /* may want to remove this line */
#define  exit_error(IFB,IFC) { fprintf(stderr,IFB,IFC); exit(0); }
#define  FTOI(a) ( (a) < 0 ? ((int)(a-0.5)) : ((int)(a+0.5)) )
typedef  unsigned char uchar;
typedef  struct {int x,y,info, dx, dy, I;} CORNER_LIST[MAX_CORNERS];

int getint(fd)
  FILE *fd;
{
  int c, i;
  char dummy[10000];

  c = getc(fd);
  while (1) /* find next integer */
  {
    if (c=='#')    /* if we're at a comment, read to end of line */
      fgets(dummy,9000,fd);
    if (c==EOF)
      exit_error("Image %s not binary PGM.\n","is");
    if (c>='0' && c<='9')
      break;   /* found what we were looking for */
    c = getc(fd);
  }

  /* we're at the start of a number, continue until we hit a non-number */
  i = 0;
  while (1) {
    i = (i*10) + (c - '0');
    c = getc(fd);
    if (c==EOF) return (i);
    if (c<'0' || c>'9') break;
  }

  return (i);
}

/* }}} */

void get_image(filename,in,x_size,y_size)
  char           filename[200];
  unsigned char  **in;
  int            *x_size, *y_size;
{
FILE  *fd;
char header [100];
int  tmp;

#ifdef FOPENB
  if ((fd=fopen(filename,"rb")) == NULL)
#else
  if ((fd=fopen(filename,"r")) == NULL)
#endif
    exit_error("Can't input image %s.\n",filename);

  /* {{{ read header */

  header[0]=fgetc(fd);
  header[1]=fgetc(fd);
  if(!(header[0]=='P' && header[1]=='5'))
    exit_error("Image %s does not have binary PGM header.\n",filename);

  *x_size = getint(fd);
  *y_size = getint(fd);
  tmp = getint(fd);

/* }}} */

  *in = (uchar *) malloc(*x_size * *y_size);

  if (fread(*in,1,*x_size * *y_size,fd) == 0)
    exit_error("Image %s is wrong size.\n",filename);

  fclose(fd);
}

/* }}} */
/* {{{ put_image(filename,in,x_size,y_size) */

put_image(filename,in,x_size,y_size)
  char filename [100],
       *in;
  int  x_size,
       y_size;
{
FILE  *fd;

#ifdef FOPENB
  if ((fd=fopen(filename,"wb")) == NULL) 
#else
  if ((fd=fopen(filename,"w")) == NULL) 
#endif
    exit_error("Can't output image%s.\n",filename);

  fprintf(fd,"P5\n");
  fprintf(fd,"%d %d\n",x_size,y_size);
  fprintf(fd,"255\n");
  
  if (fwrite(in,x_size*y_size,1,fd) != 1)
    exit_error("Can't write image %s.\n",filename);

  fclose(fd);
}

main(argc, argv)
  int   argc;
  char  *argv [];
{
/* {{{ vars */

FILE   *ofp;
char   filename [80],
       *tcp;
uchar  *in, *bp, *mid;
float  dt=4.0;
int    *r,
       argindex=3,
       bt=13,
       principle=0,
       thin_post_proc=1,
       three_by_three=0,
       drawing_mode=0,
       susan_quick=0,
       max_no_corners=1850,
       max_no_edges=2650,
       mode = 0, i,
       x_size, y_size;
CORNER_LIST corner_list;

   get_image(argv[1],&in,&x_size,&y_size);

        susan_corners(in,r,bp,max_no_corners,corner_list,x_size,y_size);
        corner_draw(in,corner_list,x_size,drawing_mode);
      }

      break;

/* }}} */
  }    

/* }}} */

  put_image(argv[2],in,x_size,y_size);
}

/* }}} */

Sorry i just cannot include the full program in because it might be too complicated or too long. The main modification will be on get_image and put_image subroutines.

So, how can i modified it to read the image files(>50) from a directory and write the processed images into another directory?

Thank you.
  #9  
Old 29-Oct-2007, 13:15
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 6,153
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: How to read images with argc & argv?


Quote:
Originally Posted by TommyC
So, how can i modified it to read the image files(>50) from a directory and write the processed images into another directory...
CPP / C++ / C Code:
/*
 * show how to use wild cards with Windows API
 * functions FindFirstFile() and FindNextFile()
 *
 * Derived from the directory listing program on msdn.
 * This expands a single argument with wild card(s) into
 * individual file names.
 *
 * Tested with Visual C++ and Borland bcc32 on Windows XP
 *
 * Non-standard library functions and data declarations may
 * be different for different compilers and even for different
 * versions of the same compiler.
 *
 * In other words: IWFM---YMMV
 * (It Works For Me---Your Mileage May Vary)
 *
 * davekw7x
 */

#include <windows.h>
#include <stdio.h>
#include <stdlib.h>


int main(int argc, char **argv)
{
    WIN32_FIND_DATA FindFileData;
    HANDLE hFind;
    DWORD dwError;
    int numberOfInputFiles = 0;

    /* 
     * If path for files is current directory use the following
     * otherwise initialize with whatever you need
     */
    char path[BUFSIZ] = ".\\";  /* BUFSIZ is #defined in <stdio.h> */

    if (argc < 2) {
        printf("Usage: %s filename_maybe_with_wild_cards\n", argv[0]);
        exit(EXIT_FAILURE);
    }

    printf("File specification is <%s>\n\n", argv[1]);

    if (strlen(argv[1]) > BUFSIZ - 3) {
        printf("File name is too long.\n");
        exit(EXIT_FAILURE);
    }
    strcat(path, argv[1]); /* this is the input file specification */

    /* Find the first file in the directory. */
    hFind = FindFirstFile(path, &FindFileData);

    if (hFind == INVALID_HANDLE_VALUE) {
        printf("No files found.\n");
        exit(EXIT_FAILURE);
    }

    else do {

        printf("Processing file number %d: %s\n", 
                ++numberOfInputFiles, FindFileData.cFileName);

        /* 
         * Do whatever you do with a file :
         * Call get_image. Perform transformations, etc.
         *
         * How do you create the output file name?
         * Well, do it and then call put_image
         *
         */

    } while (FindNextFile(hFind, &FindFileData) != 0);
    dwError = GetLastError();
    FindClose(hFind);
    printf("\nTotal files processed = %d\n", numberOfInputFiles);

    if (dwError != ERROR_NO_MORE_FILES) {
        printf("FindNextFile error. Error is %lu\n", dwError);
        exit(EXIT_FAILURE);
    }
    return EXIT_SUCCESS;
}

Here's a run:

Code:
F:\temp>cl process_wild_cards.c Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 15.00.20706.01 for 80x86 Copyright (C) Microsoft Corporation. All rights reserved. process_wild_cards.c Microsoft (R) Incremental Linker Version 9.00.20706.01 Copyright (C) Microsoft Corporation. All rights reserved. /out:process_wild_cards.exe process_wild_cards.obj F:\temp>dir Volume in drive F is Disk1a_40Gig Volume Serial Number is 44FD-261A Directory of F:\temp 10/29/2007 12:17 PM <DIR> . 10/29/2007 12:17 PM <DIR> .. 10/29/2007 12:17 PM 1,186 homers_folly.txt 10/29/2007 12:10 PM 2,344 process_wild_cards.c 10/29/2007 12:18 PM 49,152 process_wild_cards.exe 10/29/2007 12:18 PM 1,987 process_wild_cards.obj 10/29/2007 11:40 AM 90 test1.txt 10/29/2007 11:50 AM 150 test99.txt 6 File(s) 54,909 bytes 2 Dir(s) 1,174,876,160 bytes free F:\temp>process_wild_cards *.txt File specification is <*.txt> Processing file number 1: homers_folly.txt Processing file number 2: test1.txt Processing file number 3: test99.txt Total files processed = 3 F:\temp>process_wild_cards test*.txt File specification is <test*.txt> Processing file number 1: test1.txt Processing file number 2: test99.txt Total files processed = 2

Regards,

Dave
  #10  
Old 30-Oct-2007, 00:40
TommyC TommyC is offline
Junior Member
 
Join Date: Jul 2007
Location: Malaysia
Posts: 73
TommyC is on a distinguished road

Re: How to read images with argc & argv?


Thanks for the program, it's great!
Now i am working on it to get what i want. I will come to u guys if i meet with some problems that i cannot solve.
Thank you very much.
 


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Airport Log program using 3D linked List : problem reading from file batrsau C Programming Language 11 29-Feb-2008 07:44
BMP image on matrix load nemo C++ Forum 21 17-Dec-2007 23:04
argc argv errors 1978929 C Programming Language 1 16-Jul-2007 08:41
String trim problem and wrong node routing agent! newbie06 Computer Software Forum - Linux 8 01-Mar-2007 23:51
php.ini file not being read ian111 Apache Web Server Forum 1 29-Jun-2005 11:35

Network Sites: GIDNetwork · GIDApp · GIDBlog · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 20:24.


vBulletin, Copyright © 2000 - 2014, Jelsoft Enterprises Ltd.