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.

May 16, 2011

Visualizing Genetic Algorithms 1

I've started working on a Genetic Algorithm visualizer tool. The primary goal is to visualize every individual (suboptimal solution) in every generation till termination of the algorithm. Each individual is assigned an unique ID which, along with the individual's parents' IDs can be seen on top left corner of each icon. The number at the bottom left corner is the fitness value of the individual. Apart from this, average fitness, best fitness of every generation are also plotted. Using C++ and OpenGL to build the visualizer tool.



In the picture above, I'm trying to breed a regular pentagon starting with initial population of 20 randomly generated pentagons. The fitness function used here is the standard deviation of distances of the vertices from a point inside the pentagon. Here we can see the 11 generation of pentagons on the same page. I will be posting more updates.

Mar 13, 2011

Stranded turtles on Teknaf beach

Some stranded turtles were spotted on Teknaf beach during my last expedition to the area on 3rd March 2011.





Mar 10, 2011

Fiddler Crab Handedness Detection

Fiddler Crab is the common name for the semi-terrestrial crabs of the genus Uca. The males have an oversized claw or cheliped. If the large fiddle claw is lost, males will develop one on the opposite side after their next molt.

I recently stumbled upon a solution for determining handedness of a male fiddler crab from its photograph.



The procedure assumes that the photo shows front side of the crab and the fiddle claw is horizontal or nearly horizontal in the photo. The basic idea is to find out the fiddle claw and the hole formed between two pincers of the claw. From determining the center of mass of these two regions on the 2D image, it can be detected whether the crab is left-handed or right-handed.

The photo was shot last year during an expedition near the mangrove area of Reju Khal in Cox's Bazar.

Mar 7, 2011

Generation of Mountain Ranges by Modifying a Controlled Terrain Generation Approach



A modification and extension of the parametrically controlled terrain generation approach produces mountain ranges of predefined shape with peaks at specified coordinates.



Like the original algorithm, the parameters can be tweaked to generate craters instead of peaks and other interesting geographical features.



The work was published in ICCIT 2008, Khulna bearing title Generation of Mountain Ranges by Modifying a Controlled Terrain Generation Approach

Automatic Species ID : Lepidochelys olivacea



The automatic species ID procedure I'm working on, follows these steps of image manipulation and decision making to detect Lepidochelys olivacea from a photo. For demonstration I'm using an image contributed by M.A. Hannan.

1. Convert RGB image to Lab color space
2. Enhance 'b' channel of the image


3. Complement 'b' channel of the image


4. Threshold complemented 'b' channel of the image with a predefined constant, 210 for this image


5. Dilate the resulting image


6. Fill the resulting image


7. Erode the resulting image


8. Mark the blob with maximum area


9. If area of the marked blob is not less than 30% of the image size then declare detection of LO

Feb 28, 2011

Automatic Species ID : Chelonia mydas



The automatic species ID procedure I'm working on goes through the following steps of image manipulation and decision making to detect Chelonia mydas from a photo. For demonstration I'm using an image contributed by Rachel Ruzgis to the image database of www.seaturtle.org

1. Convert RGB image to Lab color space

2. Threshold 'a' channel of the image with a predefined constant, 100 for this image

3. Fill holes

4. Erode image
5. Mark the blob with maximum area

6. If area of the marked blob is not less than 20% of the image size then declare detection of CM

Jan 18, 2011

Gasteracantha hasseltii : Hasselt's Spiny Orb Weaver in Lawachhora

Gasteracantha hasseltii

This interesting yellow and black spider with three pairs of spines on its back is called Gasteracantha hasseltii or Hasselt's Spiny Orb Weaver. Found several specimens of this species in my last expedition to Lawachhora forest. It is typical of this spider to have elongated middle pair of spines unlike Gasteracantha arcuata in whose case it is the last pair that is elongated.

Gasteracantha hasseltii

Dec 30, 2010

Video of Information visualization workshop at SUST

This is a video clip of me teaching a class at the Information visualization workshop in SUST at 28th February 2010. This clip only shows Halo among the many information visualization techniques I discussed about.



Hope this will be helpful to my students. Apologies for not so good videography.

Lab color model is preferred for computer vision

For working with computer vision and image processing, I prefer using Lab to other popular color models like RGB and CMYK. Unlike the RGB and CMYK color models, Lab color is designed to approximate human vision. It aspires to perceptual uniformity. Perceptually uniform means that a change of the same amount in a color value should produce a change of about the same visual importance. The L compoent in Lab closely matches human perception of lightness while a and b components represent stimulation along the color opponent dimensions of red-green and blue-yellow respectively.



The Lab color space is a color-opponent space. The color opponent theory states that the human visual system interprets color information by processing signals from cones and rods in an antagonistic manner. The three types of cones have some overlap in the wavelengths of light to which they respond, so it is more efficient for the visual system to record differences between the responses of cones, rather than each type of cone's individual response. The opponent color theory suggests that there are three opponent channels: red versus green (a component in Lab), blue versus yellow (b component in Lab), and black versus white (L component in Lab). Responses to one color of an opponent channel are antagonistic to those to the other color.