Affichage des articles dont le libellé est OpenCV. Afficher tous les articles
Affichage des articles dont le libellé est OpenCV. Afficher tous les articles

mercredi 17 juillet 2013

Extract Circular/Elliptical ROI using OpenCV

It is well known that OpenCV manage rectangular region of interest (ROI) and not Circular or elliptical regions. So as to crop a circular or elliptical region of interest, there is a simple trick by applying a (circular/elliptical) binary mask on the image.
Here i'm going to show an example of using mask to extract elliptical/circular region for face detection task.
Actually this help to get rid of hair, background, neck, and all the noisy things which can appear jointly with the face.


void main()
{
   //----->Load image
     IplImage* Src = cvLoadImage("C:/ Test.jpg");

  //----->Face detection function
   CvRect* FaceD;
   FaceDetection(Src, FaceD); //This function is already defined, it returns the position of the face 

  //----->Extract the elliptical region of the face
        //Crop the rectangular region of the face   + convert it to grayscale image   
        CvRect ROIRect = cvRect(FaceD->x, FaceD->y, FaceD->width, FaceD->height );               
        CvSize ROISize= cvSize(FaceD->width, FaceD->height );               
        IplImage* ImgROI =  cvCreateImage( ROISize, Src->depth, Src->nChannels);   
        cvSetImageROI(Src, ROIRect);               
        cvCopy(Src, ImgROI, NULL);               
        cvResetImageROI(Src);       
        IplImage* ImgROIGray = cvCreateImage( ROISize, IPL_DEPTH_8U, 1);
        cvCvtColor(ImgROI, ImgROIGray, CV_RGB2GRAY );                 
       
        //Encircle the face

        CvPoint FaceCenter = cvPoint(FaceD->width/2,FaceD->height/2);
        IplImage *res = cvCreateImage(Size(ImgROI->width,ImgROI->height), IPL_DEPTH_8U, 1);
        IplImage *roi = cvCreateImage(Size(ImgROI->width,ImgROI->height), IPL_DEPTH_8U, 1);
        cvZero(roi);cvZero(res);   
       
        cvEllipse(roi, FaceCenter, cvSize(FaceD->width/2*0.85, FaceD->height/2*1.05), 0.0, 0.0, 360.0,    CV_RGB(255,255,255), -1, 8, 0);
        cvAnd(ImgROIGrayEq, ImgROIGrayEq, res, roi); 

        cvNamedWindow("win");cvShowImage("win", res);cvWaitKey(10);            

}


samedi 27 avril 2013

Memory Managment in OpenCV_C++

Unlike Java language, C++ isn't a garbage collector, therefore you need to manually deallocates memory every time its use is over. If you ignore this detail, you will have out-of-memory problem since your RAM couldn't supply all the space you are asking for. So you need to know perfectly handling the memory space you are using.
In the following sections I will show briefly how to get rid of allocated memory.

new and delete
if you allocate memory using new, you compulsorily deallocate memory using delete operator:
- Memory allocation/deallocation for an integer
// allocate memory using new operator 
int *var = new int; 
//deallocates memory using delete operator 
delete var;
- Memory allocation/deallocation for a table
//Allocate table of 100 elements
int *a = new int[100];
//Delete the array element
delete [] a; a = NULL;

create and release
To fix the memory leak problems with OpenCV, you should check if you have release the object you have already created. In OpenCV there are many create function and their corresponding release function. If you are creating something and want later to return it, make sure that you didn't release it. 
Here some list of the most used functions:
- CvSeq -- > cvClearSeq
- CvMemStorage --> cvReleaseMemStorage
- CvHaarClassifierCascade --> cvReleaseHaarClassifierCascade
- cvCreateImageHeader –-> cvReleaseImageHeader
- cvCreateImage –-> cvReleaseImage
- cvCreateMat –-> cvReleaseMat
- cvCreateMatND –-> cvReleaseMatND
- cvCreateData –-> cvReleaseData
- cvCreateSparseMat –-> cvReleaseSparseMat
- cvCreateGraphScanner –-> cvReleaseGraphScanner
- cvOpenFileStorage –-> cvReleaseFileStorage
- cvAlloc –-> cvFree
- ... etc.


If you have create a table of image, before releasing the table (with delete) you need to go over each table case and release the allocated image (of course after its use is over). Here is an example:
// Allocation of table of images
IplImage** TabImg = new IplImage* [NbImage];
for(int i=0; i<NbImage; i++)
{TabImg[i]=cvCreateImage(cvSize(100, 100), IPL_DEPTH_8U, 1); }
// Use the Table of image 
.....................................
//Free the Table of images
for(int i=0; i<NbImage; i++)
 {cvReleaseImage(&TabImg[i]);} //Free each table case
 delete[] TabImg; TabImg = NULL; //Free the tale itself

Then your memory is pretty free ...

Freedom is good not only for human but also for memory ;-)

mercredi 27 mars 2013

How to install OpenCV 3.2.1 on Visual Studio 2010 ( Windows XP)


This tutorial assumes that you have already installed Microsoft Visual Studio 2010 and downloaded the library OpenCV 3.2.1( here...)
These step are supposed to be done for every new project in Visual Studio 2010 (Not like Microsoft Visual Express, it is supposed to be configurated just one time)
  1. Create your new project.
  2. Go to Project>Properties>Configuration Properties>VC++ Directories>Include Directories (<Edit...>) then add the path of include files: D:\opencv2.3.1\build\include
  3. Go to Project>Properties>Configuration Properties>Linker> General > Additional Library Directories (<Edit...>) and add Library path: D:\opencv2.3.1\build\x86\vc10\lib
  4. Go to Project>Properties>Configuration Properties>Linker > Input > Additional Dependencies and add all library you are going to use in your project (Here I will mention the main used Lib)
    opencv_core231d.lib
    opencv_imgproc231d.lib
    opencv_highgui231d.lib
    opencv_ml231d.lib
    opencv_features2d231d.lib
    opencv_calib3d231d.lib

  5. Don't forget to add the path of the bin file (D:\opencv2.3.1\build\x86\vc10\bin) in the Environment Variables dialogue (the System Variables box select Path and then Edit).

jeudi 21 février 2013

OpenCV supports Java on desktop



As of OpenCV 2.4.4, OpenCV supports desktop Java development using nearly the same interface as for Android development. This guide will help you to create your first Java (or Scala) application using OpenCV. Here... 

mercredi 13 février 2013

Draw an Arrow with OpenCV (and python version too)

OpenCV does not provide functions to draw an arrow. We will introduce a simple way to do it!
First, we draw a simple line (easy). Then, we plot the two segments that represent the arrow. These segments present an angle of 45 degrees (pi / 4) with the main line.
Here is the code for that:
#include <stdio.h>
#include <cv.h>
#include <highgui.h>


void drawArrow(IplImage *image, CvPoint p, CvPoint q, CvScalar color, int arrowMagnitude = 9, int thickness=1, int line_type=8, int shift=0) 

{
    //Draw the principle line
    cvLine(image, p, q, color, thickness, line_type, shift);
    const double PI = 3.141592653;
    //compute the angle alpha
    double angle = atan2((double)p.y-q.y, (double)p.x-q.x);
    //compute the coordinates of the first segment
    p.x = (int) ( q.x +  arrowMagnitude * cos(angle + PI/4));
    p.y = (int) ( q.y +  arrowMagnitude * sin(angle + PI/4));
    //Draw the first segment
    cvLine(image, p, q, color, thickness, line_type, shift);
    //
compute the coordinates of the second segment
    p.x = (int) ( q.x +  arrowMagnitude * cos(angle – PI/4));
    p.y = (int) ( q.y +  arrowMagnitude * sin(angle – PI/4));
    //
Draw the second segment
    cvLine(image, p, q, color, thickness, line_type, shift);
}  


//How to call this function
int main()

 {
    

   IplImage *canvas = cvCreateImage(cvSize(320, 240), IPL_DEPTH_8U, 3);
    cvNamedWindow(“win”);  
    drawArrow(canvas, cvPoint(10, 20), cvPoint(100, 150), CV_RGB(100, 0, 255), 20);
    cvShowImage(“win”, canvas);
    cvWaitKey();
    cvReleaseImage(&canvas);
    cvDestroyAllWindows();
    return 1;

} 

vendredi 8 février 2013

How to install OpenCV 2.3 on Microsoft Visual C++ Express 2008 (Windows XP)

OpenCV is an open source computer vision library available from here .... The library is written in C and C++ and runs under Linux, Windows and Mac OS X. There is active development on interfaces for Python, Ruby, Matlab, and other languages.  

OpenCV was conceived as a way to make computer vision infrastructure universally available. With the aid of Intel’s Performance Library Team,* OpenCV started with a core of implemented code and algorithmic specifications being sent to members of Intel’s Russian library team. This is the “where” of OpenCV: it started in Intel’s research lab with collaboration from the Soft ware Performance Libraries group together with implementation and optimization expertise in Russia.

For more details of OpenCV, I recommend this book :"Learning OpenCV Computer Vision With The OpenCV Library" which could be found on this link.


This tutorial assumes that you have already installed Microsoft Visual C++ Express 2008 and includes the following steps:
  1. Step1: Downloading and Installing OpenCV OpenCV 
  2. Step2: Include the Bin File and Lib File 
  3. Step3: Configure Visual Studio to use OpenCV 
  4. Setp4:Create a sample project which makes use of OpenCV 2.3

Step1: Downloading and Installing OpenCV 2.3
Go to the folder where you downloaded the executable file and select “Run as Administrator”.
The executable file is essentially an archive of files with an extractor built in. It will ask you to select the location where you would like it to extract all its contents. Select for exemple C:\ as the path and click Extract. It will create a folder called opencv2.3 with the path: C:\opencv2.3

Step2: Include the Bin File and Lib File 
Add the path of the bin and lib file in the ( C:\opencv2.3\build\x86\vc9\lib and C:\opencv2.3\build\x86\vc9\bin ) Environment Variables dialogue (the System Variables box select Path and then Edit).

Step3: Configure Visual Studio to use OpenCV :

* Add Executable, Include and Lib File
This step is done only one time while installing OpenCV Bib:
1- Open Visual C++ Express 2008
2- Go to Tools →  Options → Project and Solution → VC++ Directories
3- Select in the right combox Binary file and add the binary file located in C:\opencv2.3\build\x86\vc9\bin
4-  Select in the right combox Include file and add the three path of Include file located respectively in
      C:\opencv2.3\build\include\opencv
      C:\opencv2.3\build\include\opencv2
      C:\opencv2.3\build\include
5-  Select in the right combox Bib file and add the bib file located in 
     C:\opencv2.3\build\x86\vc9\lib

* Add Lib File
This step is done for every new Visual C++ Project:
 1- Go to Project → Properties  → Configuration properties  →  Liker  → Input  → Additional Dependencies  and add the bib that you are ging to use, for exemple:
  opencv_core231d.lib  opencv_imgproc231d.lib  opencv_highgui231d.lib  opencv_ml231d.lib  opencv_video231d.lib  opencv_features2d231d.lib  opencv_calib3d231d.lib  opencv_objdetect231d.lib  opencv_contrib231d.lib  opencv_legacy231d.lib  opencv_flann231d.lib

2- Click Apply then OK


Setp4:Create a sample project which makes use of OpenCV 2.3
1- Click File → New →Project
2- In the “Installed Templates” box choose Visual C++ → Win32 and select Win32 Console Application
3- Enter a name for the project and specify  the location where you would like to save your project and then click OK
4- Select the Application setting and shoot the Precompiled header and then clich on Finish. 
5- Click on the right of your mouse on the source file  → Add new element →  specify the name of our file, for example "Test.cpp" then click on Add
6- Type the following code on your Test.cpp File:

 #include "stdafx.h"
 #include <highgui.h>
 
int _tmain(int argc, _TCHAR* argv[])

 {
     int c;

     // allocate memory for an image

     IplImage *img;

     // capture from video device #1

     CvCapture* capture = cvCaptureFromCAM(1);

     // create a window to display the images

     cvNamedWindow("mainWin", CV_WINDOW_AUTOSIZE);

     // position the window

     cvMoveWindow("mainWin", 5, 5);

     while(1)

     {

         // retrieve the captured frame

         img=cvQueryFrame(capture);

         // show the image in the window

         cvShowImage("mainWin", img );

         // wait 10 ms for a key to be pressed

         c=cvWaitKey(10);

         // escape key terminates program

         if(c == 27)

         break;

     }

  return 0;

 }