Showing posts with label MATLAB. Show all posts
Showing posts with label MATLAB. Show all posts

Mar 27, 2012

Matlab Cell Array

Cell arrays is a tabular data structure in Matlab, useful for storing heterogeneous objects whereas arrays can hold only homogeneous objects. Elements in cell array can have different dimensions as well. Elements of cell array, called cells, can contain
  • numeric arrays
  • strings
  • structures
  • cell arrays
Following example code creates and populates a cell array :

A = {['1st element'] [2 3] [8 9 7; 1 2 3] ['a' 'b']};


Index is written within curly braces e.g. A{1} to access the elements of a cell array. To recursively display the contents of a cell array, celldisp function can be used. For example, celldisp(A) produces the following result


A{1} =

1st element


A{2} =

2 3



A{3} =

8 9 7
1 2 3



A{4} =

ab



The contents of a cell array can be visualized with cellplot function, cellplot(A) would produce the following visualization.



This entry on mathworks blog can be consulted for farther information.

Jul 26, 2011

Algorithm for Handedness Detection of Fiddler Crabs

Members of the genus Uca are generally known as Fiddler crabs for the overgrown claw of the males. The following algorithm detects a male fiddler crabs handedness from a photograph. The algorithm was implemented using Matlab R2009a.


Nov 9, 2010

Histogram smoothing with erosion

The idea of using erosion for smoothing histogram is actually borrowed from the realm of artificial terrain generation where it is a common practice. Recent experiments shows this is a better approach then using polynomial fit. Histogram is a one dimensional height map, after all. The result (smoothed histogram) closely follows the actual data points most of the time and structural property like rises and falls are well preserved.



The procedure is pretty straight forward, replace each entry in the vector containing the histogram with mean of that entry along with n entries backwards n entries forward. Boundary case arises when the index of the current entry is bellow n or beyond length_of_vector-n, I chose to keep those entries unchanged.


result = arr ;

for i=n+1:length(arr)-n
sum=0;
for j=i-n:i+n
sum = sum + arr(j) ;
end
result(i) = sum/(2*n+1);
end

Oct 29, 2010

Smoothing image historgram in MATLAB

%load rgb image
src = 'C:\rainbow.jpg';
rgbI = imread(src);
grayI = rgb2gray(rgbI);

%get image histogram
[counts bins]=imhist(grayI);

%fit histogram into polynomial to get smooth curve
fit = polyfit(bins,counts,10);
fit_samples = polyval(fit,bins);

%show histogram
plot(bins,counts,'.g',bins,fit_samples,'-b');


Image histogram in MATLAB


%load rgb image
src = 'C:\rainbow.jpg';
rgbI = imread(src);
grayI = rgb2gray(rgbI);

%get image histogram
[counts bins]=imhist(grayI);

%show histogram
bar(bins,counts);


RGB to Lab color transformation in MATLAB


%load rgb image
src = 'C:\rainbow.jpg';
rgbI = imread(src);

%convert to lab
labTransformation = makecform('srgb2lab');
labI = applycform(rgbI,labTransformation);

%seperate l,a,b
l = labI(:,:,1);
a = labI(:,:,2);
b = labI(:,:,3);

figure, imshow(l) , title('l');
figure, imshow(a) , title('a');
figure, imshow(b) , title('b');


The input image


and the output







image courtesy : Ashraful Alam

Directory listing in MATLAB

For listing files in a directory

dirfullname = 'C:\Documents and Settings\';
fileFolder = fullfile(dirfullname);
dirOutput = dir(fullfile(fileFolder));
fileNames = {dirOutput.name}'


To filter files, use the second argument of fullfile function.

dirOutput = dir(fullfile(fileFolder,'*.jpg'));