Thread: How to read images with argc & argv?

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    17

    How to read images with argc & argv?

    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 protected]
      WWW:      http://www.fmrib.ox.ac.uk/~steve
    
      (C) Crown Copyright (1995-1999), Defence Evaluation and Research Agency,
      Farnborough, Hampshire, GU14 6TD, UK
      DERA WWW site:
      http://www.dera.gov.uk/
      DERA Computer Vision and Electro Optics Group WWW site:
      http://www.dera.gov.uk/imageprocessi...roup_home.html
      DERA Computer Vision and Electro Optics Group point of contact:
      Dr. John Savage, [email protected], +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.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    How to read the filenames in a directory is listed in the FAQ.

    But have you considered that driving this from the shell would be easier than modifying the code?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Oct 2007
    Posts
    17
    Mind you to educate me more on this. I am using microsoft visual studio and a beginner for it.
    So, pls feel free to show me the way because i just cannot understand due to my standard.

    thank you.

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    There are various ways to do it, but you could use a batch file:

    This link contains a set of batch command references:
    http://labmice.techtarget.com/articles/batchcmds.htm

    something like this perhaps:
    Code:
    for %%f in (*.img) do process_image %%f %%~nf.out
    This will take all files called ".img" in the current directory, and produce ".out" file of the same base name. [It is probably not a good idea to call the outputfile the same extension as the input file, as that would cause the new files to be included in the processing later on, and end up producing more new files, etc, etc, etc,]

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    Oct 2007
    Posts
    17
    I am using microsoft visual studio and i think the link given is not suitable for this platform right?
    I have find and modified some codes on how to read more files as below:
    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 protected]
      WWW:      http://www.fmrib.ox.ac.uk/~steve
    
      (C) Crown Copyright (1995-1999), Defence Evaluation and Research Agency,
      Farnborough, Hampshire, GU14 6TD, UK
      DERA WWW site:
      http://www.dera.gov.uk/
      DERA Computer Vision and Electro Optics Group WWW site:
      http://www.dera.gov.uk/imageprocessing/dera/group_home.html
      DERA Computer Vision and Electro Optics Group point of contact:
      Dr. John Savage, [email protected], +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 */
    
    #include <stdlib.h>
    #include <stdio.h>
    #include <string.h>
    #include <math.h>
    #include <windows.h>
    
    #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];
    /***********************************************************************************************/
    
    /* {{{ get_image(filename,in,x_size,y_size) */
    
    /* {{{ int getint(fp) derived from XV */
    
    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 &#37;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);
    }
    
    /* }}} */
    
    int main(int argc, char **argv)
    {
        WIN32_FIND_DATA FindFileData;
        HANDLE hFind;
        DWORD dwError;
        int numberOfInputFiles = 0;
    	int p=0;
    	uchar *in, *bp;
    	int x_size,y_size;
    	CORNER_LIST corner_list;
    
        /* 
         * 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);
    			get_image(FindFileData.cFileName,&in,&x_size,&y_size);
    				
            
    
        } 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;
    }
    
    /************************************************************************************************/
    The problem is how can i write each processed file one by on automatically?

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    The method I suggest would work directly with your platform - Visual Studioruns on something compatible with Win2K [or, rather, it DOESN'T run on non-Windows systems] - and I doubt that you are running on a Windows NT 4.0 system, so Win2K or newer would work.

    As to your current question: You will have to find a way to modify the new of the file and output it back out.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > The problem is how can i write each processed file one by on automatically?
    1. Make argv[2] the destination directory
    2. Append each FindFileData.cFileName to that directory, and use that as the output filename.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  8. #8
    Registered User
    Join Date
    Oct 2007
    Posts
    17
    Ya i am trying on this. It's worked to read files which put in the same directory only. Can i use FindFileData.cFilename to read files from other directory? How?
    There is no problem for output files.
    Thanks for your suggestion

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > FindFileData.cFilename to read files from other directory? How?
    It's all in the first parameter to FindFirstFile
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  10. #10
    Registered User
    Join Date
    Oct 2007
    Posts
    17
    Yup, it's can't happen. This command is to read from current directory.
    I have modified this program to invoke another program to read continuously.
    Thanks for the advices.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. fscanf function won't read my float value...?
    By qpsnhalfs in forum C Programming
    Replies: 8
    Last Post: 07-07-2009, 11:01 PM
  2. bytes lost with partial read in UDP
    By mynickmynick in forum Networking/Device Communication
    Replies: 3
    Last Post: 03-31-2009, 02:06 AM
  3. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  4. more argv and argc
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 09-08-2001, 11:04 PM
  5. Help! Can't read decimal number
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 09-07-2001, 02:09 AM