OpenCV (Open Source Computer Vision Library) is a great open source computer vision and machine learning software library. OpenCV was built to provide a common infrastructure for computer vision applications and to accelerate the use of machine perception in the commercial products.
The versions of OpenCV library at biicode are:
- OpenCV 2.4.10 at diego/opencv.
- OpenCV 3.0 beta at diego/opencv (beta).
Both versions are available at biicode using the hooks feature generated from this github repo.
In this example we’ll show how to get started with OpenCV using two features: showing an image and detecting faces with the object detection module.
In this example we’ll show how to get started with OpenCV using two features: showing an image and detecting faces with the object detection module.
Create a project and place the source files and images inside:
$ bii init opencv_sample -L
$ cd opencv_sample
$ # copy all following files inside
main.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 #include "opencv/cv.h" // include it to used Main OpenCV functions. #include "opencv/highgui.h" //include it to use GUI functions. int main(int argc, char** argv) { IplImage* img = cvLoadImage( "examples/opencv_sample/bii.png" ); //NOTE "examples" should be your user cvNamedWindow( "Example1", CV_WINDOW_AUTOSIZE ); cvShowImage("Example1", img); cvMoveWindow("Example1", 0, 0); cvWaitKey(0); cvReleaseImage( &img ); cvDestroyWindow( "Example1" ); return 0; }Download file:
bii.png
.
mainface.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 #include "opencv2/objdetect/objdetect.hpp" #include "opencv2/highgui/highgui.hpp" #include "opencv2/imgproc/imgproc.hpp" #include <iostream> #include <stdio.h> using namespace std; using namespace cv; /** Function Headers */ void detectAndDisplay( Mat frame ); /** Global variables */ String face_cascade_name = "examples/opencv_sample/haarcascade_frontalface_alt.xml"; //NOTE "examples" should be your user String eyes_cascade_name = "examples/opencv_sample/haarcascade_eye_tree_eyeglasses.xml"; //NOTE "examples" should be your user CascadeClassifier face_cascade; CascadeClassifier eyes_cascade; string window_name = "Capture - Face detection"; RNG rng(12345); /** @function main */ int main( int argc, const char** argv ) { CvCapture* capture; //-- 1. Load the cascades if( !face_cascade.load( face_cascade_name ) ){ printf("--(!)Error loading\n"); return -1; }; if( !eyes_cascade.load( eyes_cascade_name ) ){ printf("--(!)Error loading\n"); return -1; }; // Read the image file Mat frame = imread("examples/opencv_sample/hugh.png"); //NOTE "examples" should be your user // Apply the classifier to the frame if (!frame.empty()) detectAndDisplay(frame); else{ printf(" --(!) No captured frame -- Break!"); } int c = waitKey(); return 0; } /** @function detectAndDisplay */ void detectAndDisplay( Mat frame ) { std::vector<Rect> faces; Mat frame_gray; cvtColor(frame, frame_gray, COLOR_BGR2GRAY); equalizeHist( frame_gray, frame_gray ); //-- Detect faces face_cascade.detectMultiScale(frame_gray, faces, 1.1, 2, 0 | CASCADE_SCALE_IMAGE, Size(30, 30)); for( size_t i = 0; i < faces.size(); i++ ) { Point center( faces[i].x + faces[i].width*0.5, faces[i].y + faces[i].height*0.5 ); ellipse( frame, center, Size( faces[i].width*0.5, faces[i].height*0.5), 0, 0, 360, Scalar( 255, 0, 255 ), 4, 8, 0 ); Mat faceROI = frame_gray( faces[i] ); std::vector<Rect> eyes; //-- In each face, detect eyes eyes_cascade.detectMultiScale(faceROI, eyes, 1.1, 2, 0 | CASCADE_SCALE_IMAGE, Size(30, 30)); for( size_t j = 0; j < eyes.size(); j++ ) { Point center( faces[i].x + eyes[j].x + eyes[j].width*0.5, faces[i].y + eyes[j].y + eyes[j].height*0.5 ); int radius = cvRound( (eyes[j].width + eyes[j].height)*0.25 ); circle( frame, center, radius, Scalar( 255, 0, 0 ), 4, 8, 0 ); } } //-- Show what you got imshow( window_name, frame ); }Download files:
hugh.png
,haarcascade_eye_tree_eyeglasses.xml
,haarcascade_frontalface_alt.xml
.
Check the dependencies of the project with bii deps:
$ bii deps
INFO: Processing changes...
examples/mycvproject depends on:
system:
iostream
stdio.h
unresolved:
opencv/cv.h
opencv/highgui.h
opencv2/highgui/highgui.hpp
opencv2/imgproc/imgproc.hpp
opencv2/objdetect/objdetect.hpp
Edit biicode.conf file generated in the project folder. Add your [requirements]
depending on the version you want and map your [includes]
and your [data]
:
[requirements]
diego/opencv: 2
[includes]
opencv/*: diego/opencv
opencv2/*: diego/opencv
[data]
main.cpp + bii.png
mainface.cpp + haarcascade_frontalface_alt.xml haarcascade_eye_tree_eyeglasses.xml hugh.png
Now, checking bii deps, your dependencies are resolved.
Now execute bii build to build the project.
Windows users need Visual Studio. Execute:
$ bii configure -G "Visual Studio 12"
$ bii build
Go to bin directory and execute the binaries:
$ cd bin
$./youruser_opencv_sample_main
$./youruser_opencv_sample_mainface
This example is already in biicode at examples/opencv_sample, so you can give it a try.
Create a new project and open the block:
$ bii init mycvproject
$ cd mycvproject
$ bii open examples/opencv_sample
Just build and run both examples:
$ bii build
$./youruser_opencv_sample_main
$./youruser_opencv_sample_mainface
Got any doubts? Do not hesitate to contact us visit our forum and feel free to ask any questions.