Creating an OpenCV application with Eclipse on Windows

เนื่องจากว่าผมใช้ระบบ Windows เป็นหลัก แต่งานวิจัยผมพัฒนาบน Ubuntu ซึ่งยังยอมรับว่าไม่สามารถใช้ Ubuntu เพียวๆ ได้ เป็นเหตุทำให้งานวิจัยค่อนข้างล่าช้าไปพอสมควร ผมจึงคิดว่าจะมาเขียนโปรแกรมบน Windows แทน และวันนี้ได้ลองผิดลองถูกที่จะใช้ OpenCV กับ Eclipse อยู่นานมาก ขอจดขั้นตอนเก็บไว้หน่อยละกัน

โปรแกรมที่จำเป็นต้องติดตั้ง

  1. MinGW: เป็นชุดคำสั่งรวมพวกไลบรารี่ของภาษา Programming ซึ่งในที่นี้โปรแกรมที่จะติดตั้งก็คือ C/C++ compiler
  2. Eclipse CDT: ดั้งเดิมเป็น IDE สำหรับภาษา Java แต่ตัวนี้ได้ติดตั้งโปรแกรมเสริมให้รองรับการพัฒนาภาษา C/C++
  3. OpenCV for Windows: และแน่นอนที่ขาดไม่ได้ นั่นก็คือ OpenCV ซึ่งเป็นชุดคำสั่งรวมพวกไลบรารี่ของการพัฒนาโปรแกรมทาง Computer Vision หรือ Image Processing

Continue reading "Creating an OpenCV application with Eclipse on Windows"

Installing OpenCV on Ubuntu

Since I often reinstall Ubuntu as well as OpenCV, it would be better to note here. Thanks to Dr. Dailey for his tutorial on VGLWiki.

These are the main packages we need to install first.

  • libcv1
  • libcvaux1
  • libcvaux-dev
  • libcv-dev
  • libhighgui1
  • libhighgui-dev
  • opencv-doc (optional)

If you want to use gstreamer, here are the steps:

  1. Install two additional packages: libgstreamer0.10-dev and libdc1394-13-dev.
  2. Download OpenCV from sourceforge and extract to a folder, let's say "opencv".
  3. Change directory to that folder and run this command:
    ./configure --prefix=${HOME}/opencv --without-xine --without-quicktime --with-gstreamer --with-1394libs --with-v4l
  4. make
  5. make install
  6. The last thing is to is to tell the dynamic linker where the DLLs are; so, add the line:
    export LD_LIBRARY_PATH=${HOME}/opencv/lib
    to ~/.bashrc and then relogin.To check if it is working, type: env | grep LD_L Then you should see the result like this:
    LD_LIBRARY_PATH=/home/<your username>/opencv/lib

If you want to use ffmpeg, follow the steps (Thanks to Phong for his guidance):

  1. Install two additional packages: libswscale-dev and ffmpeg.
  2. create a folder /usr/include/ffmpeg.
  3. For Jaunty Jackalope (Ubuntu 9.04), we need to create some soft links under /usr/include/ffmpeg as follows:
    • avcodec.h to ../libavcodec/avcodec.h
    • avformat.h to ../libavformat/avformat.h
    • avio.h to ../libavformat/avio.h
    • avutil.h to ../libavutil/avutil.h
    • swscale.h to ../libswscale/swscale.h
  4. Then run this: ./configure --prefix=${HOME}/opencv --with-ffmpeg --without-quicktime
  5. make
  6. make install
  7. And don't forget to tell the dynamic linker: export LD_LIBRARY_PATH=${HOME}/opencv/lib

For the system-wide installation, change --prefix=${HOME}/opencv to --prefix=/usr/. If you want to remove OpenCV, use the commands: make uninstall or make distclean

Here I also give an example of Makefile to compile with OpenCV (Thanks to Dr. Dailey again for his example).

#What needs to be built (TARG) from what source code (SRC)
SRC = opencv-test.c
TARG = opencv-test

#Tell make what compiler we use
CC = gcc

#Tell gcc about non-standard places to find include (.h) files
#For a system wide installation use -I /usr/include/opencv
INC = -I $(HOME)/opencv/include/opencv

#Also tell gcc to include debug symbols (-g), give all possible warnings
#(-Wall), but don't generate the annoying "unused function" warning
CFLAGS = -g -Wall -Wno-unused-function $(INC)

#Tell the linker where to look for libraries and what's needed for OpenCV
#For a system wide installation -L isn't necessary
LIB = -L $(HOME)/opencv/lib -lcxcore -lcv -lhighgui -lcvaux -lml
LDFLAGS = $(LIB)

#The object files we want are just the source files with .c -> .o
OBJ = $(SRC:.c=.o)

#What make should try to build by default
all: $(TARG)

#What object files the target executable depends on
$(TARG): $(OBJ)

#Clean up executables, object files, and temp files
clean: rm -f *~ *.o $(TARG)

That's it. Enjoy OpenCV!