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);

.


De Boor’s Algorithm in C++

October 18, 2009

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;
}

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

3D mesh correspondence selection tool

July 31, 2008

This is a program that allows users to manually assign corresponding triangles between two triangular meshes. Currently it only supports triangular-only meshs. To compile the code, run make PROG=hw3c.

User has to first click on a triangle from the model on the left window and select its corresponding triangle from the model on the right. The index of corresponding facets will be stored. Press ‘w’ to save the correspondences into file “cr.txt”. You will have to backup the old cr.txt yourself.

User can rotate each model individually by moving the mouse and hold the right mouse button and zoom in/out by using the mouse wheel. If the mouse wheel is not responding, use key ‘z’ and ‘x’ to zoom in and out. Press ‘Esc’ to exit the program.

Source code can be downloaded here.

Screeshot of the program

Screeshot of the program


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