OpenCV Basics
What will you learn
- Installation steps
- Intro to API
- Basic Data Types
At the end you should be able to
- Understand OpenCV Data types
- Understand Mat data Type
Installation
I personally use Ubuntu 16.04 64 bit x86, Hence all these steps are tried and tested on my system, I would suggest you to mirror my configuration for smoother experience with the course.
Most online resources say to compile OpenCV 3 on Ubuntu, But since I am lazy I found out an ppa for OpenCV 3, ppa looks good, I just hope that it does not come back and bite me in my ass later.
Here are the steps for installing openCV 3 on Ubuntu 16.04
sudo add-apt-repository ppa:lkoppel/opencv
sudo apt-get update
sudo apt install libopencv-calib3d3.2 libopencv-core3.2 libopencv-features2d3.2 libopencv-flann3.2 libopencv-highgui3.2 libopencv-imgcodecs3.2 libopencv-imgproc3.2 libopencv-ml3.2 libopencv-objdetect3.2 libopencv-photo3.2 libopencv-shape3.2 libopencv-stitching3.2 libopencv-superres3.2 libopencv-video3.2 libopencv-videoio3.2 libopencv-videostab3.2 libopencv-viz3.2 libopencv3.2-jni libopencv3.2-java libopencv-contrib3.2
sudo apt install libopencv-calib3d-dev libopencv-core-dev libopencv-features2d-dev libopencv-flann-dev libopencv-highgui-dev libopencv-imgcodecs-dev libopencv-imgproc-dev libopencv-ml-dev libopencv-objdetect-dev libopencv-photo-dev libopencv-shape-dev libopencv-stitching-dev libopencv-superres-dev libopencv-video-dev libopencv-videoio-dev libopencv-videostab-dev libopencv-viz-dev libopencv-contrib-dev libopencv-dev
Introduction to OpenCV API
OpenCV program is divided into modules each module is a collection of API's related to one particular function in CV.
Most common modules are
- core - Contains basic datatypes and Mat type.
- highgui - provides UI capabilities and video and image capturing capabilities
- imgproc - includes image processing functions like filtering, color space conversion etc.
- features2d - Contains API for feature detection, feature matching and feature description
- objdetect - Containd API for object detection and predefined classes for (face, eyes, smile etc..)
- video - Contains API for video analysis like motion and tracking.
- gpu - Contains API for gpu accelerated algorithms
- ml - Contains API for Machine learning tools like classification, regression and data clustering.
OpenCV Classes
- All opencv functions are in one class called
cv.
OpenCV DataTypes
All primitive (basic) datatypes for OpenCV are
- bool
- unsigned char
- signed char
- unsigned short
- signed short
- int
- float
- double
- Tuple of any of the above data types
These datatypes are defined by
CV_<bit depth>{U|S|F}C(<number of channels>)
In the above line, U, S, F Stand for unsigned, signed and float.
For example : Lets say that you have to represent an 8 bit unsigned value then you will use this data type
CV_8U
Practically this data type would be used to represent an grayscale pixel
Lets say you want to do the same for RBG then you would use a Tuple of 3 channels (one for R, G and B) of the 8 bit unsigned integer, so the data type becomes.
CV_8UC(3)
OpenCV Mat Class
It is used to store n-dimensional single or multi-channel arrays.
Practically it can be used to store
- real valued vectors or matrices
- complex valued vectors ot matrices
- Colored images
- grayscale images
- Histograms
Create Mat object
Use constructor to create Mat objects, syntax for the same is
Mat(nrows, ncols, type[, fillValue])
For example: You want to store an RGB image of Resolution 640x320 and initialize it with Green color.
Mat img_A (640,320, CV_8UC(3), Scalar(0,255,0))
/* Mat with 640 rows and 320 cols
with data type 8 bit unsigned Tuple with 3 channels (to store B,G,R values)
Initialized to all pixels containing values of Blue=0, Green=255 and Red=0
*/
Note : Scalar is a type of vec datatype in OpenCV TODO : Study more of Scalar.