Labels

Linux (6) OpenCV (4) Deep Learning (3) MATLAB (3) Mac OS X (3) Windows (2) C# (1) Node JS (1)
顯示具有 OpenCV 標籤的文章。 顯示所有文章
顯示具有 OpenCV 標籤的文章。 顯示所有文章

2013年7月14日 星期日

Using LIBSVM with OpenCV Mat


  LIBSVM is the most popular machine learning tool developed by C. J. Lin at National Taiwan University (http://www.csie.ntu.edu.tw/~cjlin/libsvm/). This code demonstrates how to load a data matrix in CSV format using OpenCV, and allocates LIBSVM data structure to do SVM predict. 


#include "svm.h"
#include <iostream>

#include "opencv2/opencv.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/ml/ml.hpp"
#include <iostream>

using namespace cv;
using namespace std;


const char *CSV_FILE = "/Users/kuanting/libsvm-3.17/heart_scale.csv";
const char *MODEL_FILE = "/Users/kuanting/libsvm-3.17/heart_scale.model";

int main(int argc, char * argv[])
{
    CvMLData dataFile;
    
    // Load matrix data in csv format
    if (dataFile.read_csv(CSV_FILE) != 0)
    {
        fprintf(stderr, "Can't read csv file %s\n", CSV_FILE);
        return -1;
    }
    
    Mat dataMat(dataFile.get_values()); // Default data type is float
    
    struct svm_model *SVMModel;
    if ((SVMModel = svm_load_model(MODEL_FILE)) == 0) {
        fprintf(stderr, "Can't load SVM model %s", MODEL_FILE);
        return -2;
    }
    
    struct svm_node *svmVec;
    svmVec = (struct svm_node *)malloc((dataMat.cols+1)*sizeof(struct svm_node));
    double *predictions = new double[dataMat.rows];
    float *dataPtr = dataMat.ptr<float>(); // Get data from OpenCV Mat
    double prob_est[2];  // Probability estimation
    int r, c;
    for (r=0; r<dataMat.rows; r++)
    {
        for (c=0; c<dataMat.cols; c++)
        {
            svmVec[c].index = c+1// Index starts from 1; Pre-computed kernel starts from 0
            svmVec[c].value = dataPtr[r*dataMat.cols + c];
        }
        svmVec[c].index = -1;   // End of line
        
        if(svm_check_probability_model(SVMModel))
        {
            predictions[r] = svm_predict_probability(SVMModel, svmVec, prob_est);
            printf("%f\t%f\t%f\n", predictions[r], prob_est[0], prob_est[1]);
        }
        else
        {
            predictions[r] = svm_predict(SVMModel, svmVec);
            printf("%f\n", predictions[r]);
        }
    }
    
    return 0;
}

2013年6月7日 星期五

Running OpenCV Program on Linux Cluster without Installing Libraries

Computer Vision programs usually require heavy computing power and are time-consuming. Therefore, we usually need to run our programs on a larger clusters of machines. The problem is, we don't have the administration privilege to install libraries. This article discusses ways of running OpenCV program without installing libraries.

Supposed we have installed OpenCV on our local linux, the first thought is using a script to copying all shared libraries dependencies from local machine to target clusters. I used the great script "cpld" developed by Hemanth Copying shared library dependencies | Experiments on GNU/Linux:

#!/bin/bash 
# Author : Hemanth.HM
# Email : hemanth[dot]hm[at]gmail[dot]com
# License : GNU GPLv3
#

function useage()
{
    cat << EOU
Useage: bash $0 <path to the binary> <path to copy the dependencies>
EOU
exit 1
}

#Validate the inputs
[[ $# < 2 ]] && useage

#Check if the paths are vaild
[[ ! -e $1 ]] && echo "Not a vaild input $1" && exit 1
[[ -d $2 ]] || echo "No such directory $2 creating..."&& mkdir -p "$2"

#Get the library dependencies
echo "Collecting the shared library dependencies for $1..."
deps=$(ldd $1 | awk 'BEGIN{ORS=" "}$1\
~/^\//{print $1}$3~/^\//{print $3}'\
 | sed 's/,$/\n/')
echo "Copying the dependencies to $2"

#Copy the deps
for dep in $deps
do
    echo "Copying $dep to $2"
    cp "$dep" "$2"
done

echo "Done!"


However, the method will fail if the gcc complier on local machine is different from target cluster. Another method is to compile libraries on target machine.

1. Download OpenCV source code
2. Under the source folder, execute "mkdir release"
3. Run "cmake -D CMAKE_BUILD_TYPE=release .."
4. Verify the FFMPEG is suppored
5. make

P.S. In terms of FFMPEG, I used default libraries installed in the cluster to compile OpenCV. But while executing my program, I encounter the error of missing library "libavdevice.so". In order to sovle this issue, I have to re-compile FFMPEG-1.2, which required to link to additional libraries "libavfiler" and "libswresample" in the Makefile. I copied all FFMPEG libraries to the execution folder. Although OpenCV is compiled with older FFMPEG version (.so.52), it works fine with newer FFMPEG libraries (.so.54).


After complied, all the libraries will be put in opencv/release/lib. But since we don't have root right to install files, the include header files are not ready. To handle this issue, we need to copy our local OpenCV headers to the target machine:

scp -r /usr/lcoal/opencv user@target.com:/opencv/include/
scp -r /usr/lcoal/opencv2 user@target.com:/opencv/include/


To compile a program, we just need to assign the include and link paths of OpenCV in make file.

Finally, we need to export the library for dynamically link at runtime. (new FFMPEG are copied to current folder, so "." is also added to the PATH)
export LD_LIBRARY_PATH=.:opencv-2.4.5/release/lib

2013年5月23日 星期四

Solve the "recompile with -fPIC" error when compiling OpenCV with FFMPEG

The error happens while one wants to compile OpenCV with FFMPEG on 64 bits linux. Adding the parameter "--enable-pic" is not enough. Both the --enable-shared and --enable-pic need to be included when configuring ffmpeg:

./configure --enable-shared --enable-pic


For more details, please refer to :

A Comprehensive Guide to Installing and Configuring OpenCV 2.4.2 on Ubuntu
http://www.ozbotz.org/opencv-installation/

I have successfully compiled on Fedora 64 bits. Good luck.



Create links to old OpenCV libraries

Thanks to Michael C. Hughes, which posted a method to create symbolic links of old OpenCV libraries. Here is the original blog:
http://web.michaelchughes.com/how-to/install-stip-software-with-opencv-v2

Here is the way to link OpenCV old library (< 2.0) to latest versions :


  • libcxcore.so.2   --->  libopencv_core.so
  • libcv.so.2         ---> libopencv_imgproc.so
  • libhighgui.so.2  ---> libopencv_highgui.so
  • libml.so.2        --->  libopencv_ml.so
  • libcvaux.so.2    --->  libopencv_video.so
via the commands:

cd </PATH/TO/OPENCV/lib>
ln -s  libopencv_core.so  libcxcore.so.2

ln -s  libopencv_imgproc.so  libcv.so.2
ln -s  libopencv_highgui.so libhighgui.so.2
ln -s  libopencv_ml.so libml.so.2
ln -s libopencv_video.so  libcvaux.so.2