Difference between RGB and BGR

RGB stands for Red Green Blue. Most often, an RGB color is stored in a structure or unsigned integer with Blue occupying the least significant “area” (a byte in 32-bit and 24-bit formats), Green the second least, and Red the third least.

BGR is the same, except the order of areas is reversed. Red occupies the least significant area, Green the second (still), and Blue the third.

On some platforms (e.g. the Gamegear) a BGR model is used. However, for most, like your computer, RGB is used [citation-needed] (though BGR is supported by many graphic API’s for compatability). OpenCV supports BGR model.

I think because the standard bitmap has the following properties.

  • Bitmaps are saved row-based.
  • When the height is positive, it is a bottom-up bitmap, that is, the last line comes first in memory.
  • A row must be aligned on a 4-byte boundary.
  • Color channel order is BGR (unless specified otherwise by BI_BITFIELDS masks).

Note that the order of the channels is inverse: BGR instead of RGB. Because in many cases the memory is large enough to store the rows in a successive fashion the rows may follow one after another, creating a single long row. Because everything is in a single place following one after another this may help to speed up the scanning process. We can use the isContinuous() function to ask the matrix if this is the case. Continue on to the next section to find an example.

Resources:

http://stackoverflow.com/questions/367449/bgr-color-space

http://stackoverflow.com/questions/14556545/why-opencv-using-bgr-colour-space-instead-of-rgb

Create a blank image and Display

The following code is used to create the image of specific colour. Mat img(500, 1000, CV_8UC3, Scalar(0,0, 100)) is used to create the image of size 500 x 1000, data type of array is CV_8UC3 means 8 bit unsigned int and there are 3 channels for Red, Green and Blue colour. Here, the value assigned to Blue is 0, Green is 0 and Red is 100. So, the image will be displayed in red colour here. It follows the sequence BGR in C++. Mat is a data structure which is used to store image matrix.

#include "opencv2/highgui/highgui.hpp"
#include <iostream>

using namespace cv;
using namespace std;

int main()
{
       //create an image ( 3 channels, 8 bit image depth, 500 high, 1000 wide, (0, 0, 100) assigned for Blue, Green and Red plane respectively. )      
       Mat img(500, 1000, CV_8UC3, Scalar(0,0, 100)); 

       //create a window with the name "MyWindow"       
       namedWindow("MyWindow", CV_WINDOW_AUTOSIZE); 
       //display the image which is stored in the 'img' in the "MyWindow" window
       imshow("MyWindow", img); 

       //wait infinite time for a keypress
       waitKey(0);  

       destroyWindow("MyWindow"); //destroy the window with the name, "MyWindow"

       return 0;
}

To compile use

$ sudo g++ -o nameofFile nameofFile.cpp `pkg-config opencv --cflags --libs`

To run use

$ ./nameofFile

 

Red image will be displayed.

Read and Display

In order to read and display an image in the C++ using OpenCV, you have to load “opencv2/highgui/highgui.hpp” and “opencv2/core/core.hpp”. Here, I’ve used “opencv2/highgui/highgui.hpp” only, because it calls implicitly “opencv2/core/core.hpp”.

#include "opencv2/highgui/highgui.hpp"
#include <iostream>

using namespace cv;
using namespace std;

int main()
{
      //read the image data in the file "MyPic.JPG" and store it in 'img'
      Mat img = imread("MyPic.JPG", CV_LOAD_IMAGE_UNCHANGED); 

      if (img.empty()) //check whether the image is loaded or not
      {
           cout << "Error : Image cannot be loaded..!!" << endl;
           //system("pause"); //wait for a key press
           return -1;
      }

      //create a window with the name "MyWindow"
      namedWindow("MyWindow", CV_WINDOW_AUTOSIZE);
 
      //display the image which is stored in the 'img' in the "MyWindow" window
      imshow("MyWindow", img); 

      //wait infinite time for a keypress
      waitKey(0); 

      //destroy the window with the name, "MyWindow"      
      destroyWindow("MyWindow"); 

      return 0;
}

Before running this program, put the image in the folder of c++ or give path of image. CV_LOAD_IMAGE_UNCHANGED is used to load the image here.

To compile use

$ sudo g++ -o nameofFile nameofFile.cpp `pkg-config opencv --cflags --libs`

To run use

$ ./nameofFile

Things to remember while using OpenCV

It supports the following important OpenCV header files for C++ interface:

#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”

All openCv classes and functions are in cv namespace, so you have to add cv namespace. It supports following data types for arrays:

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)

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) )

Here possible values for <bit_depth> are 1,8,16,32 and 64

OpenCV

    OpenCV is an open source C++ library for real time image processing and computer vision applications, originally developed by Intel and now supported by Willow Garage. The primary interface of OpenCV is C++ but also has C, python, Java interfaces. It is optimized for Intel processing and is available for OS like Mac, Linux, Windows, Android and iOS. This is free for both commercial and non-commercial use. OpenCV has modular structure and the main modules of openCV are listed below:

  1. core
  2. highgui
  3. imgproc
  4. video
  5. objdetect

    Here, ‘core’ is the basic module and has basic image processing functions. The next one, ‘highgui’ provides user interface capabilities, image and video capturing, mouse events, video codec etc. For advance UI capabilities, you can use Qt. ‘imgproc’ includes algorithm like image filtering, transformation, color space conversion etc. ‘video’ is video analysis tool and includes the algorithms like object tracking, background subtraction and the last one ‘objdetect’ provides the algorithms like object detection and recognition.

Installation:

    • Install the Dependencies
sudo apt-get -y install libopencv-dev build-essential cmake git libgtk2.0-dev pkg-config python-dev python-numpy libdc1394-22 libdc1394-22-dev libjpeg-dev libpng12-dev libtiff4-dev libjasper-dev libavcodec-dev libavformat-dev libswscale-dev libxine-dev libgstreamer0.10-dev libgstreamer-plugins-base0.10-dev libv4l-dev libtbb-dev libqt4-dev libfaac-dev libmp3lame-dev libopencore-amrnb-dev libopencore-amrwb-dev libtheora-dev libvorbis-dev libxvidcore-dev x264 v4l-utils unzip
    • Download OpenCV 3.0.0 alpha
mkdir opencv
cd opencv
wget https://github.com/Itseez/opencv/archive/3.0.0-alpha.zip -O opencv-3.0.0-alpha.zip
unzip opencv-3.0.0-alpha.zip
    • Install OpenCV
cd opencv-3.0.0-alpha
mkdir build
cd build
cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local -D WITH_TBB=ON -D BUILD_NEW_PYTHON_SUPPORT=ON -D WITH_V4L=ON -D WITH_QT=ON -D WITH_OPENGL=ON ..
make -j $(nproc)
sudo make install
    • Finishing installation

To get OpenCV working properly, we need to tell Ubuntu:

sudo /bin/bash -c 'echo "/usr/local/lib" > /etc/ld.so.conf.d/opencv.conf'
sudo ldconfig

In the next post, we will start with basic image processing operations.

Image Processing

Image processing is the process of manipulating image data, e.g. changing the brightness or contrast, blur, morphological operations etc., in order to make it suitable for computer vision applications or for humans itself.

Computer vision is the concept to let the computers to see as humans do. It is something beyond image processing but before starting with this, there are some basic steps need to follow:

  1. Image Acquisition
  2. Image Manipulation
  3. Obtaining Relevant Information
  4. Decision Making

As, I have already shared with you in my early post that image processing can be done using different languages and tools, OpenCV is one of them. MATLAB is the most easiest and inefficient way, similarly the Octave. The best technique to practice image processing is to use OpenCV with C++. This is the most efficient but the hardest way to process images because there is no proper documentation or error handling codes. But OpenCV has lots of inbuilt image processing functions which helps the developers to develop their applications with proper understanding about what they are doing.

Here, in next posts, I’m going to share some basic image processing tutorials with you.