title: Object Marking in Haar training
body: <p>I am doing a project on object detection in OpenCV using haar training. I have to mark, in an image, all the places where the object is present. So, I need a code which opens each image so that I can mark the regions.
  Similar to the one here:</p>
  
  <pre><code>#include &lt;opencv/cv.h&gt;
  #include &lt;opencv/cvaux.h&gt;
  #include &lt;opencv/highgui.h&gt;
  
  // for filelisting
  #include &lt;stdio.h&gt;
  #include &lt;sys/io.h&gt;
  // for fileoutput
  #include &lt;string&gt;
  #include &lt;fstream&gt;
  #include &lt;sstream&gt;
  #include &lt;dirent.h&gt;
  #include &lt;sys/types.h&gt;
  
  using namespace std;
  
  IplImage* image=0;
  IplImage* image2=0;
  //int start_roi=0;
  int roi_x0=0;
  int roi_y0=0;
  int roi_x1=0;
  int roi_y1=0;
  int numOfRec=0;
  int startDraw = 0;
  char* window_name="&lt;SPACE&gt;add &lt;B&gt;save and load next &lt;ESC&gt;exit";
  
  string IntToString(int num)
  {
      ostringstream myStream; //creates an ostringstream object
      myStream &lt;&lt; num &lt;&lt; flush;
      /*
      * outputs the number into the string stream and then flushes
      * the buffer (makes sure the output is put into the stream)
      */
      return(myStream.str()); //returns the string form of the stringstream object
  };
  
  void on_mouse(int event,int x,int y,int flag, void *param)
  {
      if(event==CV_EVENT_LBUTTONDOWN)
      {
          if(!startDraw)
          {
              roi_x0=x;
              roi_y0=y;
              startDraw = 1;
          } else {
              roi_x1=x;
              roi_y1=y;
              startDraw = 0;
          }
      }
      if(event==CV_EVENT_MOUSEMOVE &amp;&amp; startDraw)
      {
  
          //redraw ROI selection
          image2=cvCloneImage(image);
          cvRectangle(image2,cvPoint(roi_x0,roi_y0),cvPoint(x,y),CV_RGB(255,0,255),1);
          cvShowImage(window_name,image2);
          cvReleaseImage(&amp;image2);
      }
  
  }
  
  int main(int argc, char** argv)
  {
      char iKey=0;
      string strPrefix;
      string strPostfix;
      string input_directory;
      string output_file;
  
      if(argc != 3) {
          fprintf(stderr, "%s output_info.txt raw/data/directory/\n", argv[0]);
          return -1;
      } 
  
      input_directory = argv[2];
      output_file = argv[1];
  
      /* Get a file listing of all files with in the input directory */
      DIR    *dir_p = opendir (input_directory.c_str());
      struct dirent *dir_entry_p;
  
      if(dir_p == NULL) {
          fprintf(stderr, "Failed to open directory %s\n", input_directory.c_str());
          return -1;
      }
  
      fprintf(stderr, "Object Marker: Input Directory: %s  Output File: %s\n", input_directory.c_str(), output_file.c_str());
  
      //    init highgui
      cvAddSearchPath(input_directory);
      cvNamedWindow(window_name,1);
      cvSetMouseCallback(window_name,on_mouse, NULL);
  
      fprintf(stderr, "Opening directory...");
      //    init output of rectangles to the info file
      ofstream output(output_file.c_str());
      fprintf(stderr, "done.\n");
  
      while((dir_entry_p = readdir(dir_p)) != NULL)
      {
          numOfRec=0;
  
          if(strcmp(dir_entry_p-&gt;d_name, ""))
          fprintf(stderr, "Examining file %s\n", dir_entry_p-&gt;d_name);
  
          /* TODO: Assign postfix/prefix info */
          strPostfix="";
          //strPrefix=input_directory;
          strPrefix=dir_entry_p-&gt;d_name;
          //strPrefix+=bmp_file.name;
          fprintf(stderr, "Loading image %s\n", strPrefix.c_str());
  
          if((image=cvLoadImage(strPrefix.c_str(),1)) != 0)
          {
  
              //    work on current image
              do
  
      {
                  cvShowImage(window_name,image);
  
                  // used cvWaitKey returns:
                  //    &lt;B&gt;=66        save added rectangles and show next image
                  //    &lt;ESC&gt;=27        exit program
                  //    &lt;Space&gt;=32        add rectangle to current image
                  //  any other key clears rectangle drawing only
                  iKey=cvWaitKey(0);
                  switch(iKey)
                  {
  
                  case 27:
  
                          cvReleaseImage(&amp;image);
                          cvDestroyWindow(window_name);
                          return 0;
                  case 32:
  
                          numOfRec++;
                  printf("   %d. rect x=%d\ty=%d\tx2h=%d\ty2=%d\n",numOfRec,roi_x0,roi_y0,roi_x1,roi_y1);
                  //printf("   %d. rect x=%d\ty=%d\twidth=%d\theight=%d\n",numOfRec,roi_x1,roi_y1,roi_x0-roi_x1,roi_y0-roi_y1);
                          // currently two draw directions possible:
                          //        from top left to bottom right or vice versa
                          if(roi_x0&lt;roi_x1 &amp;&amp; roi_y0&lt;roi_y1)
                          {
  
                              printf("   %d. rect x=%d\ty=%d\twidth=%d\theight=%d\n",numOfRec,roi_x0,roi_y0,roi_x1-roi_x0,roi_y1-roi_y0);
                              // append rectangle coord to previous line content
                              strPostfix+=" "+IntToString(roi_x0)+" "+IntToString(roi_y0)+" "+IntToString(roi_x1-roi_x0)+" "+IntToString(roi_y1-roi_y0);
  
                          }
                          else
                                                      //(roi_x0&gt;roi_x1 &amp;&amp; roi_y0&gt;roi_y1)
                          {
                              printf(" hello line no 154\n");
                              printf("   %d. rect x=%d\ty=%d\twidth=%d\theight=%d\n",numOfRec,roi_x1,roi_y1,roi_x0-roi_x1,roi_y0-roi_y1);
                              // append rectangle coord to previous line content
                              strPostfix+=" "+IntToString(roi_x1)+" "+IntToString(roi_y1)+" "+IntToString(roi_x0-roi_x1)+" "+IntToString      (roi_y0-roi_y1);
          }
  
                          break;
                  }
              }
              while(iKey!=66);
  
              {
              // save to info file as later used for HaarTraining:
              //    &lt;rel_path&gt;\bmp_file.name numOfRec x0 y0 width0 height0 x1 y1 width1 height1...
              if(numOfRec&gt;0 &amp;&amp; iKey==66)
              {
                  //append line
                  /* TODO: Store output information. */
                  output &lt;&lt; strPrefix &lt;&lt; " "&lt;&lt; numOfRec &lt;&lt; strPostfix &lt;&lt;"\n";
  
              cvReleaseImage(&amp;image);
              }
  
           else 
          {
              fprintf(stderr, "Failed to load image, %s\n", strPrefix.c_str());
          }
      }
  
      }}
  
      output.close();
      cvDestroyWindow(window_name);
      closedir(dir_p);
  
      return 0;
  }
  </code></pre>
  
  <p>I ran the above code using Visual Studio 10.
  When I run the above code it is <strong>opening the command window loading all the images (a new named window is also getting opened but getting but it is not staying for me to select the region)</strong> and then closing.</p>
  
  <p>When I ran it using <strong>start without debugging</strong> option in VS10 it is I could see this:</p>
  
  <pre><code>Examining file img (6).jpeg
  Loading image img (6).jpeg
  Examining file img (6).JPG
  Loading image img (6).JPG
  Examining file img (7).jpeg
  Loading image img (7).jpeg
  Examining file img (7).jpg
  Loading image img (7).jpg
  Examining file img (8).jpeg
  Loading image img (8).jpeg
  Examining file img (8).jpg
  Loading image img (8).jpg
  Examining file img (9).jpeg
  Loading image img (9).jpeg
  Examining file img (9).jpg
  Loading image img (9).jpg
  Examining file img 1.jpeg
  Loading image img 1.jpeg
  </code></pre>
  
  <p>the object marker fails and cannot load the image</p>
  
  <p>I think that it is skipping the:</p>
  
  <pre><code>if((image=cvLoadImage(strPrefix.c_str(),1)) != 0){.....}
  </code></pre>
  
  <p>part of the code</p>