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~~





9/17/2014

opencv, simple source code Video frames to jpeg files (VideoCapture, imwrite)

simple source code.

read avi file and save jpeg files.

set video file name and save directory property in your setting.

#include < opencv2\opencv.hpp>
#include < stdio.h>


#ifdef _DEBUG        
#pragma comment(lib, "opencv_core249d.lib")
#pragma comment(lib, "opencv_imgproc249d.lib")   
#pragma comment(lib, "opencv_highgui249d.lib")
#else
#pragma comment(lib, "opencv_core249.lib")
#pragma comment(lib, "opencv_imgproc249.lib")
#pragma comment(lib, "opencv_highgui249.lib")
#endif  

using namespace std;
using namespace cv;

void main()
{
 VideoCapture stream1("./bigBugs1.avi");  //file name

 if (!stream1.isOpened()) { //check if video file has been initialised   
  cout << "cannot open the file";   
 }   
 //window name
 namedWindow("Origin");   

 //string 
 char str[256];
 int frameCount=0;
 //unconditional loop   
 while (true) {   
  Mat Frame;   
  if( stream1.read(Frame) == 0) //get one frame form video   
   break;
  imshow("Origin", Frame);   

  sprintf_s(str,".\\frames1\\%d_frames.jpg", frameCount++);
  imwrite(str,Frame);
  
  

  if (waitKey(30) >= 0)   
   break;   
 }   

 destroyAllWindows();   


}

..


9/03/2014

Python mail read example, using imaplib

import imaplib
import email
import mimetypes
from email import header


def decodeHeader( headerMsg ):
    L = header.decode_header(headerMsg)
    s = ''
    for s1, chset in L:
        if(type(s1) == bytes):
            s += s1.decode(chset) if chset else s1.decode()
        else:
            s += s1
    return s



host = 'imap.xxx.comt'
userid = 'myaccount@xxx.com'
passwd = 'passward'

imap = imaplib.IMAP4_SSL(host)
imap.login(userid, passwd)

imap.select('specific_folder') #or use INBOX
status, email_ids = imap.search(None, '(ALL)') #or use , '(UNSEEN)' )


for num in email_ids[0].split():
    type1, data = imap.fetch(num, '(RFC822)')
    raw_email = data[0][1]
    email_msg = email.message_from_bytes( raw_email )

    print( 'Subject: ', decodeHeader( email_msg['Subject'] ) )
    print( 'From: ', decodeHeader( email_msg['From'] ) )
    print( 'To: ', decodeHeader( email_msg['To'] ) )
    print( 'Date: ', decodeHeader( email_msg['Date'] ) )


    type1, data = imap.fetch(num, '(UID BODY[TEXT])')
    raw_email = data[0][1]
    print('contents: ', raw_email )
    print('----\n\n')


9/01/2014

Python send mail example, smtplib


import smtplib
from email.mime.text import MIMEText
from email.header import Header

host = 'smtp.gmail.com:587'
me = 'me@gmail.com'
you = 'you@daum.net'
subject = 'I love ํŒŒ์ด์ฌ'
contents = 'It is contents'

msg = MIMEText(contents.encode('utf-8'), _subtype='plain', _charset='utf-8')
msg['Subject'] = Header(subject.encode('utf-8'), 'utf-8')
msg['From'] = me
msg['To'] = you

s = smtplib.SMTP(host)
s.starttls()
#print( s.ehlo() )
s.login('ID','PASS')
problems = s.sendmail(me, [you], msg.as_string() )
#print( problems )
s.quit()