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(); } ExplanationIplImage *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 second parameter is the Image color. Here you can specify how you want to import the image. The various possible values are :
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); } ExplanationMat 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 100×60 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. 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. 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. 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 |
I like the valuable information you provide in your articles.
I’ll bookmark your weblog and check again here regularly.
I am quite certain I will learn many new stuff right here!
Best of luck for the next!