Showing posts with label detect. Show all posts
Showing posts with label detect. Show all posts

Friday, March 15, 2013

It's any one there ?

Well there are a couple of possibilities to check if there are any object, person

But one of the simplest is to use Haar-like features.

To work with Haar features (see the example FaceDetect.cpp in your directory OpenCV-2.4.3/samples/c/facedetect.cpp)
But basically works with haar files which store an abstract of the information about what is a face (any object) and what is not a face (a object)

Depending of the necessity and the power of our machine, we should different haar libraries. Even we can build special libraries for our purposes. (but my i3 with 4 GB ram took 3 days to make one haar library about cars)

I used this three
  

"./haarcascades/haarcascade_frontalface_alt_tree.xml" (3.5 MB) (over 500ms)
"./haarcascades/haarcascade_frontalface_alt2.xml" (0.8 MB) (over 300 ms)
"./haarcascades/haarcascade_eye.xml" (0.4 MB) (over 200 ms)


Also depends the size of the image, I used the less quality (160x120) for the first ideas will works

The code is based in the sample, so you'll find the code there, any doubt ask me.



With this exercise I finish the first part of the project based on computer vision.
We can do a lot of things more, blobs, our haar libraries, structural analyzes, movement studies, etc... but all this exercise are outside of this scope.

Saturday, February 23, 2013

Contours in openCV

Detecting contours.


The process of detecting contours is one of the simples.

Load the image -> Change to gray --> use the threshold function --> find the contour --> draw the contours

The code

//Load the image in colour
        flag=(double) getTickCount();
        imagen=imread(argv[1],CV_LOAD_IMAGE_UNCHANGED);
        flagLoad=getTick(flag);
//Change the image to gray
        flag=(double)getTickCount();
        cvtColor(imagen,imgGris,CV_BGR2GRAY);
        flag2Gray=getTick(flag);
//use the threshold to separate few objects
        flag=(double)getTickCount();
        threshold(imgGris,imgContorno,122,255,THRESH_BINARY);
        flagThreshold=getTick(flag);
//we need a place to leave the new iamge
        flag=(double)getTickCount();
        Mat dst = Mat::zeros(imgGris.rows,imgGris.cols,CV_8UC3);
        flagCreate=getTick(flag);

        namedWindow("gris",CV_WINDOW_NORMAL);
        namedWindow("contorno",CV_WINDOW_NORMAL);
        namedWindow("binario",CV_WINDOW_NORMAL);

        imshow ("gris",imgGris);
        imshow ("contorno",imgContorno);
        cvMoveWindow("gris",300,50);
        cvMoveWindow("contorno",600,50);

vector< vector<Point> > vecContornos;
vector<Vec4i>jerarquia;
//Find the contours
        flag = (double)getTickCount();
        findContours(imgContorno,vecContornos,jerarquia,CV_RETR_CCOMP,CV_CHAIN_APPROX_SIMPLE);
        flagFind =getTick(flag);
        flag = (double)getTickCount();
        for (int idx=0;idx >=0;idx=jerarquia[idx][0])
        {
                drawContours(dst,vecContornos,idx,WHITE,5,8,jerarquia);
        }

The results 

And finally we have this results from the original image


Original image


Result image
 All the values are in milliseconds 

This exercise give us a useful information

The main information is the threshold to separate the right image (12 ms) and find the contours (31 ms) around 50 ms to detect objects, it's not really bad for a dive which could use this information.

But the structural analysis could be take more time, we will what happen in the next exercises 

Any comment will be appreciated