Archive

Archive for the ‘OpenCV’ Category

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);
        //save
        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: ,

MapReduce in C++

September 13, 2012 Leave a comment

Since I can’t request the admistrator to configure any particular setting for the cluster nodes.  I found the simplest way is to compile your code locally, use hadoop streaming,  ship all you liked os-files with your mapper (compiled executable) together to the node. You could write a bash codes as the mapper, which config the lib-path, and wrap the exe inside it.  Below are some websites talking about MapReduce in C++, which actually very nice, but may need more to configure.

Boost: http://www.craighenderson.co.uk/mapreduce/

JavaCV : nice for using opencv  http://code.google.com/p/javacv/

Mapreduce-lit:  http://code.google.com/p/mapreduce-lite/

Very nice example(Mapper with Second key):  http://code.google.com/p/hadoop-stream-mapreduce/

Categories: Hadoop, OpenCV

Config opencv in EclipseC++

July 26, 2012 Leave a comment

To use Eclipse C++ for your opencv related project, there are two key parts in configuration.  This post give a very clear instruction.

Note

If you do not know where your opencv files are, open the Terminal and type:

pkg-config --cflags opencv

For instance, that command gave me this output:

-I/usr/local/include/opencv -I/usr/local/include

So if you check the files under the ‘include folder’:

$ls /usr/local/include/opencv
cv.h        cvaux.hpp    cxcore.hpp    highgui.h
cv.hpp        cvwimage.h    cxeigen.hpp    ml.h
cvaux.h        cxcore.h    cxmisc.h

If you don’t know where your libraries are (or you are just psychotic and want to make sure the path is fine), type in Terminal:

pkg-config --libs opencv

My output (in case you want to check) was: .. code-block:: bash

-L/usr/local/lib -lopencv_core -lopencv_imgproc -lopencv_highgui -lopencv_ml -lopencv_video -lopencv_features2d -lopencv_calib3d -lopencv_objdetect -lopencv_contrib -lopencv_legacy -lopencv_flann

 

1. Include the right path

 

2. Put in right liker information(search path and filenames)


If check the linker path:you may find that opencv related libs are with prefix of lib, and suffix as x.x.x.dylib

the right way to put these files into the linker librarys, you need to strip off the prefix(lib) and suffix(dylib), but you need to keep the version number is there is any.  You could include all of them or just few top ones, which really depends on your application.

$ ls /usr/local/lib
QtCore.framework            libjasper.1.0.0.dylib
QtDeclarative.framework            libjasper.1.dylib
QtDesigner.framework            libjasper.a
QtDesignerComponents.framework        libjasper.dylib
QtGui.framework                libjpeg.8.dylib
QtHelp.framework            libjpeg.a
QtMultimedia.framework            libjpeg.dylib
QtNetwork.framework            libopencv_calib3d.2.3.1.dylib
QtOpenGL.framework            libopencv_contrib.2.3.1.dylib
QtScript.framework            libopencv_core.2.3.1.dylib
QtScriptTools.framework            libopencv_features2d.2.3.1.dylib
QtSql.framework                libopencv_flann.2.3.1.dylib
QtSvg.framework                libopencv_gpu.2.3.1.dylib
QtTest.framework            libopencv_highgui.2.3.1.dylib
QtWebKit.framework            libopencv_imgproc.2.3.1.dylib
QtXml.framework                libopencv_legacy.2.3.1.dylib
QtXmlPatterns.framework            libopencv_ml.2.3.1.dylib
gcc                    libopencv_objdetect.2.3.1.dylib
libQtCLucene.4.8.1.dylib        libopencv_ts.2.3.1.dylib
libQtCLucene.4.8.dylib            libopencv_video.2.3.1.dylib
libQtCLucene.4.dylib            libtbb.dylib
libQtCLucene.dylib            libtbbmalloc.dylib
libQtCLucene.prl            libtiff.3.dylib
libQtUiTools.a                libtiff.a
libQtUiTools.prl            libtiff.dylib
libQtUiTools_debug.a            libtiffxx.3.dylib
libQtUiTools_debug.prl            libtiffxx.a
libfrtbegin.a                libtiffxx.dylib
libg2c.0.0.0.dylib            node
libg2c.0.dylib                node_modules
libg2c.a                phonon.framework
libg2c.dylib                pkgconfig
libg2c.la                python2.7
libiberty.a

 

 

Categories: C++, OpenCV

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.

Steps:

– 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:

http://opencv.willowgarage.com/wiki/Mac_OS_X_OpenCV_Port?highlight=%28%28InstallGuide%29%29

   $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: cv.py and cv2.so

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:

sys.path.append(‘/usr/local/Cellar/opencv/2.3.1a/lib/python2.7/site-packages’)

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 delaunay.py file, then go ahead to run it)

$ python ~/Documents/OpenCV-2.3.1/samples/python/delaunay.py

Nice , now it’s running, finally 🙂