Saturday, 30 January 2016

Capturing a Video from a File/Webcam

Video is a series of images displayed sequentially in quick succession.
Thus in other words we can say that a video is a continuous frame of images.Here by continuous we mean that each image frame is played in a rapid succession such that it appears continuous frames to our eyes.
(Due to persistence of vision)

Thus processing a video is analogous to processing each frame of still images.
There are two ways to process a video:
1. Load it from a file
2. Capture it from a webcam i.e real time recording of video.

Thus if we need to capture a video from a webcam we need to just replace the line of the code by
VideoCapture capture(0);
where the parameter 0 indicates that we are using the default camera for capturing the video.

Thus if attach external camera other than the one which we have with our laptop we need to give that index as our parameter.

e.g VideoCapture capture(1);

Reading from a File:


//OpenCv C++ Code for reading video from a File
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <iostream>
  using namespace std;
  using namespace cv;
 
  int main()
  {
       //Capturing the Video
       VideoCapture capture("D:\\MyVideo.avi");

       
       //Check whether video is Opening
       if (!capture.isOpened())
       throw "Error when reading file";

       namedWindow("window", 1);
       
       //Reading frames of Video
       for (;;)
     {
            Mat frame;
            capture >> frame;
            if (frame.empty())
              break;
            imshow("window", frame);
            waitKey(1);
       }
   }  


Reading from a Webcam:


#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <iostream>
  using namespace std;
  using namespace cv;
 
  int main()
  {
       //Capturing the Video
       VideoCapture capture(0);

       
       //Check whether video is Opening
       if (!capture.isOpened())
       throw "Error when reading file";

       namedWindow("window", 1);
       
       //Reading frames of Video
       for (;;)
     {
            Mat frame;
            capture >> frame;
            if (frame.empty())
              break;
            imshow("window", frame);
            waitKey(1);
       }
   }  

Monday, 25 January 2016

RGB to Other Color Space Conversion

cvtcolor() converts an image from one color space to another.



Syntax :
C++:void cvtColor(InputArray src, OutputArray dst, int code, int dstCn=0 )

Parameter:
src   :– input image: 8-bit unsigned, 16-bit unsigned ( CV_16UC... ), or single-precision floating-point.
dst    :– output image of the same size and depth as src.
code  :– color space conversion code (see the description below).
dstCn :– number of channels in the destination image; if the parameter is 0, the number of the channels is derived automatically from src and code.



As it has already been mentioned that the functions converts an image form one color space to another, there are various types of conversion possible.

Transformation Syntax
RGB to YCrCb CV_RGB2YCrCb
BGR to YCrCb CV_BGR2YCrCb
YCrCb to RGB CV_YCrCb2RGB
YCrCb to BGR CV_YCrCb2BGR

Transformation Syntax
RGB to HSV CV_RGB2HSV
BGR to HSV CV_BGR2HSV
HSV to RGB CV_HSV2RGB
HSV to BGR CV_HSV2BGR

Transformation Syntax
RGB to CIE L*a*b* CV_RGB2Lab
BGR to CIE L*a*b* CV_BGR2Lab
CIE L*a*b* to RGB CV_Lab2RGB
CIE L*a*b* to BGR CV_Lab2BGR

Transformation Syntax
RGB to CIE L*u*v* CV_RGB2Luv
BGR to CIE L*u*v* CV_BGR2Luv
CIE L*u*v* to RGB CV_Luv2RGB
CIE L*u*v* to BGR CV_Luv2BGR

Transformation Syntax
RGB to CIE XYZ CV_RGB2XYZ
BGR to CIE XYZ CV_BGR2XYZ
CIE XYZ to RGB CV_XYZ2RGB
CIE XYZ to BGR CV_XYZ2BGR


//OpenCV C++ Code for ColorSpace Conversion
#include "opencv2/core/core.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "iostream"
 
using namespace cv;
 
int main( )
{
// char* imageName = argv[1];
 
 Mat image;
 image = imread( "C:\\Users\\arjun\\Desktop\\color-image.png", 1 );
 
 Mat RGB2GRAY_image;
 cvtColor( image, RGB2GRAY_image, CV_RGB2GRAY );
 
 Mat BGR2GRAY_image;
 cvtColor( image, BGR2GRAY_image, CV_BGR2GRAY );
 
 Mat RGB2YCrCb_image;
 cvtColor( image, RGB2YCrCb_image, CV_RGB2YCrCb );
 
 Mat BGR2YCrCb_image;
 cvtColor( image, BGR2YCrCb_image, CV_BGR2YCrCb );
 
 Mat RGB2HSV_image;
 cvtColor( image, RGB2HSV_image, CV_RGB2HSV );
 
 Mat BGR2HSV_image;
 cvtColor( image, BGR2HSV_image, CV_BGR2HSV );
 
 Mat RGB2Lab_image;
 cvtColor( image, RGB2Lab_image, CV_RGB2Lab );
 
 Mat BGR2Lab_image;
 cvtColor( image, BGR2Lab_image, CV_BGR2Lab );
 
 Mat RGB2Luv_image;
 cvtColor( image, RGB2Luv_image, CV_RGB2Luv );
 
  Mat BGR2Luv_image;
 cvtColor( image, BGR2Luv_image, CV_BGR2Luv );
 
 Mat RGB2XYZ_image;
 cvtColor( image, RGB2XYZ_image, CV_RGB2XYZ );
 
 Mat BGR2XYZ_image;
 cvtColor( image, BGR2XYZ_image, CV_BGR2XYZ );
 
 namedWindow( "original image", CV_WINDOW_AUTOSIZE );
imshow( "original image", image );
 
namedWindow( "RGB2GRAY image", CV_WINDOW_AUTOSIZE );
imshow( "RGB2GRAY image",RGB2GRAY_image );
imwrite( "C:\\Users\\arjun\\Desktop\\RGB2GRAY.jpg", RGB2GRAY_image );
 
namedWindow( "BGR2GRAY image", CV_WINDOW_AUTOSIZE );
imshow( "BGR2GRAY image", BGR2GRAY_image );
imwrite( "C:\\Users\\arjun\\Desktop\\BGR2GRAY.jpg", BGR2GRAY_image );
 
namedWindow( "RGB2YCrCb image", CV_WINDOW_AUTOSIZE );
imshow( "RGB2YCrCb image", RGB2YCrCb_image );
imwrite( "C:\\Users\\arjun\\Desktop\\RGB2YCrCb.jpg", RGB2YCrCb_image );
 
 
namedWindow( "BGR2YCrCb image", CV_WINDOW_AUTOSIZE );
imshow( "BGR2YCrCb image", BGR2YCrCb_image );
imwrite( "C:\\Users\\arjun\\Desktop\\BGR2YCrCb.jpg", BGR2YCrCb_image );
 
namedWindow( "RGB2HSV image", CV_WINDOW_AUTOSIZE );
imshow( "RGB2HSV image", RGB2HSV_image );
imwrite( "C:\\Users\\arjun\\Desktop\\RGB2HSV.jpg", RGB2HSV_image );
 
namedWindow( "BGR2HSV image", CV_WINDOW_AUTOSIZE );
imshow( "BGR2HSV image", BGR2HSV_image );
imwrite( "C:\\Users\\arjun\\Desktop\\BGR2HSV.jpg", BGR2HSV_image );
 
namedWindow( "RGB2Lab image", CV_WINDOW_AUTOSIZE );
imshow( "RGB2Lab image", RGB2Lab_image );
imwrite( "C:\\Users\\arjun\\Desktop\\RGB2Lab.jpg", RGB2Lab_image );
 
namedWindow( "BGR2Lab image", CV_WINDOW_AUTOSIZE );
imshow( "BGR2Lab image", BGR2Lab_image );
imwrite( "C:\\Users\\arjun\\Desktop\\BGR2Lab.jpg", BGR2Lab_image );
 
namedWindow( "RGB2Luv image", CV_WINDOW_AUTOSIZE );
imshow( "RGB2Luv image", RGB2Luv_image );
imwrite( "C:\\Users\\arjun\\Desktop\\RGB2Luv.jpg", RGB2Luv_image );
 
namedWindow( "BGR2Luv image", CV_WINDOW_AUTOSIZE );
imshow( "BGR2Luv image", BGR2Luv_image );
imwrite( "C:\\Users\\arjun\\Desktop\\BGR2Luv.jpg", BGR2Luv_image );
 
namedWindow( "RGB2XYZ image", CV_WINDOW_AUTOSIZE );
imshow( "RGB2XYZ image", RGB2XYZ_image );
imwrite( "C:\\Users\\arjun\\Desktop\\RGB2XYZ.jpg", RGB2XYZ_image );
 
namedWindow( "BGR2XYZ image", CV_WINDOW_AUTOSIZE );
imshow( "BGR2XYZ image", BGR2XYZ_image );
imwrite( "C:\\Users\\arjun\\Desktop\\BGR2XYZ.jpg", BGR2XYZ_image );
 
 waitKey(0);
 
 return 0;
}


Original Image:



RGB to Grey Image:



BGR to Grey Image:



RGB to YCrCb Image:



BGR to YCrCb Image:



RGB to HSV Image:



BGR to HSV Image:



RGB to Lab Image:



BGR to Lab Image:



RGB to Luv Image:



BGR to Luv Image:



RGB to XYZ Image:



BGR to XYZ Image:



Wednesday, 20 January 2016

RGB to GrayScale Conversion

In OpenCV we can convert an RGB image into Grayscale by two ways:
1.  By using cvtColor function.

2.  By using imread function, where the first parameter specifies the image name while the second parameter specifies the format in which we need to add the image.
Thus there can be various formats like:
a. CV_LOAD_IMAGE_UNCHANGED (<0) :loads the image as is (including the alpha channel if present).
b. CV_LOAD_IMAGE_GRAYSCALE ( 0) :loads the image as an intensity one.
c. CV_LOAD_IMAGE_COLOR (>0) :loads the image in the RGB format.



//Opencv C++ Code for RGB to GreyScale Conversion
#include "opencv2/core/core.hpp"  
#include "opencv2/highgui/highgui.hpp"  
#include "iostream"  
 
  using namespace std;  
  using namespace cv;  
 
  int main() 
  {  
   Mat image;  
   //Reading the color image  
   image = imread("C:\\Users\\arjun\\Desktop\\image003.png", CV_LOAD_IMAGE_COLOR); 
 
   //If image not found  
   if (!image.data) 
     {  
      cout << "No image data \n";  
      return -1;  
     }  
 
   //Converting the Image into GrayScale and Storing it in a Matrix 'img_gray'
   Mat img_gray;
   img_gray = imread("C:\\Users\\arjun\\Desktop\\image003.png",CV_LOAD_IMAGE_GRAYSCALE);
    
    
   //Display the original image  
   namedWindow("Display Original Image",CV_WINDOW_AUTOSIZE);  
   imshow("Display Original Image", image);  
 
   //Display the grayscale image 
   namedWindow("Display Grayscale Image",CV_WINDOW_AUTOSIZE); 
   imshow("Display Grayscale Image", img_gray);  
 
   //Save the grayscale image with a name 'gray.jpg'
   imwrite("C:\\Users\\arjun\\Desktop\\gray.jpg",img_gray);
 
   waitKey(0);  
   return 0;  
  } 


Input:



Output:


Friday, 15 January 2016

Reason for increase in the size of the image

In the previous tutorial we learnt how to save images in OpenCV, Refer:
http://opencv-code.blogspot.in/2016/12/how-to-write-and-dispay-image-in-opencv.html

This tutorial explains the reason behind increase in size of image in OpenCV.



Why does the size of the image increases when i save the same image again with a different name in OpenCV?
Solution:
There exists various methods of compression in JPEG.
So your OpenCV used a different compression technique for jpeg image than that used by an original image.
By Default OPENCV uses the compression number of 95 while dealing with JPEG images.
The higher the number , lesser compression we would obtain.


To manually pass the compression factor refer the code below:
Note: Here i have used the compression number of 25 and 100 as the compression number for JPEG image
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <iostream>
#include <stdio.h>
using namespace cv;
using namespace std;
 
 
int main(int argc, char **argv)
{
     Mat image1;
     const int JPEG_QUALITY = 25;
     const int JPEG_QUALITY2 = 25;

    // Read the file
    image1 = imread("C:\\Users\\arjun\\Desktop\\opencv-logo.jpg",CV_LOAD_IMAGE_COLOR);

    // Check for invalid input
    if(! image1.data )                              
    {
        cout << "Could not open or find the image" << std::endl ;
        return -1;
    } 

    vector<int> params;
    params.push_back(CV_IMWRITE_JPEG_QUALITY);
    params.push_back(JPEG_QUALITY);

    //Write the File
    imwrite( "C:\\Users\\arjun\\Desktop\\opencvlogo-new1.jpg",image1, params);

    vector<int> params2;
    params2.push_back(CV_IMWRITE_JPEG_QUALITY);
    params2.push_back(JPEG_QUALITY2);

    //Write the File
    imwrite( "C:\\Users\\arjun\\Desktop\\opencvlogo-new2.jpg",image1, params2);
  
    //Dispay the Image in the window 
    namedWindow("Image1");
    imshow("Image1",image1);
 
 waitKey(0);
}

Note:- Now compare the size of the original image and the two new images.

Sunday, 10 January 2016

Write and Dispay Image in OpenCV

In my previous tutorial i have explained how to read images in OpenCV.
Refer : http://opencv-code.blogspot.in/2016/12/how-to-read-and-display-image-in-opencv.html

This tutorial is about how to read, write and display images in Opencv.



Syntax:
C++:bool imwrite(const string& filename, InputArray img, const vector<int>& params=vector<int>() )
Saves an image to a specified file.

Parameters:           
filename Name of file to be loaded.
image Image to be saved.
params –
CV_LOAD_IMAGE_ANYDEPTH - If set, return 16-bit/32-bit image when the input has the corresponding depth, otherwise convert it to 8-bit.
CV_LOAD_IMAGE_COLOR - If set, always convert image to the color one
CV_LOAD_IMAGE_GRAYSCALE - If set, always convert image to the grayscale one

>0 Return a 3-channel color image.
=0 Return a grayscale image.
<0 Return the loaded image as is (with alpha channel).




//OpenCV C++ code for reading, writing and displaying image
#include "opencv2/core/core.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "iostream"
 
using namespace cv;
using namespace std;
 
int main( int argc, char** argv )
{
 Mat image1,image2;
 
// Read the file
image1 = imread("C:\\Users\\arjun\\Desktop\\opencv-logo.jpg",CV_LOAD_IMAGE_COLOR);

// Check for invalid input
 if(! image1.data )                              
    {
        cout << "Could not open or find the image" << std::endl ;
        return -1;
    } 
 
//Write the File
imwrite( "C:\\Users\\arjun\\Desktop\\opencv-logo-new.jpg",image1);
 
// Read the Writen File
image2 =imread("C:\\Users\\arjun\\Desktop\\opencv-logo-new.jpg",CV_LOAD_IMAGE_COLOR);  
    
//Window for displaying both the images
 namedWindow("Image1");
 imshow("Image1",image1);
 
 namedWindow("Image2");
 imshow("Image2",image2);

 waitKey(0);
 
}
Note:- Compare the size of the two images.(opencv-logo.jpg & opencv-logo-new.jpg).
To know the reason about the increase in size of the image refer:
http://opencv-code.blogspot.in/2016/12/why-does-size-of-images-increases-opencv-imwrite.html

Read and Display Image in OpenCV

Syntax:
C++: Mat imread(const string& filename, int flags=1 )

Parameters:           
filename Name of file to be loaded.
flags Flags specifying the color type of a loaded image:-

CV_LOAD_IMAGE_ANYDEPTH - If set, return 16-bit/32-bit image when the input has the corresponding depth, otherwise convert it to 8-bit.
CV_LOAD_IMAGE_COLOR - If set, always convert image to the color one
CV_LOAD_IMAGE_GRAYSCALE - If set, always convert image to the grayscale one

>0 Return a 3-channel color image.
=0 Return a grayscale image.
<0 Return the loaded image as is (with alpha channel).




//OpenCv C++ Example of Reading Image
#include "opencv2/core/core.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "iostream"
 
using namespace cv;
using namespace std;
 
int main( int argc, char** argv )
{
 Mat image1;
 
// Read the file
image1 = imread("C:\\Users\\arjun\\Desktop\\opencv-logo.jpg",CV_LOAD_IMAGE_COLOR);
// Check for invalid input
 if(! image1.data )                              
    {
        cout << "Could not open or find the image" << std::endl ;
        return -1;
    } 
 
//Dispay the Image in the window 
namedWindow("Image1");
 imshow("Image1",image1);

 waitKey(0);
}

Note the Location of the image is:
C:\Users\arjun\Desktop\a.jpg where "opencv-logo.jpg" .
( Here we append additional "\" after each file or folder name.)


Image Formats supported by OpenCV:
Windows bitmaps *.bmp, *.dib
JPEG files *.jpeg, *.jpg, *.jpe
JPEG 2000 files *.jp2
Portable Network Graphics *.png
Portable image format *.pbm, *.pgm, *.ppm
Sun rasters *.sr, *.ras
TIFF files *.tiff, *.tif

Tuesday, 5 January 2016

OpenCV vs Matlab vs Scilab vs Aforge

OpenCV stands for Open Source Computer Vision. It contains a library of programming functions for real time computer vision applications.Originally developed by Intel and now supported by Willow Garage.

MATLAB, short for MATrix LABoratory is a high level language and programming package specifically designed for quick and easy scientific calculations and I/O.

Scilab is a freely distributed open source scientific software package, released as open source under the CeCILL license (GPL compatible) firstly developed by researchers from INRIA and ENPC, and now by the Scilab Consortium. It is similar to that of Matlab.

AForge.NET is an open source C# framework designed for developers and researchers in the fields of Computer Vision and Artificial Intelligence - image processing, neural networks, genetic algorithms, fuzzy logic, machine learning, robotics, etc.

Out of these OpenCv and Matlab are the most widely used tools for image processing.

Advantages of OpenCv over Matlab are:
  1. Cost:
  2. OpenCv is OpenSource (released under BSD license) thus it doesn’t require any license to buy it.
    While Matlab (commercial single user) costs around USD $2150.
  3. Speed:
  4. OpenCv is built on C/C++
    While matlab is built on Java.
    Thus while compiling the code written in Matlab , your computer is busy trying to interpret the code of Matlab then turn it into Java and then to C/C++.
    Hence programs written in OpenCv tends to run faster than that compared to similar program written in Matlab.
    In Computer vision we are normally dealing with real time applications, speed is a major concern in such cases.
  5. Resources needed:
  6. Due to the high level nature of Matlab it occupies a lot of your computer resources(about 1 GB of RAM) as compared to that of OpenCv(about 70MB of RAM) for real time computer vision applications.
  7. Portability:
  8. Almost any device which can run C can run OpenCv programs.
Inspite of all these amazing features OpenCv loses on Matlab on the below mentioned feature:

Advantages of Matlab over OpenCv are:
  1. Ease of Access:
  2.  Matlab is a pretty high-level scripting language, meaning that you don’t have to worry about libraries, declaring variables, memory management or other lower-level programming issues. As such, it can be very easy to throw together some code to prototype your image processing idea.

    Say for example I want to read in an image from file and display it. In Matlab, you could write this as:
    I = imread('someImage.jpg');
    imshow(I);
    

    This seems to be quite easy.The same thing when it is done with OpenCv would look like this:
    //OpenCv C++ Example of Reading Image
    #include "opencv2/core/core.hpp"
    #include "opencv2/highgui/highgui.hpp"
    #include "iostream"
     
    using namespace cv;
    using namespace std;
     
    int main( int argc, char** argv )
    {
     Mat image1;
     
    // Read the file
    image1 = imread("C:\\Users\\arjun\\Desktop\\opencv-logo.jpg",CV_LOAD_IMAGE_COLOR);
    // Check for invalid input
     if(! image1.data )                              
        {
            cout << "Could not open or find the image" << std::endl ;
            return -1;
        } 
     
     namedWindow("Image1");
     imshow("Image1",image1);
    
     waitKey(0);
    }
    
  3. Memory Management:
  4. OpenCV is based on C. As such, every time you allocate a chunk of memory you will have to release it again. If you have a loop in your code where you allocate a chunk of memory in that loop and forget release it afterwards, you will get what is called a “leak”. This is where the program will use a growing amount of memory until it crashes from no remaining memory. Due to the high-level nature of Matlab, it is “smart” enough to automatically allocate and release memory in the background.

Sunday, 3 January 2016

Introduction to OpenCV

OpenCV stands for Open Source Computer Vision. It contains a library of programming functions for real time computer vision applications.Originally developed by Intel and now supported by Willow Garage.


It has C++, C, Python and Java interfaces and supports Windows, Linux, Mac OS, iOS and Android.

Goal : To provide a simple-to-use computer vision infrastructure that helps people build fairly sophisticated vision applications quickly.

Since its alpha release in January 1999, OpenCV has been used in many applications,products, and research efforts. These applications include stitching images together in satellite and web maps, medical image noise reduction, objectanalysis, security and intrusion detection systems, automatic monitoring and safety systems etc.

For more information refer : http://opencv.org/

Friday, 1 January 2016

Introduction to ImageProcessing

Image can be defined as a 2-dimensional representation of a 3-dimensional world.
This 2- dimensional representation actually contains an array, or a matrix, of square pixels (picture elements) arranged in columns and rows.

While  Image processing is  a method to perform some operations on an image(this matix of pixels), in order to get an enhanced image or to extract some useful information from it.

In other words we can say Image Processing means analyzing and processing image data in order to make it suitable for computer vision or in order to represent it effectively to the humans.

How do we Humans understand Images?
1. Whenever we look at a scene,our eye records the scene and sends it to the brain.
2. Our brain then analyses the information sent by the eyes and derives a meaningful inference from it.

In the similar way we can train the computers to derive information from a particular scene. This process of making the  computers see the images as we humans do is known a computer vision.
Thus , Computer vision deals with acquiring, analyzing, processing and understanding the digital images and making decision based on that information.

Various steps which are involved in Computer Vision / ImageProcessing are:
1 .     Image Acquisition.
2 .     Image Analysation
3 .     Image Manipulation
4 .     Obtaining relevant data
5 .     Decision making

There are various tools available for ImageProcessing/Computer Vision, among which some of the most widely used tools are Matlab, OpenCv and Aforge.