OpenCV as you all know is an open-source Computer Vision library for C, C++ and also Python. This post will guide you for how to use and how to implement any problem on OpenCV. First of all an apology for not writing any post till now. From last week I am thinking about the topic to write for the blog and I came up with this. Choose any one from the given two Integrated Development Environments(IDEs) its needed for developing a project then even executable file at the end and it also helps a lot in debugging.
Eclipse IDE:
As we are dealing with C and C++ we need an environment for developing the program so we use Eclipse (here). Precisely Eclipse's CDT plugin. You need to download it from here. Its around 100 MBs.Visual Studio:
For detailed instructions on configuring OpenCV on Visual Studio and Eclipse Please visit OpenCV on VS and OpenCV on Eclipse respectively.Basics:
Basics of OpenCV also starts with reading an image and showing it in a window. Well its not as simple as it was in Matlab. So before reading an image lets start with the basic structure of OpenCV C language program.#include "stdafx.h" //if you are using Visual Studio
#include <cv.h>
int main()
{
declaration of variables;
\...
program;
\...
return 0;
}
For C++:
#include <cv.h>using namespace cv;int main()
{
\...
return 0;
}
For Python:
from cv2.cv import *
\...
\...
The function in OpenCV header file cv.h
for reading an image is cvLoadImage()
in C and imread()
in C++. From that you may get an idea C++ is better to code in rather than C (later you will think its better to code in Python* rather than C++) Following is the code for reading an image and displaying on a window named “Display”
#include<cv.h>
#include<highgui.h>
int main()
{
IplImage* img1 = cvLoadImage("C:\image\path", CV_LOAD_IMAGE_COLOR);
cvNamedWindow("Display", CV_WINDOW_AUTOSIZE);
cvShowImage("Display",img1);
cvWaitKey(0);
cvReleaseImage(&img1);
return 0;
}
Explanation
Starting from the beginning “The Header Files”
cv.h
contains all the necessary functions like reading an image filtering creating shapes, etc.- Header file
highgui.h
on the other hand contains functions for Graphical User Interfaces like showing image in window having adjustment bars, etc.
Now “The Code”
- The first part like
int main()
* which says main function starts now. - Next is quite difficult to understand its the structure that is used to store images i.e.
IplImage* img1
assigns the starting address of memory toimg1
. img1 = cvLoadImage("Path to Image",CV_LOAD_IMAGE_COLOR)
stores the image defined in path to address pointed by img1 and the macroCV_LOAD_IMAGE_COLOR
is for specifying that the fetched image should be in form of "color" not "gray-scaled" if we write GRAYSCALE in place of COLOR it will read/load image in form of gray-scaled image.- Now we use
highgui.h
for creating a window for showing the image usingcvNamedWindow("WindowName",size)
here again macro has been used namedCV_WIDOW_AUTOSIZE
which resizes the window with its content. - Now as we defined the window we need to add the content in it using the function
cvShowImage("WindowName",image-var)
- That's it the image will be displayed now to hold it to view and not to just 'return 0' and exit we need to add one more line as
cvWaitKey(time)
this will wait. - We know that we need to clear the memory been used by image variable so we use
cvReleaseImage()
function to release the image memory.
Well that ended the C part now almost the same remains in both C++ and Python. The codes are given with comments in the and they are self-explanatory .
For C++:
#include <cv.h>
#include <highgui.h>
using namespace cv;
int main()
{
Mat image; //Image is in form of matrix (here)
image = imread("/home/darshil/images/hello.jpg", 1 );
if(!image.data ) //Check if image has data
{
printf( "No image data \n" );
return -1;
}
namedWindow( "Display Image", CV_WINDOW_AUTOSIZE );
imshow( "Display Image", image );
waitKey(0);
return 0;
}
For Python:
from cv2.cv import * # importing the opencv module
img=LoadImageM("path/to/image.jpg") # loading image in form of matrix
NamedWindow("Display",CV_WINDOW_AUTOSIZE) # Creating a window
ShowImage("Display",img) #Displaying the image
WaitKey(0) #waitkey to wait till the output comes
This is it.
End of Post.