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 |
Very shortly this website will be famous amid all blogging and site-building visitors, due to
it’s good posts
Thanks very interesting blog!
Wow, fantastic blog layout! How long have you been blogging for?
you make blogging look easy. The overall look of
your site is wonderful, as well as the content!
May I just say what a comfort to discover someone that
really understands what they are discussing on the net.
You certainly understand how to bring a problem to light and make it important.
More and more people must check this out and understand this side of the story.
I was surprised you are not more popular since you definitely
possess the gift.
Thanks a bunch for sharing this with all folks you actually realize what you’re speaking approximately!
Bookmarked. Please additionally consult with my website =).
We will have a link change agreement among us
Hmm is anyone else experiencing problems with the images on this blog loading?
I’m trying to determine if its a problem on my end or if it’s the blog.
Any feed-back would be greatly appreciated.
Hello, after reading this remarkable paragraph i am also glad to
share my know-how here with friends.
Hmm is anyone else encountering problems with the pictures on this blog loading?
I’m trying to determine if its a problem on my end or if it’s the blog.
Any suggestions would be greatly appreciated.
It’s truly very complicated in this full of activity life to listen news on Television, therefore I simply use world wide web for that purpose, and get the latest news.
I always used to study article in news papers but now as I am
a user of net thus from now I am using net
for content, thanks to web.
Hi there! This post couldn’t be written much better!
Reading through this post reminds me of my previous roommate!
He continually kept talking about this. I will forward this information to him.
Pretty sure he will have a good read. Thank you for
sharing!
Hi i am kavin, its my first occasion to commenting anyplace,
when i read this piece of writing i thought i
could also make comment due to this brilliant piece of writing.
There is definately a great deal to find out about this subject.
I love all of the points you’ve made.