OpenCV: Displaying video files from media/camera

Displaying video from media

#include "stdafx.h"
#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"
#include "opencv2/core/core.hpp"

using namespace cv;
#include <iostream>

using namespace cv;
using namespace std;

int main( int argc, const char** argv )
{
VideoCapture cap("C:/Users/shubham/Downloads/android/coursera/vedam.MP4"); // opens the video file

if (!cap.isOpened()) // if not success, exit program
{
cout << "Cannot open the video file" << endl;
return -1;
}

double frame = cap.get(CAP_PROP_POS_FRAMES);
double fmsRate = cap.get(CAP_PROP_FPS);

namedWindow("myVideo", CV_WINDOW_AUTOSIZE);

while (1)
{
Mat frame;
if (cap.read(frame))
{
imshow("myVideo", frame);
}
else
{
cout << "error reading frames \n";
}

if (waitKey(fmsRate) == 27)
{
break;
}
}
return 0;
}

Displaying video from camera

 

#include "stdafx.h"
#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"
#include "opencv2/core/core.hpp"

using namespace cv;
#include <iostream>

using namespace cv;
using namespace std;

int main( int argc, const char** argv )
{
VideoCapture cap(0); // opens the video camera 0

if (!cap.isOpened()) // if not success, exit program
{
cout << "Cannot open the video file" << endl;
return -1;
}

double frame = cap.get(CAP_PROP_POS_FRAMES);

//playing with other arguments of namedWindow to see the effect.

namedWindow("myVideo", CV_WINDOW_NORMAL);

while (1)
{
Mat frame;
if (cap.read(frame))
{
imshow("myVideo", frame);
}
else
{
cout << "error reading frames \n";
}

if (waitKey(30) == 27)
{
break;
}
}
return 0;
}

OpenCV: Lets discuss our first code

#include "stdafx.h"
#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"
#include "opencv2/core/core.hpp"

using namespace cv;
#include <iostream>

using namespace cv;
using namespace std;

int main( int argc, const char** argv )
{
Mat img = imread("Deepika_Padukone.jpg", CV_LOAD_IMAGE_UNCHANGED); //read the image data in the file "MyPic.JPG" and store it in 'img'

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;
}

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

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

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

return 0;
}

Above is the same code which we copied while setting up OpenCV and visual studio.

  • Mat img = imread(const string& filename, int flags=CV_LOAD_IMAGE_COLOR)
Mat is a data structure to store images in a matrix. It is declared in “opencv2/core/core.hpp” header file.

 

imread() is a function declared in “opencv2/highgui/highgui.hpp” header file. It loads an image from a file and stores it in Mat data structure.

 

Arguments of imread() function
  • filename – location of the file. If you just give the filename only, that image should be in the same folder as your C++ file. Otherwise you have to give the full path to your image.
  • flags – There are four possible inputs
    • CV_LOAD_IMAGE_UNCHANGED – image-depth=8 bits per pixel in each channel,  no. of channels=unchanged
    • CV_LOAD_IMAGE_GRAYSCALE – image depth=8 bits,  no. of channels=1
    • CV_LOAD_IMAGE_COLOR – image-depth=?,  no. of channels=3
    • CV_LOAD_IMAGE_ANYDEPTH – image-depth=unchanged ,  no. of channels=?
    • CV_LOAD_IMAGE_ANYCOLOR – image-depth=?,  no. of channels=unchanged
You can combine these above parameters to get desired image output.
e.g –
CV_LOAD_IMAGE_ANYDEPTH | CV_LOAD_IMAGE_ANYCOLOR – image-depth=unchanged,  no. of channels=unchanged
CV_LOAD_IMAGE_COLOR | CV_LOAD_IMAGE_ANYDEPTH – image-depth=unchanged,  no. of channels=3
If you are not sure what to do, use CV_LOAD_IMAGE_COLOR as the 2nd parameter of imread() function.
  • if (img.empty())
If imread() function fails to load the image, ‘img’ will not be loaded any data. Therefore ‘img.empty()‘ should return true. It’s a good practice to check whether the image is loaded successfully and if not exit the program. Otherwise your program will crash when executing imshow()function.
  • bool Mat::empty()
This function returns true, if Mat::data==NULL or Mat::total() == 0
  •  //system(“pause”);
If you are using Visual Studio, it’s better to uncomment this line because it will pause the program until user press any key. If we don’t uncomment it, the program will exit immediately so that user will not see the error message.
  • void namedWindow(const string& winname, int flags = WINDOW_AUTOSIZE);
This function creates a window.
Parameters –
    • winname – Title of the window. That name will display in the title bar of the newly created window
    • flags – determine the size of the window. There are two options
      • WINDOW_AUTOSIZE – User cannot resize the image. Image will be displayed in its original size
      • CV_WINDOW_NORMAL – Image will resized if you resize the the window
  • void imshow(const string& winname, InputArray mat);
This function shows the image which is stored in the ‘mat’ in a window specified by winname. If the window is created withWINDOW_AUTOSIZE flag, image will be displayed in its original size. Otherwise image may be scaled to the size of the window.
Parameters –
    • winname – Title of the window. This name is used to identify the window created by namedWindow() function.
    • mat – hold the image data
  • int waitKey(int delay = 0)
waitKey() function wait for keypress for certain time, specified by delay (in milliseconds). If delay is zero or negative, it will wait for infinite time. If any key is pressed, this function returns the ASCII value of the key and your program will continue. If there is no key press for the specified time, it will return -1 and program will continue.
  • void destroyWindow(const string& winname)
This function closes the opened window, with the title of winname and deallocate any associated memory usage. This function is not essential for this application because when the program exits, operating system usually close all the opened windows and deallocate any associated memory usage.
Summary
When running this program, the image of ‘Deepika_Padukone.jpg‘ is loaded into the variable, ‘img’ of type Mat. Then a window named ‘MyWindow‘ is opened. After that  ‘img‘ is loaded to that window. The window with the image will be displayed until any key is pressed.

OpenCV: Tad bit of Image Processing

While learning computer vision, now and then we will encounter two terms, image-depth and channel. So lets talk about them.

 Image-depth:  It means number of bits allocated for each pixel.

Grayscale Image with image depth of 8Grayscale Image with image depth of 8

In the above image, depth is 8 because each pixel is within value 0, 255, i.e. between 8 bits.

Channel: Pixel value between 0 and 255 can represent only black and white image. So how can we make an image colorful?

As we all know, all colors are formed by R, G, B components. So if we put three image planes each representing corresponding color vector(Red, Green, Blue) we can get the colorful image. Here each vector is a channel.

Here is some properties of the following image.
    •  Image-depth 24 bit(8 bit X 3 channel)
    • 3 channels ( So, this is a color image )
    • The height is 4 pixel
    • The width is 5 pixels
    • The resolution of this image is 4×5.

 

Red, Green, Blue planes of a color image
R, G, B planes of a color image

 

Basics of OpenCV API

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.

OpenCV: Open Source Computer Vision

OpenCV (Open Source Computer Vision) is a library of programming functions mainly aimed at real-time computer vision.

OpenCV is released under a BSD license and hence it’s free for both academic and commercial use. It has C++, C, Python and Java interfaces and supports Windows, Linux, Mac OS, iOS and Android. OpenCV was designed for computational efficiency and with a strong focus on real-time applications. Written in optimized C/C++, the library can take advantage of multi-core processing.

This post will describe setting up openCV on your system and its integration with visual studio. If you want opencv’s integration with other tools like android studio, then this post is not for you.

  1. Download openCV
  2. Run the exe and extract the file in directory say C:\
  3. Download visual studio. For me vs 2015 professional worked well. Download it from here.
  4. Now you need to change some global system parameters. This post describes it very well. Just take care of the fact that in linker’s additional dependency don’t blindly copy what was written in the post, but go to the folder “C:\opencv\build\x86\vc11\lib”(may vary depending upon your opencv version) and actually copy the name of the libs.
  5. After setting up your openCV and visual studio, download some sample opencv files and play with it. Just to begin with copy the following code in your consoleApplication.cpp file.
    Remember to add Deepika_Padukone.jpg image in your project
#include "stdafx.h"
#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"
#include "opencv2/core/core.hpp"

using namespace cv;
#include <iostream>

using namespace cv;
using namespace std;

int main( int argc, const char** argv )
{
Mat img = imread("Deepika_Padukone.jpg", CV_LOAD_IMAGE_UNCHANGED); //read the image data in the file "MyPic.JPG" and store it in 'img'

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;
}

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

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

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

return 0;
}

FileZilla: Transfer files from your local system to server

Here I will cover setting up fileZilla which helps a lot in transferring files to and from server. FileZilla is open source software distributed free of charge under the terms of the GNU General Public License

1. Download FileZilla client.

2. Open fileZilla and go to site manager.

3. Enter Host, Login details which you can find in your hosting provider. Enter the details and connect.

4. Now you can see your web files listed in the directory. To transfer files all you need to do is to drag and drop the files.

Setting up your website

This post covers some tools which you will need to setup your website.

  1. Get a domain for your website. For me i wanted to start my blog so I bought shubhamagrawal.com from godaddy. There are lot of domain providers on the web, you can go and buy your domain from any provider. It should not cost you more than Rs 500 INR unless the domain which you want to buy is already bought and the other person wants more money to give that domain to you.
  2. Buy a hosting plan from hosting provider. Again there are many provider for hosting too, almost all domain providers are hosting providers too. Go to any provider’s portal and buy a hosting plan which suits your needs the most. Try to buy this plan for at least a year if at all you wanted to develop something.
  3. Now go to the provider’s portal and in the web hosting section add your website. Now you are good to go, type your domain name on the your favorite web browser and you can see the default web page provided by the hosting provider.