Showing posts with label code. Show all posts
Showing posts with label code. Show all posts

Jun 19, 2013

Static member in Python

Python has different syntax from most common OOP languages like C++ or Java. When declaring variables, the name is not followed by a type identifier or scope modifier. For creating a static member of a class, simply write it inside the class outside of the scope of member functions. To declare a variable belonging to instance, simply add self. prefix.
class Some_class(Name_of_super_class): static_field = 0.0 __init__() : self.variable_belonging_to_instance = 1.0 print("object of Some_class instantiated") @staticmethod def static_method() : print("static method called") static_return = 1.0 return static_return

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.

May 18, 2011

C++ Function Templates

One of the seldom used features of the C++ programming language is type parametrization a.k.a. Template. This often overlooked feature actually enables a separate programming paradigm - Generic Programming. This allows functions and classes to operate on generic data types. Functions and classes can work on many different data types without being rewritten for each type. There are two kind of templates in C++, function templates and class templates.

Templates provide a way to reuse source code. Here they differ from inheritance and composition which provide ways to reuse object code. This also implies that using templates will cost more time in compilation. However, unless the template is a very complex one, the additional time in compiling is negligible.

A function template for finding maximum of two instances of the same type can be coded as following :

template <typename T>
T& max(T& x, T& y)
{
if(y < x)
return x;

return y;
}


This template function can be used for native data types as well as classes provided that the "<" operator has been overloaded for that class. The compiler will replace the T in template code with appropriate types during compilation.

If a template function is required to behave in a certain way for a certain data type, it can be specialized for those data types.

One important point about template functions is that, when working in multiple-file projects, the declaration and definition has to be in the same file.

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


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