Archive for February, 2011

How to install Greg Mori’s superpixel MATLAB code?

February 28, 2011 11 comments

This short note aims to show you how to use superpixel code from Greg Mori whose codes are observed to have very good results and used by a bunch of computer vision researchers. However, the installation process can be challenging sometimes ^_^, so I figured it’d be nice if I document the process so that it will be easier for absolute beginners to use the code, and more importantly…I can come back to read when I forget how to do it.

I have MATLAB R2010a installed on my Ubuntu 32-bit 10.04 LTS – the Lucid Lynx

I download Mori’s code, extract the zipped file to a folder called superpixels. The folder is located at

Next I download the boundary detector code from the link
I extract it to a folder called segbench, then I put it inside the superpixels
Here are the instructions of the folders

README (Mori’s)

– Run mex on *.c in yu_imncut directory
– Obtain mfm-pb boundary detector code from
– Change path names in sp_demo.m and pbWrapper.m
– Get a fast processor and lots of RAM
– Run sp_demo.m

README (segbench’s)
(1) For the image and segmentation reading routines in the Dataset
directory to work, make sure you edit Dataset/bsdsRoot.m to point to
your local copy of the BSDS dataset.

(2) Run ‘gmake install’ from this directory to build everything.  You
should then probably put the lib/matlab directory in your MATLAB path.

(3) Read the Benchmark/README file.

According to the README instruction
– Run mex on *.c in yu_imncut directory
I run mex on all the .c file in the folder
I don’t know why the command mex *.c does not work, so I have to run mex on every file one by one. Each time I run mex, I will get message
Warning: You are using gcc version “4.4.3-4ubuntu5)”.  The version
currently supported with MEX is “4.2.3”.
For a list of currently supported compilers see:
However, it seems to work fine since I can see all the .mexglx files show up in the folder. So I assume I do it correctly and go on the next step.

In this step,
– Obtain mfm-pb boundary detector code from
I got the code already, so I follow the README (segbench’s). Firstly, I do
(1) For the image and segmentation reading routines in the Dataset
directory to work, make sure you edit Dataset/bsdsRoot.m to point to
your local copy of the BSDS dataset.
So, I go to the file /home/student1/MATLABcodes/superpixels/segbench/Dataset/bsdsRoot.m and change the root to
root = ‘/home/student1/MATLABcodes/superpixels’;
which contains the image I want to segment, “img_000070.jpg”

Next, I do (2) in README (segbench’s)
(2) Run ‘gmake install’ from this directory to build everything.  You
should then probably put the lib/matlab directory in your MATLAB path.
Now at the folder, student1@student1-desktop:~/MATLABcodes/superpixels/segbench$
we need to make MATLAB seen in this folder, so we export the MATLAB path
student1@student1-desktop:~/MATLABcodes/superpixels/segbench$ PATH=$PATH:/usr/share/matlabr2010a/bin
student1@student1-desktop:~/MATLABcodes/superpixels/segbench$ export PATH
then use make install, this time I got quite a long message in the terminal
student1@student1-desktop:~/MATLABcodes/superpixels/segbench$ make install
Then you will notice some files in the folder
What you have to do here is to addpath in MATLAB by typing in the command window

Next, (3) Read the Benchmark/README file. I found that we don’t have to do anything in this step. So just skip this.

Now it’s the last step
Change path names in sp_demo.m and pbWrapper.m
so, go to the folder /home/student1/MATLABcodes/superpixels and change the path
in pbWrapper.m I make the path pointing to ‘/home/student1/MATLABcodes/superpixels/segbench/lib/matlab’
in sp_demo.m I make the path pointing to

Now run the file sp_demo.m. Unfortunately you will get some error messages because of a function spmd. This happens because MATLAB 2010a has function spmd of its own which has the number of input argument different from that of spmd from the toolbox. One way to get around this is to change the name of spmd.c in the toolbox to spmd2.c, then compile spmd2.c using mex spmd2.c. Then replace spmd(…) with spmd2(…). If you encounter more errors from this point on, don’t panic, because it’s probably from this spmd issue, so just do the same thing and it will work fine.

That’s it! Enjoy Greg Mori’s code!


For windows users, please refer to Thanapong’s blog, whose URL is given below:

Image Segmentation using Gaussian Mixture Model (GMM) and BIC

February 27, 2011 3 comments

A while ago, I was so amazed about the image segmentation results using Gaussian Mixture Models (GMMs) because GMM gives pretty good results on normal/natural images. There are some results on my previous post. Of course, GMM is not the best for this job, but hey look at its speed and easiness to implement–it’s pretty good in that sense. However, one problem with GMM is that we need to pick the number of components. In general, the more component numbers we assume, the better log-likelihood it would be for GMM. In that case, we would simply send the number of components to infinity, right? Well…but there is nothing good come out of that because the segment would not be so meaningful–in fact, we overfit the data, which is bad.

Therefore, Bayesian Information Criteria (BIC) is introduced as a cost function composing of 2 terms; 1) minus of log-likelihood and 2) model complexity. Please see my old post. You will see that BIC prefers model that gives good result while the complexity remains small. In other words, the model whose BIC is smallest is the winner. Simple as that. Here is the MATLAB code. Below are some results from sweeping the number of components from 2 to 10. Unfortunately, the results are not what I (and maybe other audiences) desire or expect. As a human, my attention just focuses on skier, snow, sky/cloud and perhaps in the worst case, the shadows, so the suitable number of components should be 3-4. Instead, the BIC assigns 9-component model the winner which is far from I expected. So, Can I say that the straightforward BIC might not be a good model for image segmentation, in particular, for human perception? Well…give GMM-BIC a break– I think this is too early to blame BIC because I haven’t use other more sophisticated features like texture, shape, color histogram which might improve results from using GMM-BIC. The question is what are the suitable features and the number of components that makes the segmentation results using GMM-BIC similar to human perception? MATLAB code is made available here.

original image

original image

Plot of BIC of model using 7-10 components

Plot of BIC of model using 7-10 components

Setting Remote Desktop to Ubuntu from Windows

February 24, 2011 Leave a comment

To set the remote desktop to Ubuntu from Windows machine, there are 2 parts;

1) Setting the remote desktop on Ubuntu machine; the details can be found here. After setting that, you will get IP address of the Ubuntu machine.

2) Setting VNC on Windows machine. I use realVNC 4.1.3 [link] which is free, and I just use the default parameters. Using the IP obtained from 1) to connect to the Ubuntu machine.

That’s it.

Categories: Tutorials Tags: ,

Irregular Tree Structure Bayesian Networks (ITSBNs)

February 24, 2011 Leave a comment

Irregular Tree Structure Bayesian Networks (ITSBNs)

This is my on-going work on structured image segmentation. I’m about to publish the algorithm soon, so the details will be posted after submission. ^_^ Please wait.

<details will be appeared soon>

Here are some results

original image

original image

segmented using GMM

segmented using GMM

segmented using ITSBN

segmented using ITSBN

How to use the ITSBN toolbox

  1. Install Bayesian networks MATLAB toolbox and VLFeat. Let’s say we put them in the folders Z:\research\FullBNT-1.0.4 and Z:\research\vlfeat-0.9.9 respectively.
  2. Download and unpack the ITSBN toolbox. Let’s say the folder location is “Z:\research\ITSBN”. The folder contains some MATLAB files and 2 subdirectories 1) Gaussian2Dplot and 2) QuickShift_ImageSegmentation
  3. Put any color image to be segmented in the same folder. In this case, we use the one from Berkeley image segmentation BSDS500 and the folder is ‘Z:\research\BSR\BSDS500\data\images\test’
  4. Open the file main_ITSBNImageSegm.m in MATLAB and make sure that all the paths pointing to their corresponding folders:
    1. vlfeat_dir = ‘Z:\research\vlfeat-0.9.9\toolbox/vl_setup’;
    2. BNT_dir = ‘Z:\research\FullBNT-1.0.4’;
    3. image_dataset_dir = ‘Z:\research\BSR\BSDS500\data\images\test’;
  5. Run main_ITSBNImageSegm.m. When finished you should see folders of segmented images in the folder ‘Z:\research\ITSBN’.

Which is better…(int* nPtr) vs (int *nPtr)?

February 24, 2011 Leave a comment

Compare 2 functions in the main.cpp

#include <iostream>

using std::cout;
using std::endl;

int fn_CubeByValue(int);
void fn_CubeByReference(int *);

int main()
	int number, result;
	number = 5;

	cout << "number =" << number;
	result = fn_CubeByValue(number);
	cout << "\n after result = fn_CubeByValue(number);, number =" << number;
	cout << "\n, result =" << result;

	cout << "========================================";
	cout << "\nnumber =" << number;
	cout << "\nafter fn_CubeByReference(&number), number =" << number <<endl;


int fn_CubeByValue(int n)
	return n*n*n;

void fn_CubeByReference(int *nPtr)
	*nPtr = *nPtr * *nPtr * *nPtr;

In the first function, fn_CubeByValue, if I were to compare

int fn_CubeByValue(int n)


result = fn_CubeByValue(number);

, I would equate int n and number, represented by n <– number.

What if I use the same analogous to the second function, fn_CubeByReference, I would get that  int *nPtr must be equated to &number, that is, *nPtr<–&number which is not semantically true because *nPtr is would take a variable whereas &number is address.

Perhaps it makes more sense to say nPtr<–&number which indirectly conveys that we should write int* nPtr instead of int *nPtr. In which case we can interpret that the input nPtr takes value of type int* (indicating memory address). However, one can argue that another way to interpret int *nPtr is to say nPtr is the input variable taken by the function, but “int” restricts that whatever in “*nPtr” MUST be integer, hence, nPtr is automatically a memory address. In both cases, the address &number is brought into the function via the variable nPtr, that is, nPtr<–&number. I’m not sure which interpretation is better?

Categories: Uncategorized Tags: ,

How to Setup Dual Monitor on Ubuntu

February 21, 2011 Leave a comment

I got a new old monitor from a dumpster, so I want to use this guy as my secondary monitor. I tried several suggestions on internet and found that this is the best explanation for me. Hopefully, this can be useful to you.

Categories: Tutorials

Often-used MATLAB Commands

February 18, 2011 Leave a comment

I’m very forgetful…Hopefully this will help me to fetch some memory when I want to use it.

mkdir -- make a directory
../testdata -- go out one step
save([imagename,'_package'], 'Y', 'Y_index', 'Z', 'evidence_label', 'n_level', 'H');
movefile(['./',imagename,'_package.mat'], ['./',imagename]);


[pathstr, name, ext] = fileparts(filename)

[s,mess,messid] = mkdir('../testdata','newFolder')
pwd -- identify current folder
list all the filename with extension from a directory and its subdirectory. Please visit [link] from stackoverflow.
caxis([cmin cmax])

How do I control axis tick labels, limits, and axes tick locations?
move xtick label to the top

How to plot image icon on the image? I will post my code very soon.

Categories: Reviews