Basics of OpenCV API



Header files

These are some of important OpenCV header files for C++ interface. As a beginner, you will need few of these header files for your application. In my following lessons, I will include only necessary header files to my example programs. If you are not sure what to include, include them all. No any penalty incurred for including all this header file except for a fact that the length of your source code will increased by few lines than necessary.
    • #include "opencv2/core/core.hpp"
    • #include "opencv2/flann/miniflann.hpp"
    • #include "opencv2/imgproc/imgproc.hpp"
    • #include "opencv2/photo/photo.hpp"
    • #include "opencv2/video/video.hpp"
    • #include "opencv2/features2d/features2d.hpp"
    • #include "opencv2/objdetect/objdetect.hpp"
    • #include "opencv2/calib3d/calib3d.hpp"
    • #include "opencv2/ml/ml.hpp"
    • #include "opencv2/highgui/highgui.hpp"
    • #include "opencv2/contrib/contrib.hpp"
    • #include "opencv2/core/core_c.h"
    • #include "opencv2/highgui/highgui_c.h"
    • #include "opencv2/imgproc/imgproc_c.h"


Namespace 

All OpenCV classes and functions are in cv namespace. So, you have to do one of following
  • Add the 'using namespace cv' line just after including your header files (I have used this method in all my sample programs)
e.g.
                              #include "opencv2/core/core.hpp"
                              using namespace cv; 
                              int main()
                              {
                                         Mat frame = cvQueryFrame( capture );
                                         imshow( "Video", frame );
                              }



  • append the cv:: specifier at the beginning of every OpenCV classes, functions and data structures in your source code
e.g.                              
                              #include "opencv2/core/core.hpp"

                              int main()
                              {
                                         cv::Mat frame = cvQueryFrame( capture );
                                         cv::imshow( "Video", frame );
                              }


Data Types for Arrays

Data type of an array defines the number of bits allocated for each element of array (pixels in an image) and how the value is represented using those bits. Any array elements should have one of following data types.

For single channel arrays : 

  • CV_8U (8 bit unsigned integer)
  • CV_8S (8 bit signed integer)
  • CV_16U (16 bit unsigned integer)
  • CV_16S (16 bit signed integer)
  • CV_32S (32 bit signed integer)
  • CV_32F (32 bit floating point number)
  • CV_64F (64 bit float floating point number)
e.g. : Here I have illustrated a single channel array with 8 bit unsigned integers. As the datatype of this array is 8 bit unsigned integers, each element should have a value from 0 to 255.
Single Channel Array with 8 bit unsigned integers
Single Channel Array

For multi channel arrays :

We can define all of above data types for multi channel arrays (supports up to 512 channels). Here I am going to show you how to define CV_8U data type for multi channel arrays.
  • CV_8UC1 (single channel array with 8 bit unsigned integers) 
  • CV_8UC2 (2 channel array with 8 bit unsigned integers)
  • CV_8UC3 (3 channel array with 8 bit unsigned integers)
  • CV_8UC4 (4 channel array with 8 bit unsigned integers)
  • CV_8UC(n) (n channel array with 8 bit unsigned integers (n can be from 1 to 512) )
e.g. 1 : Here I have illustrated a 3 channel array with 8 bit unsigned integers. As the datatype is 8 bit unsigned integers, each element should have a value from 0 to 255. Because this is a 3 channel array, array consists of tuples with 3 elements. The first tuple is {54, 0, 34}, second tuple is {58, 78, 185} and so on.
3 - Channel Array for 8 bit unsigned integers
3 Channel Arrays


e.g. 2 : Here I have illustrated a 2 channel array with 8 bit signed integers. As the datatype is 8 bit signed integers, each element should have a value from -128 to 127. Because this is a 2 channel array, array consists of tuples with 2 elements. The first tuple is {-85, -127}, second tuple is {25, 23} and so on.
2 Channel Array with 8 bit signed integers
2 Channel Array
Note : CV_8U = CV_8UC1 = CV_8UC(1)

Example Usage :

    • Mat img1(3, 5, CV_32F ); //3 x 5 single-channel array with 32 bit floating point numbers
    • Mat img2(23, 53, CV_64FC(5) ); //23 x 53 5-channel array with 64 bit floating point numbers
    • Mat img3(Size(100, 200), CV_16UC2 ); //100 x 200 2-channel array with 16 bit unsigned integers
Remember :
Some OpenCV functions can handle only a subset of above data types. So, be careful, when using OpenCV functions. 


Bit Depths for IplImage (C style)
    • IPL_DEPTH_<bit_depth>(S|U|F)
      • Here possible values for <bit_depth> are 1,8,16,32 and 64
      • S = Signed
      • U = Unsigned 
      • F = Float
      • 1 bit depth images should be unsigned
      • 8 bit depth images should be signed or unsigned
      • 16 bit depth images should be signed or unsigned
      • 32 bit depth images should be signed or float
      • 64 bit depth images should be float
    • E.g.:  
      • IPL_DEPTH_1U (1 bit depth and unsigned)
      • IPL_DEPTH_8U (8 bit depth and unsigned)
      • IPL_DEPTH_16U
      • IPL_DEPTH_32F ( 32 bit depth and float )
      • IPL_DEPTH_8S
      • IPL_DEPTH_16S ( 16 bit depth and signed )
      • IPL_DEPTH_32S
      • IPL_DEPTH_64F
Bit depth means the number of bits allocated for a pixel. For example, IplImage with IPL_DEPTH_8U uses 8 bit unsigned integer per each pixel. That means each pixel can hold 0 to 255 integer numbers. 

IPL_DEPTH_8U, IPL_DEPTH_8S, IPL_DEPTH_16S,  IPL_DEPTH_32S, IPL_DEPTH_32F and IPL_DEPTH_64F are currently supported by IplImage data structure.



42 comments:

  1. Thanks for the tutorial! I ran into a problem with this, however:

    After following the steps in the previous tutorial, the header file cv.h was still not recognized by Visual Studio (2010 Express).

    I had to change cv.h to
    C:\opencv\build\include\opencv\cv.h
    to successfully build the project.

    Regards

    ReplyDelete
    Replies
    1. after setting environment variables ..

      Restart your PC

      Delete

    2. windows 7 upgrade key store review , genuine windows 8 enetrprise key , windows 10 activation server , Genuine Windows 10 Pro Online Key Sale , windows.2003.enterprise.r2.sp2 key , Buy Windows 7 Product Key Online , windows 7 utilities crack , windows 7 pro anytime upgrade from home premium trial , l3Q4vl

      cheap office project 2013 product keys

      buy windows 7 ultimate sp1 product key

      Online Windows 10 Pro Product Key Store

      Windows 7 Home Premium SP1 key code

      buy cheap windows 7 product keys

      Delete
  2. Welcome : so what do you mean by a channel ??

    ReplyDelete
    Replies
    1. A channel means a matrix which contains pixel values.
      Gray scale image (black and white image) have only a channel. So, single matrix is enough to represent a gray scale image.
      Color image consists of three channels to represent red, blue and green colors. So, there should be three matrixes. So, color of each and every pixel is represented by 3 values in the 3 matrixes. By combination of these 3 colors you can create any color.

      Delete
  3. Unhandled exception at 0x000007fefced9e5d in NEW-PROJECT.exe: 0xC06D007E: Module not found.


    I am getting this error while debugging ur code for reading an image.....

    I am using Microsoft Visual Studio 2010 Professional Edition and OpenCV 2.4.5

    My System is 64 bits

    ReplyDelete
    Replies
    1. Please follow this lesson
      http://opencv-srf.blogspot.com/2013/05/installing-configuring-opencv-with-vs.html

      You will need to replace x86 with x64 because your system is 64 bit

      Delete
    2. i can't read cvQueryFrame ???

      Delete
  4. no errors in writing and building
    but in debugging it shows error MSVCP110D.dll is missing from your computer.

    how to solve this ??

    ReplyDelete
    Replies
    1. perhaps u have mismatched architecture.. WIN86 and x64 clash. I was also getting, but then after this, it's resolved. --jj

      Delete
  5. I understand till 3 Channel Array? But what is an n-channel array where n > 3?
    what is there beyond Red, Blue and green?

    Also what is this IPL_image?

    ReplyDelete
    Replies
    1. Apart from red, blue and green, there is a channel known as an alpha channel which makes images transparent.
      And you can define any number of channels for the sake of processing data. It's up to you to use those arrays with any number of channels although there are no standard image channels more than 4 as far as I know. (There may be more than 4 channels)

      IplImage is a data type (similar to cv::Mat in C++) used in C style OpenCV

      Delete
  6. When i'm trying to compile the code given the following error occured.

    ------ Build started: Project: Sample, Configuration: Debug Win32 ------
    1> Sample.cpp
    1>c:\users\vyaswant\documents\visual studio 2010\projects\sample\sample\sample.cpp(8): error C2065: 'capture' : undeclared identifier
    1>c:\users\vyaswant\documents\visual studio 2010\projects\sample\sample\sample.cpp(8): error C3861: 'cvQueryFrame': identifier not found
    1>c:\users\vyaswant\documents\visual studio 2010\projects\sample\sample\sample.cpp(9): error C2039: 'imshow' : is not a member of 'cv'
    1>c:\users\vyaswant\documents\visual studio 2010\projects\sample\sample\sample.cpp(9): error C3861: 'imshow': identifier not found
    ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

    ReplyDelete
    Replies
    1. i have got same issue after configuring every thing.plz any one help

      Delete
  7. On your example:
    Mat img3(Size(100, 200), CV_16U2 ); //100 x 200 single-channel array with 16 bit unsigned integers
    Isn't that a two-channel array?

    ReplyDelete
    Replies
    1. Your welcome, great tutorials by the way really helpful. Keep up the great work.

      Delete
  8. Thank you so much. The best tutorial I have came so far. Please keep up the good work :)

    ReplyDelete
  9. i have follow all steps of configuration when i compiled this error occured help??
    Error 1 error LNK1104: cannot open file 'opencv_calib3d245d.lib'

    ReplyDelete
  10. "If you are not sure what to include, include them all. No any penalty incurred for including all this header file except for a fact that the length of your source code will increased by few lines than necessary."

    This is a very bad idea, and bad practice. This would increase your compile time unnecessary.

    ReplyDelete
  11. any help plz
    LINK : fatal error LNK1104: cannot open file 'opencv_calib3d245d.lib'

    ReplyDelete
    Replies
    1. rewrite opencv_calib3d245d.lib or copy the name of same lib from bin

      Delete
  12. Great blog Thanks for share click itHow To Remove Rundll ErrorGet free from rundll error .
    Thank you
    Aalia lyon

    ReplyDelete
  13. Hey for everybody who has problem with LINKER : change Debug from defualt "Win32" into "x64" ( u have to add new platform in configuration manager) this worked in my case.

    ReplyDelete
    Replies
    1. Thank you so much for the help!
      I had been struggling with linker errors for very long, but did not understand how to resolve them.
      Thanks again :)

      Delete
  14. i'm sure i've followed all steps correctly but still i'm getting "error 2605: 'capture': undeclared identifier"

    Can someone help me please..

    ReplyDelete
    Replies
    1. I'm having the same problem

      Delete
  15. fatal error LNK1104: cannot open file 'opencv_haartraining_engined.lib'

    ReplyDelete
  16. this working with opencv2.4.9 and sublimetext in windows 8

    ReplyDelete
  17. sir, i have to make face detection and tracking.. how to do that??

    ReplyDelete
  18. Great tutorial. thanks very much.

    ReplyDelete
  19. Hi. By looking at your writings i can only specify an images size and dahatype of the pixels. But i want to determine pixel values too. How can i do that ?

    ReplyDelete
  20. 1>------ Build started: Project: HelloWorld, Configuration: Debug Win32 ------
    1>Compiling...
    1>stdafx.cpp
    1>d:\face\helloworld\helloworld\stdafx.h(12) : fatal error C1083: Cannot open include file: 'opencv2/core/core.hpp': No such file or directory
    1>Project : warning PRJ0018 : The following environment variables were not found:
    1>$(OPENCV_DIR)

    THis is the error,what should i do?/ I am using VS 8 with 64 bit processor

    ReplyDelete
  21. Hi Mr.Fernando!
    What is the difference between datatype of an Array and bit depth of IPL Image? These two lines "Bit depth means the number of bits allocated for a pixel" and "Data type of an array defines the number of bits allocated for each element of array (pixels in an image)" mean the same thing. So how are they different?
    -Nirav

    ReplyDelete
  22. Broadband Services in Zimbabwe-Open Access is popular hotspot, internet and broadband service provider in Zimbabwe. That is not matter that where you are in Zimbabwe. Visit us for current list of Open Access Hotspots.

    ReplyDelete
  23. i got alot of infos and advices from this post thank you so much

    ReplyDelete
  24. Thank you for this excellent post. I have just started to read this article carefully. Keep up posting these type of post continuously.
    Leak Detection Switch

    ReplyDelete
  25. Thank you sir. for this excellent and amazing post

    ReplyDelete