1

OpenCv Book and basics

Posted by SUYOG PATIL on 11:03 PM in , , ,
Hello guys,
After long time i am writing this tutorial on OpenCV.
Now you must have some basic idea about OpenCV and how to setup it in different IDEs from my last posts.If you have any query then feel free to ask by mailing me or commenting directly below the post.Comments are preferred as they will help others too in debugging.

Let us start with the book which should be read to get an brief knowledge about OpenCV structure and programming methods.I want to suggest you only one book which i read during early days of my learning OpenCV and that is "Learning OpenCV by O'Reilly".The book is available on net for opensource community.

It is truly an awesome book to start and you must read it....
Most of my examples and code samples will be based on them..

You can read blogs also to get tutorials and code samples directly for small applications.
useful links:
Introduction To Computer Vision Using OpenCV
Introduction to OpenCV
openCV
FullOpenCVWiki
Introduction OpenCV

If you go through these sites thoroughly then you will become master in Opencv in few days!!!

So lets start with OpenCV naming conventions.
  • Function naming conventions:
        cvActionTargetMod(...)
        Action = the core functionality (e.g. set, create)
        Target = the target image area (e.g. contour, polygon)
         Mod    = optional modifiers (e.g. argument type)
      
  • Matrix data types:
        CV_<bit_depth>(S|U|F)C<number_of_channels>
    
        S = Signed integer
        U = Unsigned integer
        F = Float 
    
        E.g.: CV_8UC1 means an 8-bit unsigned single-channel matrix, 
              CV_32FC2 means a 32-bit float matrix with two channels.
     
  • Image data types:
        IPL_DEPTH_<bit_depth>(S|U|F)
    
        E.g.: IPL_DEPTH_8U means an  8-bit unsigned image.
              IPL_DEPTH_32F means a 32-bit float image.
     
  • Header files:
        #include <cv.h>
        #include <cvaux.h>
        #include <highgui.h>  
 These conventions may look confusing at the beginning but as we proceed you will get more familiar to them.

Basic OpenCV data structures

Image data structure


  • IPL image:
    IplImage
    |-- int  nChannels;     // Number of color channels (1,2,3,4)
    |-- int  depth;         // Pixel depth in bits: 
                           //   IPL_DEPTH_8U, IPL_DEPTH_8S, 
    |                       //   IPL_DEPTH_16U,IPL_DEPTH_16S, 
    |                       //   IPL_DEPTH_32S,IPL_DEPTH_32F, 
    |                       //   IPL_DEPTH_64F
    |-- int  width;         // image width in pixels
    |-- int  height;        // image height in pixels
    |-- char* imageData;    // pointer to aligned image data
    |                       // Note that color images are stored
    in BGR order
    |-- int  dataOrder;     // 0 - interleaved color channels, 
    |                       // 1 - separate color channels
    |                       // cvCreateImage can only create 
                               interleaved images
    |-- int  origin;        // 0 - top-left origin,
    |                       // 1 - bottom-left origin 
                               (Windows bitmaps style)
     
    |-- int  widthStep;     // size of aligned image row in bytes
    |-- int  imageSize;     // image data size in 
                              bytes = height*widthStep
    |-- struct _IplROI *roi;// image ROI. when not NULL specifies 
    |                       // image region  to be processed.
    |-- char *imageDataOrigin; // pointer to the unaligned origin 
                                 of image data
    |                   // (needed for correct image deallocation)
    |
    |-- int  align; // Alignment of image rows: 4 or 8 byte alignment
    |              // OpenCV ignores this and uses widthStep instead
    |-- char colorModel[4]; // Color model - ignored by OpenCV
    




 

Matrices and vectors 



  • Matrices:
    CvMat                      // 2D array
      |-- int   type;          // elements type (uchar,short,int,float,double)
                                and flags
      |-- int   step;          // full row length in bytes
      |-- int   rows, cols;    // dimensions
      |-- int   height, width; // alternative dimensions reference
      |-- union data;
          |-- uchar*  ptr;     // data pointer for an unsigned char matrix
          |-- short*  s;       // data pointer for a short matrix
          |-- int*    i;       // data pointer for an integer matrix
          |-- float*  fl;      // data pointer for a float matrix
          |-- double* db;      // data pointer for a double matrix
    
    
    CvMatND               // N-dimensional array
      |-- int   type;     // elements type(uchar,short,int,float,double) 
                             and flags
      |-- int   dims;     // number of array dimensions
      |-- union data;
      |   |-- uchar*  ptr;// data pointer for an unsigned char matrix
      |   |-- short*  s;  // data pointer for a short matrix
      |   |-- int*    i;  // data pointer for an integer matrix
      |   |-- float*  fl; // data pointer for a float matrix
      |   |-- double* db; // data pointer for a double matrix
      |
      |-- struct dim[]; // information for each dimension
          |-- size; // number of elements in a given dimension
          |-- step; // distance between elements in a given dimension
    
    
    CvSparseMat // SPARSE N-dimensional array
    
  • Generic arrays:
    CvArr* 
    // Used only as a function parameter to specify that the
    // function accepts arrays of more than a single type, such
    // as: IplImage*, CvMat* or even CvSeq*. The particular array 
    // type is determined at runtime by analyzing the first 4
    // bytes of the header of the actual array.
    

  • Scalars:
    CvScalar
      |-- double val[4]; //4D vector
    
    Initializer function:
    CvScalar s = cvScalar(double val0, double val1=0, double val2=0, double val3=0);
    
    Example:
    CvScalar s = cvScalar(20.0);
    s.val[0]=10.0;
    
  • Points:
    CvPoint      p = cvPoint(int x, int y);
    CvPoint2D32f p = cvPoint2D32f(float x, float y);
    CvPoint3D32f p = cvPoint3D32f(float x, float y, float z);
    
    E.g.:
    p.x=5.0;
    p.y=5.0;
    
  • Rectangular dimensions:
    CvSize       r = cvSize(int width, int height);
    CvSize2D32f  r = cvSize2D32f(float width, float height);
    
  • Rectangular dimensions with offset:
    CvRect       r = cvRect(int x, int y, int width, int height);
    




Ohh quite big...hence I am stopping here. Next time we will see some 
code examples so that you understand it better.Till then keep 
referring those sites which I have given for their good information 
content.My tutorials are based on those links and Oreilly book.
So keep in touch.

 
Have a great programming summer!!!




|
0

Overview of OpenCV and setting up OpenCV in Visual Studio 2005/08/10

Posted by SUYOG PATIL on 8:22 PM in , , ,
Overview of OpenCV :
  • CV:Image processing and vision algorithms
  • HighGUI-GUI, Image and Video I/O
  • CXCORE-basic structures and algoritms,XML support, drawing functions
  • CvauxAuxiliary(experimental) OpenCv functions
  • CvCamcross-platform module for processing video stream from digital video cameras
  • ML-MachineLearning methods
Documentation via the Wiki
OpenCV’s documentation Wiki is more up-to-date than the html pages that ship with
OpenCV and it also features additional content as well. Th e Wiki is located at http://
opencvlibrary.SourceForge.net. It includes information on:
Instructions on compiling OpenCV using Eclipse IDE
• Face recognition with OpenCV
• Video surveillance library
• Tutorials
• Camera compatibility
• Links to the Chinese and the Korean user groups
Another Wiki, located at http://opencvlibrary.SourceForge.net/CvAux, is the only documentation
of the auxiliary functions discussed in “OpenCV Structure and Content”
(next section).
CvAux includes the following functional areas:
• Stereo correspondence
• View point morphing of cameras
• 3D tracking in stereo
• Eigen object (PCA) functions for object recognition
• Embedded hidden Markov models (HMMs)



Regardless of  documentation source, it is often hard to know:
• Which image type (floating, integer, byte; 1–3 channels) works with which
function
• Which functions work in place
• Details of how to call the more complex functions (e.g., contours)


So questions above will be answered soon when we will discuss about actual programming....Now let us start....


Setting up OpenCV in Visual Studio 2005/08/10:

1)Creating the Project:

A project is initially created by selecting:
File -> New -> Project


Create a “Win32 Console Project

Make it an “Empty Project” by selecting the box under “Application Settings”

2

























 2)
Create the First File
Right Click the “Source Files” Folder under the project name 
Add -> Add new Item
 


Select “C++ file” and give it a name


Creating a file makes it possible to set “Additional Include Directives” in the C/C++ pane under the project properties.
In order to build projects using OpenCV the required libraries and directives must be included in the project’s properties.
Open the Properties Pane
Right Click the name of the project and select “Properties”



Set Additional Include Directives
Under the C/C++ tab select “General


Select the “Additional Include Directives
Add the full path to each of the folders which contain “.h” files required to use OpenCV
Be sure to include trailing “\
Utilized Directives
C:\Program Files\OpenCV\cvaux\include\
C:\Program Files\OpenCV\cxcore\include\
C:\Program Files\OpenCV\cv\include\
C:\Program Files\OpenCV\otherlibs\highgui\
                                                 C:\Program Files\OpenCV\otherlibs\cvcam\include
 (This was for old Opencv versions)


For Opencv 2.2
C:\OpenCV2.2\include\opencv
C:\OpenCV2.2\include
C:\OpenCV2.2\lib






 
 

























Set Additional Dependencies
Under the Linker tab select “Input
Select the “Additional Dependencies
Add the full path to each of the “.lib” files required to use OpenCV
Be sure to keep the paths in quotes
Utilized Dependencies
"C:\Program Files\OpenCV\lib\cv.lib“
"C:\Program Files\OpenCV\lib\cvaux.lib“
"C:\Program Files\OpenCV\lib\cxcore.lib“
"C:\Program Files\OpenCV\lib\cvcam.lib“
                                                  "C:\Program Files\OpenCV\lib\highgui.lib"


 


 For Opencv 2.2
  
 C:\OpenCV2.2\lib\opencv_core220d.lib
C:\OpenCV2.2\lib\opencv_highgui220d.lib
C:\OpenCV2.2\lib\opencv_video220d.lib
C:\OpenCV2.2\lib\opencv_ml220d.lib
C:\OpenCV2.2\lib\opencv_legacy220d.lib
C:\OpenCV2.2\lib\opencv_imgproc220d.lib
C:\OpenCV2.2\lib\opencv_objdetect220d.lib
C:\OpenCV2.2\lib\opencv_calib3d220d.lib


If you are using  OpenCV2.3 then just change names of include and library files accordingly.
Now that the environment is configured it would be a good idea to test it to make sure that a program will correctly build and run.
 


Testing the First Program
The sample code provided with Opencv can be cut and pasted into the file created in the project space to test OpenCV


You can try following program as given in one tutorial which I read at beginning days of learning Opencv.

#include <cv.h>
#include <highgui.h>
/*
  This will pop up a small box with "Hello World" as the text.
  @author: Gavin Page, gsp8334@cs.rit.edu
  @date: 28 November 2005
*/
int main( int argc, char** argv ) {
  //declare for the height and width of the image
  int height = 320;
  int width = 240;
  //specify the point to place the text
  CvPoint pt = cvPoint( height/4, width/2 );
  //Create an 8 bit, 3 plane image
  IplImage* hw = cvCreateImage(cvSize(height, width), 8, 3);
  //initialize the font
  CvFont font;
  cvInitFont( &font, CV_FONT_HERSHEY_COMPLEX,
  1.0, 1.0, 0, 1, CV_AA);
  //place the text on the image using the font
  cvPutText(hw, "Hello World", pt, &font, CV_RGB(150, 0, 0) );
  //create the window container
  cvNamedWindow("Hello World", 0);
  //display the image in the container
  cvShowImage("Hello World", hw);
  //hold the output windows
  cvWaitKey(0);
  return 0;
}

Building the Program
The program is built by selecting:
Build -> Build Solution
Or by pressing “F7
Running the Program
The program is run by selecting:
Debug -> {Start||Start without Debugging}
Or by pressing “F5” or
<Ctrl>-F5



At this point you should have a working OpenCV project. If the program is not working you should go back and carefully recheck the steps.
 
From here you can explore the documentation to review the functions available.
There are also a number of tutorials on the web including:
Or you can just search for them 
You should also join the OpenCV Community located at:
As of today there are >45000 members available to answer questions. There is also a searchable message board where you can look up previous queries.



For DevC++ or other development environment

Settings


We will suppose that the OpenCV library is installed in the C:\Program Files\OpenCV directory. Now, we have to configure DevCpp that he can take into account, all the includes files (.h), all the static libraries in OpenCV (.lib), and all the dynamic library (.dll) useful for the execution and not for the compilation.
This configuration has been tested with the version 4.9.9.x of DevCPP.
First of all, you have to indicate the header files you want to add. To do that, select Tools->Compiler Options.
compilerOptions.jpg .
Then click on the plus sign to add a new compiler (in fact, only some different options) named here, OpenCV.
newCompiler.jpg
To finish, on the section Add the following ... write : -L"C:\Program Files\OpenCV\lib" -lcxcore -lcv -lcvaux -lhighgui -lml -lcvcam. Or the following for OpenCV 2.1 -L"C:\OpenCV2.1\lib" -lcxcore210 -lcv210 -lcvaux210 -lhighgui210 -lml210
openCVOptions.jpg

Include files configuration


Next, click on Directories and then on C Includes to add all the headers, located in some C:\Program Files\OpenCV subdirectories as shown in the picture.
For OpenCV2.x, you will notice that there are changes in the overall directory structure and hence you won't find the folders shown in the image below. You only need to add C:\Program Files\OpenCV2.x\include\opencv in the include tab to get things to work.
Of course, if you want to write C++ programs, do the same thing on the C++ Includes tab.
includeFiles.jpg

Static library files configuration


The following picture shows the static libraries paths to add.For OpenCV2.x there is no otherlibs\highgui folder and hence just adding C:\Program FIles\OpenCV2.x\bin is sufficient.
libFiles.jpg

Dynamic library files configuration


And to finish, add the bin directory where the dlls are:
binFiles.jpg

Test


Let us choice a C program on the samples directory of OpenCV and try to execute it by typing F9. As you can see, all is OK.
compileProgress.jpg
That's all folk. I hope this short tutorial was useful.


 ....................................................................................................
...............................................................................................

Source-http://opencv.willowgarage.com/wiki/CodeBlocks
                         
                               For CodeBlocks
CodeBlocks is an GPL based and cross-platform IDE. This is the tutorials using CodeBlocks with OpenCV. 

Create a simple console project.


We can use project wizard to create a simple console project. Here is the steps
cbWizardConsole.png
Give this project name of "test_opencv"
cbWizardName.png
Then copy the sample code to main.cpp
cbMainCode.png

Configuring Code::Blocks for OpenCV v2.2


Now you need to configure the compiler to find the OpenCV header files and libraries. OpenCV was made of just 4 libraries that could each be manually added (until v2.1), but from OpenCV v2.2 onwards, there are many more library files so you are highly recommended to use the tool "pkg-config" to setup your OpenCV configuration, as mentioned on CompileOpenCVUsingLinux.
pkg-config is a free command-line tool (available on Windows, Mac and Linux) that should have been automatically setup correctly when you built OpenCV with CMake, so that if you type "pkg-config opencv --cflags" or "pkg-config opencv --libs", it will display the compiler and linker arguments to successfully compile your own OpenCV projects without worrying about where OpenCV is installed or worrying about which version you have installed, or how to link to the library in each Operating System, etc.
If you were compiling your project on the command line you could type:
    cc `pkg-config opencv --cflags` -c main.cpp
    cc `pkg-config opencv --libs` -o test_opencv main.o

(Note: pkg-config is surrounded by back-tick characters, not quote or apostrophe characters (its usually the same key as the Tilda key ~, next to the 1 and Esc keys).
To setup Code::Blocks to use pkg-config, first you should right-click on your project and open the "Build options ..." dialog.
cbBuildOption.png
Now you can simply put this into "Linker settings -> Other linker options":
    `pkg-config opencv --libs`

And put this into "Compiler-> Other options":
    `pkg-config opencv --cflags`

(Remember to include the back-tick characters, by copy-pasting those lines directly into Code::Blocks).
The beauty of this method is that it should work for Linux, Windows and Mac, and for all versions of OpenCV, whereas the old manual method has different lib filenames for different Operating Systems and different versions of OpenCV.

Troubleshooting


If you have tried the pkg-config method above but Code::Blocks does not find OpenCV headers and libs but you have verified that pkg-config works for OpenCV on a command-line, then Code::Blocks probably doesn't know where pkg-config is installed. So you should place a link into "/usr/bin/" (Linux & Mac) or "C:\Windows\" (Windows) so that it will be found. For Linux or Mac, you can find the path to pkg-config if you type this into a terminal:
    which pkg-config

This might print something like "/opt/local/bin/pkg-config".
So then type this in a terminal:
    cd /usr/bin
    ln -s /opt/local/bin/pkg-config pkg-config

This allows accessing pkg-config from the most common program folder "/usr/bin/" as well as it's actual location.

Configuring Code::Blocks using old method for OpenCV v2.0


Set the include header file path (OpenCV v2.0):
Set the library path (OpenCV v2.0):
Add the libraries (OpenCV v2.0) directive:

Configuring Code::Blocks using old method for OpenCV v1.1


Set the include header file path (OpenCV v1.1): cbIncludePath.png
cbLibPath.png

Set the library path (OpenCV v1.1):
Add the libraries (OpenCV v1.1) directive:
cbAddLibs.png

Here is another way you can add the include path and lib path in Code::Blocks


First, you need to add a global variable in Codeblocks, see here: You can open this dialog in:
Codeblocks Menu->settings->global variables, than add a variable named "cv", and choose the "base path" of the "cv"
cb_globalvariable.png
Then you can define the #cv.include and # cv.lib path, in my system,
fill the include edit bar with(this is where your opencv include path locates) : $(#cv)\OpenCV-2.1.0\include\opencv
fill the lib path with(this is where your opencv libraries locate) : $(#cv)\opencv_build\lib
Later, in your Opencv project, you can change the build options like below:
cb_globalvariable_include.png
cb_globalvariable_lib.png
Also, you can add the libraries by using these linker options:
-lcxcore210 -lhighgui210 -lcv210
cb_addlink_using_command.png

Build and Setting input image


cbSetArgument.png
cbSetInputFile.png

Run the build program


cbShowLena.png



 

 


 





|

Copyright © 2009 ALL ABOUT ROBOTICS!! All rights reserved. Theme by Laptop Geek. | Bloggerized by FalconHive.