Sunday, July 27, 2008

Load & Display Image using OpenCV

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

cvWaitKey(0);

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 !

13 comments:

Fu Fu said...

Hi Masimet, I am very new and also interest in OpenCV. I have many confusing with the OpenCV. Do you mind or not if I may disturb your time to ask a question via Email. My Email is alphokung@gmail.com.

I'll waiting for your reply.
Very Thank

Anonymous said...

hai mas Masimet, saya mau coba program opencvnya ada yang gak jelas di 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. dan Add "C:\Program Files\OpenCV\bin" to PATH. The location of OpenCV DLL files might be differentEach different path file is separated by semicolon (;) dan jika di compile ada error
inking...

LIBCD.lib(wincrt0.obj) : error LNK2001: unresolved external symbol _WinMain@16

Debug/coba1.exe : fatal error LNK1120: 1 unresolved externals

Error executing link.exe.



coba1.exe - 2 error(s), 0 warning(s)

Cik Di said...

Salam Mas Masimet. I just browse for opencv tutorial and finally get here. I tried your code for the first time using opencv and its good. I`m diyana from ump, kawan Mas Kamarul. Do you remember me? The one who attend Matlab course at UMp with Mas kamarul. Anyway..good blog too..letakla code banyak2 sini. Saya baru blajar opencv and i will used it for my project.

Anonymous said...

Salam Mas Masimet;
Saya Razali dari UMP dan skrg sedang meneruskan pengajian PhD di Chiba University, Japan. Saya cuba load image menggunakan program code yg diberikan di sini, tapi ada error spt di bawah:
:\Program Files\OpenCV\cv\src\MyImage.cpp(22) : fatal error C1010: unexpected end of file while looking for precompiled header directive
Generating Code...
Error executing cl.exe.

cv110d.dll - 1 error(s), 1 warning(s)

Kenapa ye?
Bagaimana bisa saya load image dari drive D atau yang lain2?

Terima kasih. Wassalam.

abhishek said...

Hi, Masimet
i m a new learner of openCV. i have followed all instructions for loading and displaying an image. i have done path linking, project setting, but i m getting an error every time. error is as follows:

compiling...
test.cpp
D:\openCV program\test.cpp(23) : fatal error C1010: unexpected end of file while looking for precompiled header directive
Error executing cl.exe.

cv110d.dll - 1 error(s), 0 warning(s)

my e-mail is
abhikanpuriya@gmail.com
i will wait for your reply.
thanks

Anonymous said...

see what is written here http://opencv.willowgarage.com/wiki/CodeBlocks and do exactly as it is saying you have to create a project use codeblocks compiler its free and easy to use.

regards, for any problem you can mail me sarkarcse@yahoo.com I will try

waqas said...

Assalam-o-ailikum... I am using Open Cv first time. I used yours code. Its works well. Thanks
Waqas

Sherry said...

Assalam alaikum !
i am new to OpenCv and have to implement my electronics degree's undergrad final year project on it. I would be really grateful if u send me your email id so that i may send you my queries and seek advice on them
my email id is sherry_sherry1@hotmail.com
pls email me on that, i ll tell u my queries

Thanks Alot!

Sheheryar Zahoor Qazi
Karachi,Pakistan

robotica said...

Thank you VERY MUCH. Im trying to get started in OpenCV but all the tutorials think you already understand what you're doing!! Thanks for the friendly comments and simple sintax.

Anonymous said...

C:\Program Files (x86)\OpenCV\cv\include\cvcompat.h|425|error: macro "CV_UNREFERENCED" passed 4 arguments, but takes just 1|

what can ı do opencv 1.0 and codeblocks 10.5

waqas said...

Thanks.
JazakALLAH.....

Very simple and nice interpretation...

Anonymous said...

how could we modify this if we want to manually choose the wavelength of freq of capturing the image?

Unknown said...

Thanks, that helped