9/26/2014

opencv tip, Rect bounding

Sometimes, it causes memory error or unexpected result when we set lager rect size than image.

It is very annoying to always check.

But this tip is very simple and easy.

Plz see the code and result.


cv::Rect bounds(0,0,100,100);
cv::Rect roi(10,10,40,40);
Rect boundedRect = (roi & bounds);
cout << "x = " << boundedRect.x << endl;
cout << "y = " << boundedRect.y << endl;
cout << "width = " << boundedRect.width << endl;
cout << "height = " << boundedRect.height << endl;
 
cv::Rect roi2(-10,10,40,40);
Rect boundedRect2 = (roi2 & bounds);
cout << boundedRect << endl;

cv::Rect roi3(-10,-10,400,40);
cout << (roi3 & bounds) << endl;
...


9/24/2014

google urls of each countries

usa : http://www.google.com/webhp?hl=en
uk: http://www.google.co.uk/webhp?hl=en
australia  :  http://www.google.com.au/webhp?hl=en

france by french   http://www.google.fr/webhp?hl=fr
france by english   http://www.google.fr/webhp?hl=en

japen   http://www.google.co.jp/webhp?hl=ja
japen by english   http://www.google.co.jp/webhp?hl=en

OpenCV face detection using adaboost example source code and cpu vs gpu detection speed compare (CascadeClassifier, CascadeClassifier_GPU, detectMultiScale)

OpenCV has AdaBoost algorithm function.
And gpu version also is provided.

For using detection, we prepare the trained xml file.
Although we can train some target using adaboost algorithm in opencv functions, there are several trained xml files in the opencv folder. (mostly in opencv/sources/data/haarcascades )

I will use "haarcascade_frontalface_alt.xml" file for face detection example.

gpu and cpu both versions use xml file.

more detail refer to this source code.
The source code is included 2 version of cpu and gpu.

result is ..
gpu is faster than cpu version (but exactly they may not be same condition..)
blue boxes are result of cpu.
red boxes are results of gpu.
The results are not important because it can be different by parameters values.



<code start>

<code end>

Github
https://github.com/MareArts/AdaBoost-Face-Detection-test-using-OpenCV


#Tags
cvtColor, CascadeClassifier, CascadeClassifier_GPU, detectMultiScale,

9/23/2014

C/C++, option parameter / argument parser (using wingetopt.h, wingetopt.c)

This is option parameter parsing example.
When we excute cmd file with option, this paser is parsing each value of options.

ex) facedetection.exe -o B.avi -p 1000 -l A.avi

in source, parameters is parsred by "B.avi", "1000", ""(-l option exist), "A.avi"

see example source code easier understanding.


main.cpp
#include < iostream>
#include < string>
#include "wingetopt.h"

using namespace std;

struct Options 
{
 Options():Number(10),use_A(false),infile(),outfile()
 {}

 int Number;
 bool use_A;
 string infile;
 string outfile;
};

void parse_command_line(int argc, char** argv, Options& o)
{
 int c = -1;
 while( (c = getopt(argc, argv, "lo:p:")) != -1 )
 {
  switch(c)
  {
  case 'l':
   o.use_A = true;
   break;
  case 'o':
   o.outfile = optarg;
   break;
  case 'p':
   o.Number = atoi(optarg);
   break;
  default:
   cout << "error message" << endl;
   exit(1);
  }
 }

 if( optind < argc )
 {
  o.infile = argv[optind];
 }

 cout << "Num : " << o.Number << endl;
 cout << "Input file: " << o.infile << endl;
 cout << "Output file: " << o.outfile << endl;
 cout << "Use A: " << o.use_A << endl;
}

int main(int argc, char** argv)
{
 Options o;
 parse_command_line(argc, argv, o);
 


}
... wingetopt.h
/*
POSIX getopt for Windows

AT&T Public License

Code given out at the 1985 UNIFORUM conference in Dallas.  
*/

#ifdef __GNUC__
#include 
#endif
#ifndef __GNUC__

#ifndef _WINGETOPT_H_
#define _WINGETOPT_H_

#ifdef __cplusplus
extern "C" {
#endif

extern int opterr;
extern int optind;
extern int optopt;
extern char *optarg;
extern int getopt(int argc, char **argv, char *opts);

#ifdef __cplusplus
}
#endif

#endif  /* _GETOPT_H_ */
#endif  /* __GNUC__ */
... wingetopt.c
/*
POSIX getopt for Windows

AT&T Public License

Code given out at the 1985 UNIFORUM conference in Dallas.  
*/

#ifndef __GNUC__

#include "wingetopt.h"
#include < stdio.h>

#define NULL 0
#define EOF (-1)
#define ERR(s, c) if(opterr){\
 char errbuf[2];\
 errbuf[0] = c; errbuf[1] = '\n';\
 fputs(argv[0], stderr);\
 fputs(s, stderr);\
 fputc(c, stderr);}
 //(void) write(2, argv[0], (unsigned)strlen(argv[0]));\
 //(void) write(2, s, (unsigned)strlen(s));\
 //(void) write(2, errbuf, 2);}

int opterr = 1;
int optind = 1;
int optopt;
char *optarg;

int
getopt(argc, argv, opts)
int argc;
char **argv, *opts;
{
 static int sp = 1;
 register int c;
 register char *cp;

 if(sp == 1)
  if(optind >= argc ||
     argv[optind][0] != '-' || argv[optind][1] == '\0')
   return(EOF);
  else if(strcmp(argv[optind], "--") == NULL) {
   optind++;
   return(EOF);
  }
 optopt = c = argv[optind][sp];
 if(c == ':' || (cp=strchr(opts, c)) == NULL) {
  ERR(": illegal option -- ", c);
  if(argv[optind][++sp] == '\0') {
   optind++;
   sp = 1;
  }
  return('?');
 }
 if(*++cp == ':') {
  if(argv[optind][sp+1] != '\0')
   optarg = &argv[optind++][sp+1];
  else if(++optind >= argc) {
   ERR(": option requires an argument -- ", c);
   sp = 1;
   return('?');
  } else
   optarg = argv[optind++];
  sp = 1;
 } else {
  if(argv[optind][++sp] == '\0') {
   sp = 1;
   optind++;
  }
  optarg = NULL;
 }
 return(c);
}

#endif  /* __GNUC__ */

...




one more tip,
If you want to input argument values in the VS tools, you can input in here, -> property -> debug -> command argument
refer to this image~~