Labels

Linux (6) OpenCV (4) MATLAB (3) Mac OS X (3) Windows (2) C# (1) Node JS (1)

2013年12月29日 星期日

Setup VNC server on Fedora

The VNC server can support multi-user desktops and is very useful for Linux system (On Windows I prefer to use Teamviewer). There is a great tutorial written by Jack Wallen. However on Fedora there is minor difference on setting up firewall. The steps of setting up VNC server on Fedora is listed below:

1. Install VNC server
yum -y install tigervnc-server


2. Add VNC user
su vncuser1
vncpasswd


3. Edit VNC config file
sudo vim /etc/sysconfig/vncservers
add the following lines:
VNCSERVERS="1:vncuser1 2:vncuser2 "
VNCSERVERARGS[1]="-geometry 1800x1200"
VNCSERVERARGS[1]="-geometry 1440x960"


4. Start VNC server
sudo service vncserver start


5. Enable firewall port 5901 by running config (Note editing iptable doesn't work on Fedora). Remember to add port 5902 for second user.
sudo system-config-firewall


6. Connect with VNC viewer (I use Mac version). Remember to add :1 after your IP address!








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年7月2日 星期二

Using SCP on MAC with Filename Containing Spaces

This is a little tricky so it's worth posting:


scp user@host:"'/Path/Some Filename With Spaces'" [destination]


Note that we need to use " + ' to brace the Path, or the SCP will print error "scp: ambiguous target"

2013年6月25日 星期二

Convert File Glob to Regular Expression

The tips of converting file glob to regular expression are extracted from following website
http://www.proftpd.org/docs/howto/Regex.html
Thanks to Jan Borsodi for the good tutorial. The tips are as follows:


Wildcards
For people who have some knowledge with wildcards (also known as file globs or file globbing), I'll give a brief explanation on how to convert them to regular expressions. After reading this article, you probably have seen the similarities with wildcards. For instance:
*.jpg

matches any text which end with .jpg. You can also specify brackets with characters, as in:
*.[ch]pp

matches any text which ends in .cpp or .hpp. Altogether very similar to regular expressions.
The * means match zero or more of anything in wildcards. As we learned, we do this is regular expression with the punctuation mark and the * quantifier. This gives:
.*

Also remember to convert any punctuation marks from wildcards to be backslashified.
The ? means match any character but do match something. This is exactly what the punctuation mark does.
Square brackets can be used untouched since they have the same meaning going from wildcards to regular expressions.
These leaves us with:
  • Replace any * characters with .*
  • Replace any ? characters with .
  • Leave square brackets as they are.
  • Replace any characters which are metacharacters with a backslashified version.
Examples
*.jpg

would be converted to:
.*\.jpg

ez*.[ch]pp

would convert to:
ez.*\.[ch]pp

or alternatively:
ez.*\.(cpp|hpp)





2013年6月9日 星期日

Using MATLAB Local Parallel Computing Toolbox

MATLAB Parallel Computing Toolbox can automatically divide tasks of for loop and run multiple instances simultaneously. Just use the "parfor" keyword and MATLAB will do the rest work. However, we usually need to configure MATLAB to make this feature work. The configuration is located at:
Menu -> Parallel -> Manage Configurations...

The common two issues that prevent running parallel computing are:
1. Unresolved hostname
=> MATLAB use TCP/IP and hostname to connect to clients. Please make sure your hostname mapping is set in /etc/hosts. Use command !hostname in MATLAB to check your hostname

2. Data Location is not set
=> To set data exchange location, open Menu -> Parallel -> Manage Configurations..., select the local profile,  Property... -> set "Folder where Job data is stored (DataLocation)" to [your location] (e.g. /tmp/MATLAB)


Other problems include no license of Parallel Computing Toolbox, check it by typing command:
license checkout Distrib_Computing_Toolbox
Or problem of local mpiexec
If you are using R2010a or newer, you may experience issues with the new local mpiexec implementation. In that case, try the following command to disable this feature:
distcomp.feature( 'LocalUseMpiexec', false )
Refer to http://www.mathworks.com/support/solutions/en/data/1-C27YO8/index.html?product=SL&solution=1-C27YO8 for more information


Moreover, MAC user may encounter the error below:
The class distcomp.typechecker has no property or method named 'getDefaultValue'.

Error in distcomp.configsection (line 50)
    obj.PropValue{i} = distcomp.typechecker.getDefaultValue(obj.Types{i});

This is a known bug of MATLAB with Java version. Please refer to MATLAB official site:

Bug 919688

Summary

Parallel Computing Toolbox code fails with Java Virtual Machine update 1.6.0_39

Description

Running any Parallel Computing Toolbox code using a Java Virtual Machine (JVM) version of 1.6.0_39 or later might cause a NullPointerException to be thrown by the JVM. For example:....


Follow the instruction on MATLAB, download the JVM and update it. Be careful not to overwrite other files under MATLAB folder!

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

2013年2月2日 星期六

Running Mosek on Mac OS X 10.8 Mountain Lion

Mosek is a commercial software library for solving large-scale LP, QP, SOCP and MIP problems. (http://www.mosek.com)

To use the library, the user needs to add the following environment variables:
export DYLD_LIBRARY_PATH= $HOME/mosek/6/tools/platform/osx64x86/bin:$DYLD_LIBRARY_PATH
export MOSEKLM_LICENSE_FILE=$HOME/mosek/6/licenses/mosek.lic

suppose you put Mosek under your home directory.

Before Mountain Lion, the environment variables were put in .MacOSX/environment.plist. However, this method is no longer supported by OS X 10.8. Therefore, we need to put the variables in /etc/profile, and execute MATLAB from terminal

Finally, remember to add mosek/6/toolboxs/r2009b into your MATLAB path. Then you can run Mosek on Mac!