Compiling and running standalone matlab executable.

August 21, 2012

First, define the paths:

export LD_LIBRARY_PATH=/opt/matlab2012a/bin/glnxa64:/opt/matlab2012a/sys/os/glnxa64:/opt/matlab2012a/runtime/glnxa64/

Compile the .m file

mcc -mv mymatlabfile.m

The matlab compiler generates two execute files: mymatlabfile and run_mymatlabfile.sh

Ignore the shell script generated by matlab. Simply execute ./mymatlabfile 

If you have addpath commands in your .m file, these will cause error while running the standalone executables. To solve this, do the following

if ~isdeployed
    addpath('path1...');
end

If your .m file has input parameters, execute the file in the following way

mymatlabfile para1 para2.

For example, if this is your matlab file

function myprog(num1, num2)

you call the file by

myprog 100 200

Note that the paramters 100 and 200 will be passed as string instead of numbers so you have to modify your .m file:

function myprog(num1, num2)
if isdeployed
    num1 = str2double(num1);
    num2 = str2double(num2);
end

Bash script to run in clusters

#!/bin/bash

# total number = 8677
for i in {1..8599..860}
do
    let j=$i+860-1
    echo "Running $i and $j....."
    srun -c 5 -o output/output$i.txt ./calc_bg_pooling $i $j &
done

echo "Running the last small part....."
srun -c 5 -o output/output8601.txt ./calc_bg_pooling 8601 8677 &

Sort blobs generated by cvBlobsLib by areas.

May 18, 2011

cvBlobsLib is an useful OpenCV 3rd party library to do blob analysis. However the blobs extracted are sorted based on their y coordinates and the author provided no interface to sort the blobs by other criteria such as area. To do so, you need to make some internal change. Add the following member function that sort the blobs based on their areas to the class CBlobResult.

    // sort blobs
    void SortBlobs()
    {
        struct myclass {
            bool operator() (CBlob* b1,CBlob* b2)
            {
                return (b1->GetArea() < b2->GetArea());
            }
        } blobComp;

        std::sort(m_blobs.begin(),m_blobs.end(),blobComp);
    }

If you want to sort the blobs based on their x coordinates, change the 7th line of above code to

return (b1->GetBoundingBox().x < b2->GetBoundingBox().x);

.


Read LiDar/LAS file in matlab.

January 11, 2010

Below is the c code for reading LiDar/LAS file into matlab. To compile the code, simply use
mex LASReader.cpp

The code reads the points coordinates only. Please see the code for details.

For details of LiDar file format, please visit this link.

Read the rest of this entry »


De Boor’s Algorithm in C++

October 18, 2009
Visit my page at https://sites.google.com/view/chi3x10/home

Here is the c++ code for the De Boor algorithm. It calculates a point C(x) on a B-Spline curve of any degree.

Point deBoor(int k,int degree, int i, double x, double* knots, Point *ctrlPoints)
{   // Please see wikipedia page for detail
	// note that the algorithm here kind of traverses in reverse order
	// comapred to that in the wikipedia page
	if( k == 0)
		return ctrlPoints[i];
	else
	{
		double alpha = (x-knots[i])/(knots[i+degree+1-k]-knots[i]);
		return (deBoor(k-1,degree, i-1, x, knots, ctrlPoints)*(1-alpha )+deBoor(k-1,degree, i, x, knots, ctrlPoints)*alpha );
	}
}

The Point class is defined as the follow:

class Point
{
public:
	Point(){x=0.;y=0.;z=0.;};
	// copy operator
	Point operator=(const Point &pt) ;
	Point operator+(const Point &pt) const;
	//Point operator-(const Point &pt) const;
	Point operator*(double m) const;
	Point operator/(double m) const;
	double x,y,z;
};

Point Point::operator=(const Point &pt)
{
	x = pt.x;
	y = pt.y;
	z = pt.z;
	return *this;
}
Point Point::operator+(const Point &pt) const
{
	Point temp;
	temp.x = x + pt.x;
	temp.y = y + pt.y;
	temp.z = z + pt.z;
	return temp;
}
Point Point::operator*(double m) const
{
	Point temp;
	temp.x = x*m;
	temp.y = y*m;
	temp.z = z*m;
	return temp;
}
Point Point::operator/(double m) const
{
	Point temp;
	temp.x = x/m;
	temp.y = y/m;
	temp.z = z/m;
	return temp;
}

Include EPS graphics in pdflatex

June 18, 2009

https://sites.google.com/view/chi3x10/home

It’s a shame that pdflatex supports jpg, png, pdf… graphic type but not EPS while latex supports EPS but not the others. From my previous post, there is an easy way to convert EPS to pdf.  But it is just annoying that every time you want to include an eps graphic you have to do the conversion manually. After googling, I found out that there is a way to let pdflatex do the conversion for you. Here is how. Read the rest of this entry »


Convert EPS to PDF: problem of bounding box

June 17, 2009

https://sites.google.com/view/chi3x10/home

When you save a multiple plots/subfigures figure as an eps file in matlab, the bounding box might not be properly defined. People who use pdflatex need to convert EPS to PDF. The undefined bounding box will cause problems if you use epstopdf to convert EPS to PDF. This problem is shown in the first row in the picture below. The top-left figure is the eps file without properly defined bounding box. Because of this, epstopdf simply printed a letter size pdf file that only contains part of the figure as shown in the top-right figure. You can try to use the option –exact

epstopdf --exact bad.eps

It will probably generate an warning msg like “Warning: BoundingBox not found!” and the resulting pdf is still wrong. One way to fix this problem is to use epstool

epstool --copy --bbox bad.eps --output good.eps

The bbox option tells epstool to generate a (correct) bounding box for the eps file. With a correctly defined bounding box, you can easily convert an eps file to a pdf file.

epstopdf --exact good.eps

The results are shown in the 2nd row of the picture below. epstopdf generated an pdf file in the same size as the eps file.
pic


Create iPhone ringtone for FREE using iTune 8

February 21, 2009

https://sites.google.com/view/chi3x10/home

iTune allow users to create their own ringtone from the music file downloaded via iTune store. It’s very convenient for users to create ringtones from their favorite songs  but it’s not free. This guide will show you how to convert mp3/acc files to iPhone for FREE simply using iTune 8. Read the rest of this entry »


Install flash player for firefox in ubuntu 8.04

February 1, 2009

1. Open firefox. Go to youtube.com and select any video. If you havn’t install flah, a message of “Hello, you either have JavaScript turned off or an ol version of Adobe’s Flash Player. Get the lastest Flash player.”. Click on the link and you will be redirected to adobe website to download it. Select .deb for Ubuntu x.xx.

2. Let the Package installer software install it. Restart firefox and it should be working.

If it’s still not working, follow the following steps.

3. Open Terminal and input
$> firefox &
to execute firefox.

4. Go to youtube.com and click on any video. It will be just like step 1 but this time you can see the error message from the Terminal. For me, the error messages say
LoadPlugin: failed to initialize shared library /usr/lib/adobe-flashplugin/libflashplayer.so [libnss3.so: cannot open shared object file: No such file or directory]
The problem is not in flash player but because of a dependency lib of flash player mssing .

5. Open Synaptic package manager (System >> Administration >> Synaptic package manager) and install the missing lib (libnss3). Restart firefox and the flash should work fine.


WebDAV client with encrypted SSL connection in ubuntu

January 27, 2009

WebDAV is a set of extensions to the HTTP protocol that allow users to edit and manage files on remote www servers. Here is the installation guide.

1. #> sudo apt-get install davfs2

2. create a folder
mkdir /media/webdav

3. Mount remote webdav server by
sudo mount -t davfs https://ip_address:port /media/webdav



The index of the largest element in a 2D array in matlab

November 12, 2008

[v,ind]=max(X);
[v1,ind1]=max(max(X));
disp(['The largest element in X is' num2str(v1) ' at (' num2str(ind(ind1)) ',' num2str(ind1) ')']);


Converting images to avi file in matlab

October 21, 2008

https://sites.google.com/view/chi3x10/home

Here is the matlab code to convert a collection of images to a video file.

%// Will open an avi file name test.avi in local folder
aviobj = avifile('test.avi');
%// the quality of this video file
aviobj.Quality = 80;
%// compression method. See matlab manual for details.
aviobj.COMPRESSION ='None';%%color image

for i =1:numOfFrames
    %// apply image processing algorithms to the image here.
    %// image must be in size width x height x 3
    %//  in other words, color image.
    ..........
    ..........
    %// add image to the end of the avi file
    aviobj = addframe(aviobj,image);
end
%// close the file handle.
aviobj = close(aviobj);

Click here for detail instruction on addframe function.

Keywords: convert images jpb bmp to video avi compressed codec

Using rdesktop to remotely access window XP from ubuntu/linux

August 16, 2008

First, install rdesktop if you don’t already have it. rdesktop is a client software for RDP (remote desktop protocol) which is used in various microsoft OS’s.

$ sudo apt-get install rdesktop

Open a terminal and input

$ rdesktop -f -a 16 <ip/domain name>

where -f is for full screen mode and -a is the connection color depth. -a 16 means 16 bits color depth will be used for this connection. These are the two option I used. Other two useful options are -u <username> and -p <password>.

Press Ctrl-Alt-Enter to switch between full screen and window mode.


Displaying a sequence of images like video in matlab

July 9, 2008

images is an array of dim imageHeight x imageWidth x frameNumber


for i = 1:size(images,3)
    imshow(images(:,:,i));
    title(['frame# ' num2str(i)]);
    drawnow; %// this is why it works
end



Optical flow in matlab

June 9, 2008

Implementation based on Horn’s optical flow algorithm.


function [Vx,Vy] = OpticalFlow(images,alpha,iterations)
%// Calculating optical flow of a sequence of images. 
%// images : 3D array that contains a sequence of images. size of images is (imageHeight, imageWidth, frame number)
%// alpha
%// iterations. 
[height,width,frames]=size(images);

%//initialzation of u and v
Vx = zeros(height,width);
Vy = zeros(height,width);

for k = 1:frames-1
    
    % //initialization of Ex Ey and Et 
    Ex = zeros(height-1,width-1,frames-1);
    Ey = zeros(height-1,width-1,frames-1);
    Et = zeros(height-1,width-1,frames-1);
    
    %//calculating Ex Ey and Et in frame k.
    for x = 2:width-1
        for y = 2:height-1
            Ex(y,x,k) = (images(y+1,x+1,k)-images(y+1,x,k)+images(y,x+1,k)...
                -images(y,x,k)+images(y+1,x+1,k+1)-images(y+1,x,k+1)...
                +images(y,x+1,k+1)-images(y,x,k+1))/4;
            
            Ey(y,x,k) = (images(y,x,k)-images(y+1,x,k)+images(y,x+1,k)...
                -images(y+1,x+1,k)+images(y,x,k+1)-images(y+1,x,k+1)...
                +images(y,x+1,k+1)-images(y+1,x+1,k+1))/4;
            
            Et(y,x,k) = (images(y+1,x,k+1)-images(y+1,x,k)+images(y,x,k+1)...
                -images(y,x,k)+images(y+1,x+1,k+1)-images(y+1,x+1,k)...
                +images(y,x+1,k+1)-images(y,x+1,k))/4;
        end
    end

    for nn = 1:iterations
        for x = 2:width-1
            for y = 2:height-1
                
                Vxbar = (Vx(y-1,x)+Vx(y,x+1)+Vx(y+1,x)+Vx(y,x-1))/6+...
                     (Vx(y-1,x-1)+Vx(y-1,x+1)+Vx(y+1,x+1)+Vx(y+1,x-1))/12;
                
                Vybar = (Vy(y-1,x)+Vy(y,x+1)+Vy(y+1,x)+Vy(y,x-1))/6+...
                    (Vy(y-1,x-1)+Vy(y-1,x+1)+Vy(y+1,x+1)+Vy(y+1,x-1))/12;

                %// chapter 12 of Horn's paper
                temp = (Ex(y,x,k)*Vxbar+Ey(y,x,k)*Vybar+Et(y,x,k))/(alpha^2 + Ex(y,x,k)^2 + Ey(y,x,k)^2);
                %// update u and v 
                Vx(y,x) = Vxbar-Ex(y,x,k)*temp;
                Vy(y,x) = Vybar-Ey(y,x,k)*temp;
            end
        end
    end
    
end


Calculation of matrix inverse in C/C++

May 28, 2008

Below is the code to calculate matrix inverse of a matrix of arbitrary size (order) by using analytic solution. This method is known to be slow for very large matrix because of the recursion. However, I used this mainly for calculating inverse of 4×4 matrices and it worked just fine. You can also use CalcDeterminant and GetMinor to calculate determinant of a matrix.

MatrixInversion: the main function to calculate the inverse matrix.
CalcDeterminant
: calculate the determinant of a matrix.
GetMinor:
Get minor matrix.

Read the rest of this entry »


Fast calculation of integral image in matlab

May 21, 2008

After implementing a mex-files for calculating integral image in matlab, a friend of mine told me this can be easily done with the cumsum function in matlab.

intImage = cumsum(cumsum(double(img)),2);


SOM (Self-organizing map) code in matlab

May 8, 2008

Please visit my page to download complete code and training dataset.
https://sites.google.com/view/chi3x10/home

 

Below is the code of applying SOM on handwritten digits recongnition. It was implemented for a homework assignment in a course offered by professor Paul Gader. I used only ten handwritten digits images provided by Dr. Gader. Each image is resized to 30×30 and reshape to a 900×1 column vector. data in the code below is the training set. It’s a coolection of 900-dimensional column vectors.

Read the rest of this entry »


Be aware of memory alignment of IplImage in OpenCV

May 7, 2008

The imageData of IplImage is aligned 4 or 8 bytes in order to enhance the processing speed. For example, let’s say you have a color image of size 98-by-98 (pixel depth 8 bits ) and imageData is aligned 4 bytes. The size of each row will not be 98×3=294 bytes but 296 bytes. The widthStep of IplImage shows the actual size of aligned image row in bytes. This can help us access imageData correctly. Here is a code that copy imageData to a user-created buffer.

//
IplImage *colorImage;
// load an color image
colorImage = cvLoadImage("SL_0230.jpg");
// create a temp buffer
unsigned char *buffer,*temp2;
buffer = new unsigned char[colorImage->width*colorImage->height*colorImage->nChannels];
temp2 = buffer;

// pointer to imageData
unsigned char *temp1 = (unsigned char*) colorImage->imageData;
// copy imagedata to buffer row by row
for(int i=0;i<colorImage->height;i++)
{
    // memory copy
    memcpy(temp2, temp1, colorImage->width*colorImage->nChannels);
    // imageData jump to next line
    temp1 = temp1 + colorImage->widthStep;
    // buffer jump to next line
    temp2 = temp2+ colorImage->width*colorImage->nChannels;
}
// ......................
//.....

Calculating principal components of data with VERY HIGH dimensionality

March 14, 2008

Suppose we have a set of data X with dimensionality D and total number of data is N. You might encounter insufficient memory problem when D is a large number or simply waste too much time calculating those redundant components when D >> N. Using singular value decomposition can prevent those problems.

From singular value decomposition, we know that a D-by-N matrix X can be decomposed as X = USV^T, where U is composed of the eigenvectors of XX^T and of the size D-by-D and V is composed of the eigenvectors of X^TX and of size N-by-N. Because V^TV is an identity matrix(column vectors in V are orthonormal), the first equation can be written as XV = US. From the question, the first m (m<=N) columns of matrix U are actually the principle components of X. We can actually get the first effect m columns of U without actually calculating XX^T. Here is how it works. Read the rest of this entry »


Histograms of two sets of data with different color in matlab

March 10, 2008
figure;
hist(data1);
hold on;
%//make data1 red
%//get the handle of the bars in a histogram
h = findobj(gca,'Type','patch');
%//color of the bar is red and the color of the border
%// of the bar is white!
set(h,'FaceColor','r','EdgeColor','w');
%//data 2 use default color!
hist(data2);

Edit: 03/30/09

As suggested by Adam in the comments, there is a better way to achieve that. Thanks Adam!!
_______________________________

The variable h contains handles to each of the histograms. Just use “set” on each element individually.

e.g.:

hist(data1);
 hold on;
 hist(data2);
 hist(data3);

h = findobj(gca,’Type’,’patch’);
display(h)

set(h(1),’FaceColor’,’r’,’EdgeColor’,’k’);
set(h(2),’FaceColor’,’g’,’EdgeColor’,’k’);
set(h(2),’FaceColor’,’b’,’EdgeColor’,’k’);