Adding Trackbar

Trackbar helps the user to play with image property dynamically, when your app is running. You can change brightness, contrast with the use of trackbar on the go.

#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 std;

int main()
{
Mat img = imread("scenary2.png", CV_LOAD_IMAGE_COLOR);

int brightValue = 50;
int contrastValue = 50;
namedWindow("Image", CV_WINDOW_AUTOSIZE);

createTrackbar("brightness", "Image", &brightValue, 100);
createTrackbar("contrast", "Image", &contrastValue, 100);

while (1)
{
Mat dest;
double brightVal = brightValue - 50;
double contrastVal = contrastValue / 50.0;

img.convertTo(dest, -1, contrastVal, brightVal);

imshow("Image", dest);

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

return 0;
}

Just run the application and play with the trackbars ūüėČ

  • int createTrackbar(const string& trackbarname, const string& winname, int* value, int count, TrackbarCallback onChange = 0, void* userdata = 0)
This OpenCV function creates a trackbar and attached that trackbar to a specified window
    • trackbarname – The name of the trackbar
    • winname – The name of the window to which the trackbar is attached
    • value – This integer, pointed by this pointer, holds the value associated with the position of the trackbar
    • count – The maximum value of the trackbar. The minimum value is always zero.
    • onChange – This function will be called everytime the position of the trackbar is changed. The prototype of this function should be “FunctionName(int, void*)“. The “int” value is the value associate with the position of the trackbar. And “void*” is any pointer value which you pass as the “userdata” (See the next parameter).
    • userdata – This pointer variable will be passed as the second parameter of the above function

 

Now lets use the 5th and 6th parameter of the createTrackBar function to avoid running in infinite loop.

#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 std;

Mat img;

void CallBackBrightNess(int brightVal, void* userData)
{
Mat src;
int cvalue = *(static_cast<int*>(userData));
double cFactor = (double)cvalue / 50.0;
img.convertTo(src, -1, cFactor, brightVal - 50);
imshow("Image", src);
}

void CallBackContrast(int contrastVal, void* userData)
{
Mat src;
double cFactor = (double)contrastVal / 50.0;
img.convertTo(src, -1, contrastVal / 50.0, *(static_cast<int*>(userData)) - 50);
imshow("Image", src);
}

int main()
{
img = imread("scenary2.png", CV_LOAD_IMAGE_COLOR);

int brightValue = 50;
int contrastValue = 50;
namedWindow("Image", CV_WINDOW_AUTOSIZE);

createTrackbar("brightness", "Image", &brightValue, 100, CallBackBrightNess, &contrastValue);
createTrackbar("contrast", "Image", &contrastValue, 100, CallBackContrast, &brightValue);

imshow("Image", img);

waitKey(0);

return 0;
}

OpenCV: playing with brightness, contrast, histogram, blurness

Change Brightness of Image or Video

Changing brightness is a point operation on each pixel. If you want to increase the brightness, you have to add some constant value to each and every pixel.
new_img (i, j) = img(i, j) + cIf you want to decrease the brightness, you have to subtract some constant value from each and every pixel.
new_img (i, j) = img(i, j) – ce.g- Say, this is your original image

Original Image
Original Image

Say, you want to increase the brightness of the image by 20 units. Here is the output image of which the  brightness is increased by 20 units.

Image of which brightness is increased
Image of which brightness is increased

Say, you want to decrease the brightness of the image by 20 units. Here is the output image of which the  brightness is decreased by 20 units.

Image of which brightness is decreased
Image of which brightness is decreased

Note :
You may already notice that although the 1st pixel of the above image should have (12 Р20)  = -8, I have put 0. It is because pixels never have negative values. Any pixel value is bounded below by 0 and bounded above by 2^(bit depth).

#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 std;

int main()
{
Mat img = imread("Deepika_Padukone.jpg");
Mat imgH;
Mat imgL;
imgH = img + Scalar(100, 100, 100);
imgL = img + Scalar(-100, -100, -100);

namedWindow("Original Image", CV_WINDOW_AUTOSIZE);
namedWindow("Increased Brightness Image", CV_WINDOW_AUTOSIZE);
namedWindow("Decreased Brightness Image", CV_WINDOW_AUTOSIZE);

imshow("Original Image", img);
imshow("Increased Brightness Image", imgH);
imshow("Decreased Brightness Image", imgL);
cvWaitKey(0);

return 0;
}

Change Contrast of Image or Video

Changing the contrast is also a point operation on each pixel. The easiest way to increase the contrast of an image is, multiplying each pixel value by a number larger than 1.
                         new_img (i, j) = img(i, j) * c                     c > 1
The easiest way to decrease the contrast is, multiplying each pixel value by a number smaller than 1.
                         new_img (i, j) = img(i, j) * c                     c < 1
There are more advance methods to adjust contrast of an image such as histogram equalization. Such method adjust the contrast of an image such that color distribution is balanced equally. We will discuss the histogram equalization in the next lesson.
e.g- Say, this is your original image
Example Image
Example Image
By multiplying each pixel value by 2, you can effectively double the contrast of an image. Here is the image of which the contrast is increased.
I have considered this image as a 8 bit unsigned image. So, any pixel value should be from 0 to 255. If the resulting image has values more than 255, it should be rounded off to 255.
Contrast Increased
Contrast Increased
By multiplying each pixel value by 0.5, you can effectively halve the contrast of an image. Here is the image of which contrast is decreased.
Contrast Decreased
Contrast Decreased
#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 std;

int main()
{
Mat img = imread("Deepika_Padukone.jpg");
Mat imgH;
Mat imgL;
imgH = img * 1.2;
imgL = img *0.8;

namedWindow("Original Image", CV_WINDOW_AUTOSIZE);
namedWindow("Increased contrast Image", CV_WINDOW_AUTOSIZE);
namedWindow("Decreased contrast Image", CV_WINDOW_AUTOSIZE);

imshow("Original Image", img);
imshow("Increased contrast Image", imgH);
imshow("Decreased contrast Image", imgL);
cvWaitKey(0);
return 0;
}

Instead of imgH = img * 1.2, you can use convertTo function.

img.convertTo(imgH, -1, 1.2, 0); //increase the contrast (double)

 

  • void convertTo( OutputArray m, int rtype, double alpha=1, double beta=0 )
This OpenCV function converts  image into another format with scaling. Scaling is done according to the following formula.
                                              m[i,j] = alfa * img[i,j] + beta
Here is the parameters of this function
    • OutputArray m – Store the converted image
    • int¬†rtype – Depth of the output image. If the value of rtype is negative, output type is same as the input image. I have used a negative value in the above program because I don’t want to change the depth of the original image. Possible inputs to this parameter
      • CV_8U
      • CV_32S
      • CV_64F
      • -1
    • double¬†alpha – Multiplication factor; Every pixel will be multiplied by this value
    • double¬†beta – This value will be added to very pixels after multiplying¬†with¬†the above value.
Here is the formula again. Here m[i, j] means a pixel at ith row and jth column.
m[i,j] = alfa * img[i,j] + beta

Histogram Equalization of Grayscale or Color Image

Histogram

Histogram is the intensity distribution of an image.
E.G –
Consider the following image. Say, depth of the image is 2 bits. Therefore the value range for each and every pixel is from 0 to 3.
Sample Image (Depth = 2 bits)
Sample Image (Depth = 2 bits)
Histogram of the a image shows how the pixel values are distributed. As you can see in the above image there are 5 pixels with value 0, 7 pixels with value 1, 9 pixels with value 2 and 4 pixels with value 3. These information is tabulated as follows.
Intensity Distribution of above image
Intensity Distribution of above image
Histogram of a image usually presented as a graph. The following graph represents the histogram of the above image.
Image Histogram
Image Histogram

Histogram Equalization

Histogram Equalization is defined as equalizing the intensity distribution of an image or flattening the intensity distribution curve. Histogram equalization is used to improve the contrast of an image. The equalized histogram of the above image should be ideally like the following graph.
Equalized Histogram
Equalized Histogram
But practically, you cannot achieve this kind of perfect histogram equalization. But there are various techniques to achieve histogram equalization close to the perfect one. In OpenCV, there is an in-built OpenCV function to equalize histogram.
With uncolored image
#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 std;

int main()
{
Mat img = imread("Deepika_Padukone.jpg");
cvtColor(img, img, CV_BGR2GRAY);
Mat imgHist;
equalizeHist(img, imgHist);

namedWindow("Original Image", CV_WINDOW_AUTOSIZE);
namedWindow("Histogram Image", CV_WINDOW_AUTOSIZE);

imshow("Original Image", img);
imshow("Histogram Image", imgHist);
cvWaitKey(0);

return 0;
}


With colored image
#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 std;

int main()
{
Mat img = imread("scenary2.png", CV_LOAD_IMAGE_COLOR);

Mat imgHist, Y, Cr, Cb;
vector<Mat> channels(3);
cvtColor(img, imgHist, CV_BGR2YCrCb);

split(img, channels);

equalizeHist(channels[0], channels[0]);

merge(channels, imgHist);
namedWindow("Histogram before convert Image", CV_WINDOW_AUTOSIZE);

imshow("Histogram before convert Image", imgHist);

cvtColor(imgHist, imgHist, CV_YCrCb2BGR);

namedWindow("Original Image", CV_WINDOW_AUTOSIZE);
namedWindow("Histogram Image", CV_WINDOW_AUTOSIZE);

imshow("Original Image", img);
imshow("Histogram Image", imgHist);
cvWaitKey(0);

return 0;
}

Some of the functions to look:

  • cvtColor(img, img_hist_equalized, CV_BGR2YCrCb)
This line converts the color space of BGR in ‘img‘ to YCrCb color space and stores the resulting image in ‘img_hist_equalized‘.
In the above example, I am going to equalize the histogram of color images. In this scenario, I have to equalize the histogram of the intensity component only, not the color components. So, BGR format cannot be used because its all three planes represent color components blue, green and red. So, I have to convert the original BGR color space to YCrCb color space because its 1st plane represents the intensity of the image where as other planes represent the color components.
  • void split(const Mat& m, vector<Mat>& mv )
This function splits each channel of the ‘m‘ multi-channel array into separate channels and stores them in a vector, referenced by ‘mv‘.
Argument list
    • const¬†Mat& m – Input multi-channel array
    • ¬†vector<Mat>& mv – vector that stores the each channel of the input array
  • equalizeHist(channels[0], channels[0]);
Here we are only interested in the 1st channel (Y) because it ¬†represents the intensity information whereas other two channels (Cr and Cb) represent color components. So, we equalize the histogram of the 1st channel using OpenCV in-built function, ‘equalizeHist(..)’ and other two channels remain unchanged.
  • void merge(const vector<Mat>& mv, OutputArray dst )
This function does the reverse operation of the split function. It takes the vector of channels and create a single multi-channel array.
Argument list
    • const vector<Mat>& mv – vector that holds several channels. All channels should have same size and same depths
    • OutputArray dst – stores the destination multi-channel array
  • cvtColor(img_hist_equalized, img_hist_equalized, CV_YCrCb2BGR)
This line converts the image from YCrCb color space to BGR color space. It is essential to convert to BGR color space because ‘imshow(..)’ OpenCV function can only show images with that color space.

Filtering image: dilation, inversion, erosion

#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 std;

int main()
{
//display the original image
IplImage* img = cvLoadImage("Deepika_Padukone.jpg");
cvNamedWindow("MyWindow");
cvShowImage("MyWindow", img);
while (1)
{

IplImage* img2 = img;
int ans = waitKey(30);
if (ans == 65)
{
//erode and display the eroded image
cvErode(img, img2, 0, 2);
cvNamedWindow("Eroded");
cvShowImage("Eroded", img2);
}
else if (ans == 66)
{
//dilate and display the eroded image
cvDilate(img, img2, 0, 2);
cvNamedWindow("dilated");
cvShowImage("dilated", img2);
}
else if (ans == 67)
{
//invert and display the eroded image
cvNot(img, img2);
cvNamedWindow("inverted");
cvShowImage("inverted", img2);
}

}
cvWaitKey(0);

//cleaning up
cvDestroyWindow("MyWindow");
cvDestroyWindow("Eroded");
cvReleaseImage(&img);

return 0;
}

Press A, B, C and see the magic ūüėČ

Writing Image and Video to file

Writing image got from camera

Following program saves the image when you press ‘A’ from your keyboard.

#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) == 65)
{
vector<int> compression_params; //vector that stores the compression parameters of the image

compression_params.push_back(CV_IMWRITE_JPEG_QUALITY); //specify the compression technique

compression_params.push_back(98); //specify the compression quality

bool bSuccess = imwrite("D:/TestImage.jpg", frame, compression_params); //write the image to file
if (!bSuccess)

{

cout << "ERROR : Failed to save the image" << endl;

//system("pause"); //wait for a key press

}
break;
}
}
return 0;
}

Remember saving the image to drive other than C:, usually saving to C: Drive causes permission issue.

Writing video got 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);

namedWindow("MyVideo", CV_WINDOW_AUTOSIZE); //create a window called "MyVideo"

double dWidth = cap.get(CV_CAP_PROP_FRAME_WIDTH); //get the width of frames of the video
double dHeight = cap.get(CV_CAP_PROP_FRAME_HEIGHT); //get the height of frames of the video

cout << "Frame Size = " << dWidth << "x" << dHeight << endl;

Size frameSize(static_cast<int>(dWidth), static_cast<int>(dHeight));

VideoWriter oVideoWriter("D:/MyVideo.avi", CV_FOURCC('M', 'J', 'P', 'G'), 20, frameSize, true); //initialize the VideoWriter object
//20 is frame rate..

if (!oVideoWriter.isOpened()) //if not initialize the VideoWriter successfully, exit the program
{
cout << "ERROR: Failed to write the video" << endl;
return -1;
}

while (1)
{
Mat frame;
bool success = cap.read(frame);

if (success)
{
oVideoWriter.write(frame);
imshow("MyVideo", frame);
}
if (waitKey(10) == 27) //wait for 'esc' key press for 30ms. If 'esc' key is pressed, break loop
{
cout << "esc key is pressed by user" << endl;
break;
}
}

return 0;
}

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