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

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

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

Bash script to run in clusters


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

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

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

November 12, 2008

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

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)
    title(['frame# ' num2str(i)]);
    drawnow; %// this is why it works

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. 

%//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)...
            Ey(y,x,k) = (images(y,x,k)-images(y+1,x,k)+images(y,x+1,k)...
            Et(y,x,k) = (images(y+1,x,k+1)-images(y+1,x,k)+images(y,x,k+1)...

    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+...
                Vybar = (Vy(y-1,x)+Vy(y,x+1)+Vy(y+1,x)+Vy(y,x-1))/6+...

                %// 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;

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

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 »