Load & Display Image using OpenCV

By | July 27, 2008

Let’s start to create our first project!

1. Open OpenCV workspace: Click Start–>All Programs–>OpenCV–>OpenCV Workspace MSVC6
2. Create new C++ source file: Click File–>New–>C++ Source File. Put the file name & the folder. Now you have a blank editor to write the code.
3. Write the following code:

#include “cv.h” //main OpenCV header
#include “highgui.h” //GUI header

int main() {
// declare a new IplImage pointer

IplImage* myimage;

// load an image
myimage = cvLoadImage(“sayyidsmile.jpg”,1); //change the file name with your own image

//create a new window & display the image
cvNamedWindow(“Smile”, 1);

cvMoveWindow(“Smile”, 100, 100);

cvShowImage(“Smile”, myimage);

//wait for key to close the window

cvDestroyWindow( “Smile” );

cvReleaseImage( &myimage );

return 0;


4. Add library in the project setting: Click Project–>Setting–>Link–>Object/Library Modules then write cv.lib cvaux.lib cxcore.lib highgui.lib. Make sure that you do this step anytime you create a new project.
5. Save and click “Execute Program” icon, then your window named “Smile” will appear.

Easy right …? Explore other functions in OpenCV and enjoy it !

Download this article: DOC or PDF