Posts Tagged ‘OpenCV’

Javacv Example

September 17, 2012 2 comments

First try out java opencv, which is java wrapper for opencv. The first thing you should do is to follow the instruction install opencv. You will need to know where are your lib files locate. In this example, I have them all in ‘opencv24lib’.  Here’s what it contains:

Download javacv zip files, unzip it, you will get several jar files(wrappers) which needed for your project. Below is a link to a good warming up example of javacv:

//static imports
import static com.googlecode.javacv.cpp.opencv_core.*;
import static com.googlecode.javacv.cpp.opencv_highgui.*;
import static com.googlecode.javacv.cpp.opencv_imgproc.*;
//non-static imports
import com.googlecode.javacv.cpp.opencv_core.CvScalar;
import com.googlecode.javacv.cpp.opencv_core.IplImage;

public class ColorDetect {
    //color range of red like color
    static CvScalar min = cvScalar(0, 0, 130, 0);//BGR-A
    static CvScalar max= cvScalar(140, 110, 255, 0);//BGR-A    public static void main(String[] args) {
        //read image
        IplImage orgImg = cvLoadImage("colordetectimage.jpg");
        //create binary image of original size
        IplImage imgThreshold = cvCreateImage(cvGetSize(orgImg), 8, 1);
        //apply thresholding
        cvInRangeS(orgImg, min, max, imgThreshold);
        //smooth filter- median
        cvSmooth(imgThreshold, imgThreshold, CV_MEDIAN, 13);
        cvSaveImage("threshold.jpg", imgThreshold);

To compile and run the code (inside the folder of your code):

mkdir classes
javac -classpath .:/yourpathto/javacpp.jar:/yourpathto/javacv.jar:/yourpathto/javacv-linux-x86_64.jar:/yourpathto/javacv-linux-x86.jar *.java -d ./classes
jar cf JOpencvTest.jar -C classes .
java -Djava.library.path=/yourpathto/opencv24lib -cp .:/yourpathto/javacpp.jar:/yourpathto/javacv.jar:/yourpathto/javacv-linux-x86_64.jar:/yourpathto/javacv-linux-x86.jar:JOpencvTest.jar ColorDetect

Input Image:

output Image:


Here’s a link how to configure it on Win7, I didn’t give it a try since my windows machine installed of opencv2.3 not 2.4 version.


Categories: OpenCV Tags: ,

Opencv Test on Xcode

April 19, 2012 1 comment

I pre-installed opencv (2.3.1) for python binding, but haven’t tried it out in Xcode. Here’s what I found that works on my Mac. I am using Xcode4.3, Mac Lion 10.7.3.

Just to clarify, I almost followed this link, which turns out working well for the example code.

– Make a new project and choose “Command Line Tool”, choose C++ as the type for the project. Xcode will automatically generate a main.cpp for you.

– Go to project setting, select all, among which search the “Header search path”, and add “/opt/local/include”

– Create a folder for your ‘framework’ on the left panel,  let’s say, name it as “opencv-Frameworks”, and right click it use “Add files to..”. You will going to add three files which are located at </opt/local/lib>. They are: libopencv_ml.2.3.1.dylib, libopencv_highgui.2.3.1.dylib, libopencv_core.2.3.1.dylib

– You don’t have to copy those items, but just an option 🙂

– Now, copy the demo code in the above link to your main.cpp, click build+run, you should see a pop up window like this:

– You can find a completed working project here.

Categories: MacOS, OpenCV Tags: ,

Install OpenCv with Python Binding on MacOS

April 4, 2012 1 comment

I had tried several versions of install opencv on my mac that can be used in python.

Here are lessons — use the python provided by MacOS, you may have the following problem if you are using other versions.


– Use python by MacOS, which you should be able to finda and locate, e.g. the link /usr/bin/python    (my version is Python 2.7)

I found that I can not use the python I installed somewhere else, such as </Library/Frameworks/Python.framework/Versions/2.7/>

– I used the brew to install opencv:

   $sudo brew install opencv  

It enventually install it to  </usr/local/Cellar/opencv/2.3.1a>

– Next step, you need to add the path </usr/local/Cellar/opencv/2.3.1a/lib/python2.7/site-packages> to your PYTHONPATH

If you check the sit-packages folder, it should include two files: and

So, change your ~/.profile to include the following line:

export PYTHONPATH=”/usr/local/Cellar/opencv/2.3.1a/lib/python2.7/site-packages:${PYTHONPATH}”

Otherwise, whenever you would like to import the cv, you will have to first include the path in your python code, something like this:


Now, time to do the test:

I couldn’t find the example folder of python by the package installed by brew.

So I went ahead and use the previous download one, which is a full package opencv:

$ls ~/Documents/OpenCV-2.3.1/samples/python             (where you will be able to find the file, then go ahead to run it)

$ python ~/Documents/OpenCV-2.3.1/samples/python/

Nice , now it’s running, finally 🙂

Install Opencv Python API

February 16, 2012 1 comment

1. Download and install Python 2.7
2. Download and install NumPy and SciPy. You can find most of the compieled packages for windows here:
3. Download OpenCV 2.3.1 for windows use the superpack one:
4. Double click on the OpenCV installer. It will extract OpenCV to your selected folder. I put it to C:\OpenCV2.3.
5. Configure your opencv: add path to your system enviroment.
At least you should add C:\OpenCV2.3\build\x86\vc10\bin to PATH
I forget what I did when I first install opencv, well this is my current system path:
C:\OpenCV2.3\build\lib\Debug;C:\OpenCV2.3\build\x64\vc10\bin;C:\Program Files\Intel\tbb\4.0\bin\intel64\vc10;C:\OpenCV2.3\bin

6. OpenCV 2.3.1 folder structure is different from OpenCV 2.2. The pre-build library and Python package are located at C:\OpenCV2.3\build.
7. Open Python IDLE
8. Add the following path to your python path:

import sys

8. Now, you are ready to import OpenCV with
import cv