Code For Free
  • Home
  • Robotics
    • Microcontrollers >
      • Essentials
      • About ATmega16
    • Optical Encoders
    • Servo Motors
  • Stellaris
    • Introduction to Stellaris LaunchPad
    • Tutorial 1 : Blink
    • Tutorial 2 : Using Button Switch
    • Tutorial 3 : Using a locked pin for GPIO usage
    • Tutorial 4 : Serial Communication using Stellaris LaunchPad
  • OpenCV
    • Tutorial 1 : Open and Display Image
    • Tutorial 2 : Streaming video from Camera
    • Tutorial 3 : Image Filtering Techniques
  • Android Tutorials
    • App Development >
      • Installing android IDE
      • My First App
      • Running your app
    • Rooting
  • MatLab
  • About us

Opening and Displaying Images using OpenCV

This is like the hello world program in OpenCV. When we talk about image processing, the basic necessity is an image, how to import it to our program and manipulate it. This is why i call this tutorial the Hello World program of OpenCV. In this tutorial I will be explaining the old C implementation using the old Intel IPP compatible image format called IplImage and also the latest class based implementation of the same using the OpenCV2. Basically, for a beginner the old version will be easier than the new version. I found the template implementations in classes a little confusing when i migrated to the new version. 
The left hand side of the page will show you how to implement the code in C whereas the right side pane will illustrate how to do it using C++(class) implementation. So lets Get Started. I would like to take a different approach here. I strongly suggest that the learner go through the full code before going to the line by line explanation. As far as I have understood from my little experience, this will help you understand the concepts more clearly. Some of you may not even need any explanation at all.

C implementation

I assume that you have an image file named image.jpg in the same folder as your C code.
#include <cv.h>
#include <highgui.h>

int main()
{
IplImage *im = cvLoadImage("image.jpg",CV_WINDOW_AUTOSIZE);
cvNamedWindow("image");
cvShowImage("image", im);
cvWaitKey(0);
cvReleaseImage(&im);
cvDestroyAllWindows();
}

Explanation

IplImage *im = cvLoadImage("image.jpg",CV_WINDOW_AUTOSIZE);
IplImage is basically a structure which contains the image size, number of channels, a pointer to the image data etc as the structure elements. As this structure only contains a pointer and not the data as such, it is called IPL image header. An IplImage was inherited from the Intel Image Processing Library(IPL). For more information you may visit : IplImage

cvLoadImage(const char* name, int color=CV_LOAD_IMAGE_COLOR)

can be used to load an image from the hard drive to your IplImage. 
The First parameter is the filename. Here you must specify the file path if the file is not in the working directory. 

The second parameter is the Image color. Here you can specify how you want to import the image. The various possible values are :
  • CV_LOAD_IMAGE_COLOR the loaded image is forced to be a 3-channel color image
  • CV_LOAD_IMAGE_GRAYSCALE the loaded image is forced to be grayscale
  • CV_LOAD_IMAGE_UNCHANGED the loaded image will be loaded as is.

For details you may visit : cvLoadImage
cvNamedWindow("image");
cvShowImage("image", im);
cvNamedWindow(const char* name,int flag)  is used to create window to show our image. This is not a necessary step. But you may sometimes encounter some logical errors if you dont create a window explicitly. To keep the explanation simple I am omitting the flags parameters because we never use that in real life coding.
To read more : cvNamedWindow 
cvShowImage(const char* name, IplImage* im) is used to display image in the wiindow we created right now. This means that the first parameter must be same ass the one we gave in cvNamedWindow. The Second parameter is a pointer to the image, which in our case is an IplImage. 
To read more :cvShowImage
cvWaitKey(0);
cvReleaseImage(&im);
cvDestroyAllWindows();
cvWaitKey(int milliseconds) is used to wait for a keypress. The parameter is the number of milliseconds to wait. If it is zero, it means that the excecution must be paused until a keypress. Otherwise, the program will wait for the mentioned milliseconds for a keypress and continue the excecution if no keypress is detected. But if a keypress is detected, the function will return the ASCII code corresponding to the key pressed.
To read more :cvWaitKey
cvReleaseImage  is used to avoid memory leaks. Once we create an IplImage and assign an image to it in C, we must release it when the use is over. This is one drawback of the C version of OpenCV. 
cvDestroyAllWindows() is used to destroy all the windows that we created in our program. There is another function called cvDestroyWindow which allows us to induvidually destroy each window.
To read more : cvDestroyAllWindow

C++ implementation

I assume that you have an image file named image.jpg in the same folder as your C++ code.
#include <cv.h>
#include <highgui.h>

using namespace cv;

int main()
{

Mat im = imread("image.jpg", CV_LOAD_IMAGE_COLOR);
namedWindow("image");
imshow("image", im);
waitKey(0);
}

Explanation

Mat im = imread("image.jpg", CV_LOAD_IMAGE_COLOR);
Mat can be used to create a matrix.  Once the matrix is created we can assign the image to the matrix. Actually Mat is a class and the image is actually stored in a pointer in the class. Mat can be used to create n dimensional matrices. The created Mat object can be defined in a number of ways using the member functions. I will illustrate some of the usage here.
Mat im,M1,M2,M3;
im.create(100,60,CV_8UC(15));
M2 = im.col(1);
im.CopyTo(M1);
M3 = im.clone();
The above snippet will create a Mat Object and allocate it for a 100x60 8 bit 15 channel image.
The first coloumn of object im  is copied into the object M2.
The entire data of im is copied into M1. 
A clone of the image header which contains the details of the image are copied into the object M3. Now the object shares the same image with im.
namedWindow("image");
imshow("image", im);
waitKey(0);
namedWindow(const char* name,int flag)  is used to create window to show our image. This is not a necessary step. But you may sometimes encounter some logical errors if you dont create a window explicitly. To keep the explanation simple I am omitting the flags parameters because we never use that in real life coding.
To read more : namedWindow 

imshow(const string& winname, InputArray mat) is used to display an image in a specified window. The window being reffered here is the one we created using the namedWindow function. Therefore the window name must be same. Otherwise twoo windows will be created, one with image while the other, created using the namedWindow will remain just as an empty window. 
To read more :imshow



waitKey(int milliseconds) is used to wait for a keypress. The parameter is the number of milliseconds to wait. If it is zero, it means that the excecution must be paused until a keypress. Otherwise, the program will wait for the mentioned milliseconds for a keypress and continue the excecution if no keypress is detected. But if a keypress is detected, the function will return the ASCII code corresponding to the key pressed.
To read more :waitKey

You may have noticed that unlike the C implementation, we didnt deallocate the memory. Yes. We dont have to care about the memory management in the class implementation of opencv because the destructors will make sure that the memory is deallocated once our object goes out of scope. This will make our code more efficient and faster. If you want to read more about the memory management method in OpenCV2 please go trough the OpenCV API reference for 2.4.6
Powered by Create your own unique website with customizable templates.