Wednesday, December 14, 2011

Use of Lambda, Map, Reduce and Filter in Python

I have heard a lot about Lambda on Python.

I was scared in the beginning as I thought the code was very cryptic until very recently explored what it is all about.

Lambda is a anonymous function that one can write for quick operations instead of formally writing a function using def.

It can be used in conjunction with other functionality like Map, Reduce, Filter in Python.

Examples of Lambda are given below.

Map, Reduce and Filter functions in Python make writing code much more easier (less lines of code) and I think they are optimized internally which will make them more faster than writing custom code which will most probably be sub-optimal in terms of performance.

Here is a example of Matrix Multiplication (along with other optimizations) making use of Map along with some.

Now lets see what Map, Reduce, Filter is and how Lambda can be used with them.

  • Map functionality in Python i.e. with Syntax map(function, sequence) takes a sequence of values and passes it to function and returns a list of items.
  • Filter functionality in Python i.e. with Syntax filter(function, sequence) takes a sequence of values and passes it to function and returns a list of items for which function(item) is true.
  • Reduce functionality in Python i.e. with Syntax reduce(function, sequence) takes first 2 items in sequence, processes them and takes result, next item and so on.

Sunday, December 11, 2011

KMeans and Vector Quantization

KMeans clustering is a type of Vector Quantization ("Since vector quantization is a natural application for k-means, information theory terminology is often used."[1] ).

From Machine Learning perspective, feature vectors can be considered as code-word and the table mapping code-word to cluster ids is a codebook.

Each feature vector to be clustered is considered as a code-word and in KMeans clustering, they are assigned to the closest centroid (cluster id).

Pages describing Vector Quantization - KMeans:

  1. Vector Quantization
  2. Documentation on K-means clustering and VQ in Python
  3. KMeans in OpenCV using C++.

Thursday, December 8, 2011

Interesting talk on Robotics and Artificial Intelligence

This is an interesting talk by Dr. Andrew Ng at Stanford University on Future of Robotics and Artificial Intelligence. He talks about present problems in Robotics and Artificial Intelligence.

Saturday, November 26, 2011

Action Recognition Survey

The paper titled "Machine Recognition of Human Activities: A survey" is an interesting Journal Paper I've read recently.

It basically talks about the recent advances, open research problems, future directions of research in Action Recognition and different techniques employed to do Action Recognition.

Friday, November 25, 2011

Image Overlap in Android OpenCV

Its been recently that I have been playing around with OpenCV on Android.

Btw, My other programs in OpenCV will be posted here.

Basically, I've written a program that overlaps two images. One image exists in the "res" folder in android project and other image is the Image coming from the camera.

Image coming from the camera is available in array of bytes. We convert it into a Bitmap, then into a "Mat". The other image is read as a "Mat" directly using Utils.loadResource method.

Now since both images are in Mat format, I use Mat.getNativeObjAddr() method from android-opencv project to pass the image to JNI code. This is more useful as we can reconstruct cv::Mat in JNI code more easily than passing bytes (lookup samples from android-opencv project) and reconstructing them.

Also, one thing to take care in number of channels in the images/Mat being processed. We need to make sure that all the images being operated on should be of same number of channels. During this process, you might have to convert RGB to Gray or RGB to RGBA, etc.

In the sample program, I've written a code to send RGBA image, RGB image to JNI (OpenCV C++) code and then convert RGB image to RGBA using cvtColor function. Then do a "addweighted" function call on the 2 images and return it to Java code.

I got the following image on my App where I performed merging  of two images:

We can use more intense image processing stuff in C++ which is more faster than it is on Android (java).

If you are looking for the program to merge images in OpenCV, take a look at this program.

Sunday, November 20, 2011

Hidden Markov Models for Dummies

This article is a great collection of the best resources available on the web which explain Hidden Markov Models and their applications.

I think there is never a "best place" to learn all the points of a new concept/idea. But, you need to go through a lot of sources (books, webpages, jounals, etc) to understand something.

If you are done with learning what HMMs can do, you can checkout this post which discusses about selecting optimal parameters for training of a HMM classifier.

  1. Here is a really good explanation of what HMMs are all about. You need some background (Bayesian Networks) with Probability though. The above website Autonlab (from Carnegie Mellon University) has really good explanation/slides for Data Mining, Machine Learning, Pattern Recognition which is helpful to Math, Statistics, Computer Science researchers.
  2. Here is another link (from University of Otago, they make really good tutorials which start from basics) that explains HMMs with basics of Probability and Naive C/C++ implementation.
  3. Here is another link from UC-Berkley which explains Hidden Markov Models (in Practical Machine Learning class). This one clearly states learning, testing/classification without much deep explanation (on how they came up with the equations) and with quick formulae to start with. This is very much unlike other tutorials on the web which seemed to me confusing.
  4. Another tutorial here (Utah State University) describes HMM and also talks about issues in implementing HMM (floating point underflow problem).
  5. Matlab documentation website does a good job in explaining Hidden Markov Models in a basic manner along with the code (to use this code, you need "Statistics" toolbox in Matlab).
  6. This article (University of Cambridge) compares Hidden Markov Models with Dynamic Bayesian Networks. Also covers other stuff on Computer Vision applications using these Stochastic Models.
  7. Most referred tutorial in writing other tutorials is "A tutorial on Hidden Markov Models and selected applications in speech recognition".

Here are set of videos on Youtube that explain Hidden Markov Models in a more mathematical way! Other videos from that guy have excellent Machine Learning examples explained very mathematically and clearly!

There are 3 problems to solve in Hidden Markov Model namely, State EstimationDecoding or Most Probable Path (MPP) and Training/Learning HMM.

The above 3 problems are solved using the following techniques:

  1. State Estimation: Forward-Backward technique is used for State Estimation (what will be the next state, given set of observations). This step is also known as Classification. This can be used in testing the classifier built.
  2. Decoding or Most Probable Path: Viterbi Decoding technique is used for estimating Most Probable Path (given a set of observations, what is the most probable path that is taken that best explains the observations). This step is also known as Decoding. This can be used in Testing the classifier built.
  3. Training/Learning HMM: Baum-Welch (Expectation Maximization) technique is used for Learning HMM. If we are given a set of observations, we can predict the maximum likelihood HMM that may have produced the observations (adjust the HMM model that fits the data). This is also known as Training.

For Computer Vision guys, these slides explains how to apply it to action recognition.

Once you are comfortable with basics of HMM, you might want to look into this paper. Here authors describe on how to select initial parameters of HMM.

A background in Statisctical Pattern Recognition, Stochastics will definitely help in understanding Hidden Markov Models. Hidden Markov Models are widely used in Speech Recognition, Computer Vision (Gesture Recognition and Action Recognition).

There are 3rd party libraries available on the web for use in your project. Also, there is a Matlab toolkit available

Wednesday, November 16, 2011

Move to the Cloud

This is an interesting article and video from UC Berkley, concerning Cloud Computing. It discusses about the need for Cloud for building Scalable Services, opportunities for Startups, advantages of Cloud, concerns about Cloud Computing!

It also talks about present concerns about Cloud Computing, possible solutions. Its basically the way UC Berkley would want to see the Cloud Services evolve.

Wednesday, November 9, 2011

Possibilities with Array Cameras

This is a interesting video on usage of Array Cameras to avoid Occlusions. Array Cameras are a set of Cameras whose Images are Co-Planar .There are a lot of possibilities with usage of Array Cameras in Surveillance, Occlusion Avoidance.

The link for this research paper from UCSD is here.

Wednesday, November 2, 2011

Apple's Siri and Android's Voice Control

Its been a while since Apple's iPhone 4S was released and everyone going GAGA over Siri. It seems very funny looking over claims of Apple fanboi's to be able to talk to their phones and it replying it back. That feature has been in Android for almost an year.

However, Siri is absolutely amazing.. Apple has once again shown us that better implementation always attracts positive attention.

Presently, Android's voice feature is not as extensive as of Siri. For example, I can say "weather in Morgantown" in Android and it will give me a weather prediction on a Google search page. Siri will give me the weather directly by speaking to me (which is really cool).

We need to specify the input properly in Android. For example I can't say "drive to Pittsburgh" in Android (this opens up searching "drive to pittsburgh" in google search) rather, I would have to say "Navigate to Pittsburgh" which is not so natural (to me).

However, translate feature is awesome on Android.

Android 4.0 features updated text to speech feature, making the API more cleaner. I think this move will encourage more developers to bring in more apps to Android market. Previously, I believe developers would have to write it in C++ (unofficial API), now there is no need for that.

No matter, Siri is a great feature but I'm a little skeptical over its success as its not a natural interface to talk to phone. Google voice search, and voice dialer are great features on Android which were present since the days of Gingerbread. But, I see very few people talking about it or using it extensively.

Andy Rubin also claims the same. They want to wait and see how the public reception of this feature will be.

But on the other hand, maybe Apple did the right thing by giving voice control in iPhone 4S a name ("Siri") and calling it an personal assistant or it is an marketing tactic made by Apple.

I guess its time for Google to make the same move.

Monday, October 31, 2011

Object tracking survey

This is an old Journal Paper I've read (long time back) on Object Tracking during Literature reivew: "Object Tracking: A Survey"

This has a gist of almost all object tracking methods used till 2006.

Saturday, October 29, 2011

Kinect Depth and Real-world distance

Here is an interesting link where the author compares real world distance with the distance measured with Kinect.

My friend and colleague Sriram, is also working in a similar area. I think we can expect important posts regarding that on

Wednesday, October 5, 2011

Science and Computer Simulation

Here is an interesting link which tries to describe analogy between Science/Scientific Experiments and Computer Simulation/Programming.

  1. An Introduction to Computer Simulation Methods: Amazon Book
  2. Sample Chapters.
  3. Scientific Programming in Java.

I'm glad, someone author wrote it in form of a book (first.

Tuesday, October 4, 2011

Interesting Papers in Distributed Camera Networks

I have read these papers published in the field Camera Networks recently (which I found interesting):

  1. Distributed Coordination for Fast Iterative Optimization in Wireless Sensor/Actuator Networks: Proposed algorithm talks about how wireless sensors come to consensus on an issue in a Fast manner. How does this compare to other methods. (optimzation problem).
  2. Distributed Multi-Target Tracking In A Self-Configuring Camera Network: Talks about a modified Distributed Kalman filter that is used for tracking in Smart Cameras.
  3. Programming Support for Distributed Optimization and Control in Cyber-Physical Systems: Talks about issues regarding programming support for distributed shared memory architecture.
  4. Scalable Target Coverage in Smart Camera Networks: With only Pan changing in a PTZ camera, talks about Camera Coverage problem (which is an optimization problem). Talks about impact of proposed algorithm with changes in density of sensors/cameras, number of targets, etc.

Monday, September 19, 2011

Monday, September 5, 2011

Color spaces in Vision

Here is an interesting article that talks about the pros and cons of using a specific color space for

It talks about YCrCb, Lab, RGB, Normalized RGB,YUV, HSV, other color spaces and their pros and cons with respect to each other.

Thursday, September 1, 2011

Kalman Filter for Dummies

I've been reading about Kalman filter and its application in Tracking.

For those who are finding Kalman Filter tutorials, even the wikipedia page for Kalman Filter highly confusing might just find these links comparatively easy to understand.

These tutorials help to understand the basic idea of Kalman Filter, why it is applied in Vision problems, etc:

  1. Kalman Filter for dummies
  2. Introduction to Kalman Filter (includes basic probability concepts, etc).
  3. A Study of the Kalman Filter applied to Visual Tracking.

Wednesday, August 17, 2011

OpenCV performance tests

This is a good article comparing the performance of common tasks on different versions of OpenCV. Newer version doesn't necessarily mean that it is better in performance.

Here is another link that talks about comparison of OpenCV with other programming frameworks.

Sunday, August 14, 2011

A Simple Read/Write Example using Cassandra and Java

In this post, we take a look at basic CRUD (Create Read Update Delete) operations on Cassandra database using Java JDBC.

We only use Eclipse IDE. This example was tested in Windows 7 Operating System.

To find out more about NoSQL databases, look here.

Cassandra is an open source distributed database management system developed by Apache Software Foundation. It was designed to handle very large amounts of data spread out across many commodity servers while providing a highly available service with no single point of failure.
Popular user of Cassandra is Facebook

To find out more on Cassandra, refer its home site here.

  • Download and Install Cassandra from here.
  • Extract the files.
  • Setup the following Environment Variables.
    • JAVA_HOME (Give the link to the jre folder where java is installed)
    • CASSANDRA_HOME (Give the link to the cassandra folder where the files are extracted)
  • Go to the location where you extracted and then into the "~/apache-cassandra-0.6.6/bin" folder and open 'cassandra-cli.bat' file using any text editor
    Now save the file after editing the line.
  • Now open the command prompt and goto "~/apache-cassandra-0.6.6/bin>" folder and type the following command '.\cassandra.bat'. This starts the server.
  • Now open another command prompt and goto "~/apache-cassandra-0.6.6/bin>" folder and run '.\cassandra-cli.bat'. This is the UI for the database
  • Connect to the server by typing the command 'connect localhost/9160'.
  • Here, type "Create Keyspace AuthDB ;" This creates a cluster (i.e., database)
  • Here, type "Create Column Family AuthCollection ;" This creates a column family (i.e., table)
  • There are certain jar files needed which are available in the Project Dump which is available for download below. It is present in the folder titled 'JarFiles' in the Project Directory.
  • Be sure to add these jar files to the class path.

The code which performs all the CRUD operations is as follows.

The screen shot of the output is as follows:

This code is available for download along with the entire project directory here.

Wednesday, August 3, 2011

OpenCV ROS tutorial

I recently realized that OpenCV tutorials at ROS website is more useful than any others available out there.

Btw, My other programs in OpenCV will be posted here.

You can download it and other related stuff (cheatsheet, etc) at

Tuesday, August 2, 2011

Computer Vision research at Google: papers at CVPR 2011

The following are the links to Google's contribution to Computer Vision at CVPR 2011.

Also its not fair to ignore Microsoft's contribution to Vision Research in CVPR 2011. Microsoft has got about about 20 papers including one of the best papers.

Check this out:

Thursday, July 28, 2011

Point Vectors in OpenCV

I've started exploring C++ API of OpenCV library and use Standard Template Library along with it. I find it very convenient to use.

For example take a look at the following program. We use vectors to store a sequence of "Point" in OpenCV

In OpenCV 2.0 C++ API:

Monday, July 18, 2011

OpenNI API and Kinect Skeleton

Here is the link, which I've used to download, install OpenNI and other API's to do lots of fun stuff with Kinect.

Sunday, July 17, 2011

Android Sensors API tutorial

I found a document that well describes the different sensors available on Android devices and the API to be used to use the sensors.

Here it is:

Its almost an year old. There might be new sensors added already and new API's exposed.

I wanted to share this with you guys...


Monday, July 11, 2011

android screencast to PC

I found this program "Android Screenshots and Screen Capture by alexus-ivanov, etf, mightypocket " on the web (written in Java) that exports the android screen to the PC.

It comes with a executable jar file. If you are using the latest version of the SDK, you may want to copy the adb binary to sdkroot/tools folder in order for this program to work.

This might be actually useful if you were to give an presentation which is stored on a phone or you may want to share your screen onto PC.

After running the progarm, set the screenshots, Android SDK root export folders.

Its slow though.

Screenshot of my phone:

I also found a similar project "androidscreencast" on the web, didn't try it though.

Saturday, July 9, 2011

Why do we need Feature Selection (Pattern Recognition)?

Feature selection process is a very important task in Statistical Pattern Recognition. It significantly increases/reduces the performance of any classification algorithm applied afterwards. I've mentioned a few guidelines which are commonly used.

In Statistical Pattern Recognition, we need Feature selection techniques due to the following reasons:
  • All Features: Selection of all features for Pattern Classification will lead to more error. So, less features the better.
  • Curse of dimensionality: The more dimensions you add to the feature vector for classification, you might encounter more error on the long run. Less features will give sub-optimal results. There is this safe "number of dimensions" which are needed for optimal classification for the given size of a data-set.
  • Complexity, Size: increase of storage, complexity will be the result of a selection of all dimensions. It basically computationally intensive. So, we don't want to be there either.
  • dimension Subset: specific subset of features may give more accuracy. Our goal will be to find those optimal subset of features that try to classify a given data-set.

Sunday, June 19, 2011

A review on "OpenCV 2 Computer Vision Application Programming Cookbook"

I got a chance to review this book last week. This book is for those who are interested in learning Image Processing/Computer Vision programming in C++.

The thing that differentiates this book from the original book written by Researchers at Willow Garage (maintainer of OpenCV) is that this book mainly concentrates on the programming paradigm in C++.

The book "OpenCV 2 Computer Vision Application Programming Cookbook" is for those who want to get started quickly on programming with OpenCV. All the syntax is in C++ and thats a positive point as most of the programming in Industry is done using C++.

This book shows how to extensively use C++ for simple/common tasks required for daily tasks in Computer Vision/Image Processing.

There are sections on "Scanning an image with iterators", "Filtering Images with low pass filters", Segmentation of the image, Fundamental Matrix, Computing Homography, etc.

These can be considered as the sections which were not properly covered in the original "Learning OpenCV" book and yet they are very commonly used.

There is a Chapter on "Processing Images with Classes" which I think many may find useful.

Sections like "How it works" try to explain whats really going on and stuff.

Overall, this can be considered as a really good book to get started with OpenCV programming.

However, there are only two points about this book that dissapoint me.

  • One, there is no proper coverage of Machine Learning algorithms feature of OpenCV (including adaboost, d-trees, clustering, classification etc). 
  • Two, there is no mention of python API of OpenCV. It is important because many people have increasingly started using Python to quickly test their algorithms in OpenCV rather than coding completely in Matlab which is different programming language/environment. 

I can consider this a "Must Buy" as there have been lots of changes in API to OpenCV over the past few years which are not covered by any other book presently available in the market as of now.

You can buy it at the official Packt publishers website.

Friday, June 17, 2011

Configure NDK with OpenCV on Android

Here are the official links to configure environment to compile, write OpenCV apps on Android:

  1. Android SDK, NDK with Eclipse.
  2. Compiling OpenCV, NDK apps.

Thursday, June 16, 2011

MIX on Android @ WVU

Here is how I configured MIX (Mountaineer Information eXpress) mail on my android:

Update: Now that MIX and Master-ID are renamed as My-ID, just enter My-ID details and you will be good.

Go to email app, add a new account. While configuring the account, leave the default settings except for the following:

  1. Name is as MIX (or whatever you want)
  2. username is <your_id> and password is <your_password>
  3. Incoming mail settings, server is "" with appropriate security settings.
  4. Outgoing mail settings, username is <your_id> and password is <your_password>
  5. Outgoing mail settings, server is "" and security as SSL.

There are no articles on the web suggesting the same (except for suggestions on forums), so I've decided to write this article.

Saturday, June 11, 2011

Convert nphmotionjpeg to video or images

Hi all. I've found a work-around to convert streaming video in nphMotionJpeg format into mp4/jpg. I'm sure that this is what some of you might be interested. So, I'm posting this.

Apparently, ffmpeg doesn't seem to do this. Instead,  I made this process work with VLC player and Avidemux only.

There might be some other better way to do it, I'm not sure. But, I found no help on the web regarding the same, so I've written this blog post.

They are explained as follows:

  1. Make sure that open a streaming video on the internet. (Obviously :p)
  2. Right click on the video and click as "Save As"/ "Save Image as". This might take some time for the browser to understand whats going on, as what we are saving is not a image, its a continuous stream instead. If browser doesn't respond after a minute or two retry this step.
  3. Leave the default name/format. It is likely to be "nphMotionJpeg"
  4. Save the file in certain location which is accessible. 
  5. Save the video as long as you want (I've saved as long as the file size becomes 8 GB). More than 8 GB didn't seem to work if i did so.
  6. Now as its a continuous stream and not a file of certain size, its size goes on increasing. If you cancel the download, then the entire file gets deleted. Make another copy of this "downloading" file. You may want to do this number of times possible if you want the latest data.
  7. Now after you made a copy of the currently "downloading" file, cancel the download and the file gets deleted (except for the copies).
  8. Now rename the file as "foo.avi" (apparently, if I name it as ".avi" extension only then this method works).
  9. Open the file in vlc player (windows/linux). Now you have the entire video here.
  10. Pause the video and Click on File->Convert/Stream
  11. "Add" the file that you want to convert.
  12. Select convert, then it asks for which format it needs to be converted.
  13. Select H.264 .mp4 format.
  14. Click on convert and it converts it to the specified format.
  15. Now as the nphMotionJpeg stream is in a readble format, open it in Avidemux (you can use ffmpeg here, but may loose some frames if you don't give the right parameters while converting).
  16. Open the file in Avidemux and it will give a warning ask you to open the file in SAFE MODE. DO NOT open it in SAFE MODE, as you might loose frames. Click on  NO/Cancel and then select OK for indexing.
  17. Go to File->Save->Selection as series of Jpegs.
  18. Give the filename in a particular folder and it will give you a series of jpeg images from the video stream recorded.

Friday, June 10, 2011

Learning Representations for real-world recognition

This is a talk that I found on Google Tech Talk channel on youtube. Check it out

Thursday, June 2, 2011

Kinect on Ubuntu

In this post, we will see how to setup Kinect (Xbox 360)  to work with Ubuntu 10.4 or higher using Freenect

Update: If you plan to develop full fledged Applications with Kinect (Computer Vision, AR apps), you may also want to take a look at OpenNI Api (Apparently, this has wider support and more features in the API).

You will need to install a software/binary called "freenect" which comes with working binaries and header file (for development in C/C++).

Follow the steps mentioned below:

  1. Fire up your terminal and execute the following command:

    sudo add-apt-repository ppa:arne-alamut/freenect

    So basically you are adding the software source to your authorized software sources list
  2. Update your software sources list by executing the following command:

    sudo apt-get update

  3. Install the software using the following command:

    sudo apt-get install freenect

  4. Now, you need to add your user to "video" group using the following command:

    sudo adduser <OS_USER_NAME> video
  5. Now, connect your "Kinect" to your PC and execute the following command:
    also, there are other programs like:
After you execute the following commands, you might see something like as follows:

Along with this application, comes libraries for development. Run your synaptic manager and search for "kinect" and you might see something like this:

So, you just need to include these C++ header along with your programs (C/C++ probably) to develop applications that use Kinect sensor.

Happy Kinect Hacking/Programming.


Wednesday, June 1, 2011

Behavior Subtraction

The following is an interesting paper on behavior subtraction. It basically talks about detection abornal activity in a visual scene.

The authors of the paper seem to detect abnormal activity in a busy area (like an interstate highway) by just performing simple static background subtraction. They add the background subtracted images for each frame over time (spatio temporal) and subtract the default background image to create a behavior image of the area.

Then to detect abnormal activity, they simply subtract the new frame from the behavior image (after setting a threshold).

The idea is pretty intuitive and also very effective.

Tuesday, May 24, 2011

KNN classifier Python

I believe you might have read my previous article on KNN classifier. So, this is the next part of that where we are dealing with implementation of it in Python.

My other machine learning articles will be posted here.

The required data set to run this program can be found here: train.txt and test.txt .


The above data set was DERIVED from the famous Iris Flower dataset .

This program uses Matplotlib and Numpy.

If you have not installed (on Ubuntu OS), install it using the following commands:

sudo apt-get install python-matplotlib
sudo apt-get install python-numpy
sudo apt-get install python-scipy

The program is as follows:



If you observe, you can see that as value of k increases, accuracy increases to some extent and then doesn't increase after that (in fact decreases). 

Please leave a comment if you find this article useful.

Monday, May 23, 2011

KNN classification algorithm

Hi all, in this post we discuss on what 'K- Nearest Neighbors Algorithm' is all about. If you ever come across a classification problem, this solution (K- Nearest Neighbors Algorithm) might be the most simplest of all the classification algorithms you could possibly apply.

Implementation of Algorithm in python is available here.

Consider the following image:

credit: Wikipedia

The idea behind KNN is very intuitive. Assume we have 3 classes to classify (squares, circles, triangles) and we need to classify the test data to one of these classes. So what we do is:

  1. Assume size of k=3.
  2. Select a point from test data (green circle in above image)
  3. Find 3 nearest neighbors.
  4. Assign the test data to the class which occurs the most in the 3 nearest neighbors (here it is 2 red triangles).

Now, let us look the above described method in algorithm form.

In order for the classification to occur:
  • we assume, availability of training data.
  • size of k.

Step1: initialize training data (load them in your program).
Step2: initialize value of k
Step3: for each training data item:
                 for every training data item:
                      calculate distance
                 arrange distances in ascending order and put them in a list.
                 select first k distances in sorted list.
                 assign the class which occurs the most in the sorted list.

  • Intuitive.
  • Simple to implement.
  • Simple to understand.


  • Computationally complex.

Additional info:

Friday, May 20, 2011

Memory Management for OpenCV applications

Memory management in OpenCV is an important task because:
  1. Most of OpenCV coding is done in C,C++ where programmers have to themselves deal with pointers, references (not have dealt with python programming in OpenCV). 
  2. No garbage collection in these languages (destructors in C++ claim to do it, but not sure how efficient it is).
I'm talking about managing memory, where one would have to deal with 1000's of images (video stream, stream of images over the network, etc) in real time.

Update: I would suggest moving to C++ API or Python API of OpenCV instead of C. They have better Memory Management internally.

Btw, My other programs in OpenCV will be posted here.

Things NEVER to do in OpenCV:
  • DECLARE structures, variables in a loop

    instead it should be (assuming function will return the right image):

  • use "create" functions in a loop. (cvCreateImage, cvCreateMat, etc).

    instead, it should be something as:

Things to do:

  • recycling: reuse already allocation memory ( img_src = find_a_face(img_src) )
  • pass by reference instead of pass by value.
  • release images, mats, storage, etc after they are done with ( cvReleaseImage(&img_src) ).
  • try to identify variable that remain constant through out the loop, declare it separately (may be from the constructor).
  • make use of static variable wherever necessary.

Also cool website has some stuff on OpenCV Memory management:

Wednesday, May 18, 2011

Creating a Local Ubuntu Mirror using apt-mirror

The pre-requisites:
  1. Have a running Ubuntu installation.
  • Type the command below in the terminal:
    sudo apt-get install apt-mirror
  • or install the package "apt-mirror" from Synaptic if you prefer a GUI or you can alternatively use aptitude from the command line.
  • Edit the configuration file /etc/apt/mirror.list file. You might find the lines below by default:

    ############# config ##################
    # set base_path    /var/spool/apt-mirror
    # set mirror_path  $base_path/mirror
    # set skel_path    $base_path/skel
    # set var_path     $base_path/var
    # set cleanscript $var_path/
    # set defaultarch  
    # set postmirror_script $var_path/
    # set run_postmirror 0
    set nthreads     20
    set _tilde 0
    ############# end config ##############
    deb maverick main restricted universe multiverse
    deb maverick-security main restricted universe multiverse
    deb maverick-updates main restricted universe multiverse
    #deb maverick-proposed main restricted universe multiverse
    #deb maverick-backports main restricted universe multiverse
    deb-src maverick main restricted universe multiverse
    deb-src maverick-security main restricted universe multiverse
    deb-src maverick-updates main restricted universe multiverse
    #deb-src maverick-proposed main restricted universe multiverse
    #deb-src maverick-backports main restricted universe multiverse
  • This may be rewritten as:

    ############# config ##################
    set base_path    /home/myusername/apt-mirror
    set mirror_path  $base_path/mirror
    set skel_path    $base_path/skel
    set var_path     $base_path/var
    set cleanscript $var_path/
    set defaultarch  i386
    set postmirror_script $var_path/
    set run_postmirror 0
    set nthreads     20
    set _tilde 0
    #set limit_rate 2.5k
    ############# end config ##############
    deb maverick main restricted universe multiverse
    deb maverick-security main restricted universe multiverse
    deb maverick-updates main restricted universe multiverse
    deb maverick partner
    deb maverick main

What I have done in this configuration:
  • base_path: The place where are the downloaded packages go.
  • mirror_path,skel_path,var_path,cleanscript,postmirror_script,run_postmirror,_tilde: Better leave them as they are.
  • defaultarch: When you specify "deb <deb-url>" it'll fetch the URL of this type, or you can explicitly specify which architecture packages to pick up by specifying it as "deb-i386 <deb-url>"
  • nthreads: The number of threads of wget that have to be launched for fetching the mirror contents
  • limit_rate: This variable can be set to limit the download rate of the wget but this is per thread. So it boils down to (20 Threads)*2.5KBps = 50 KBps (Notice the 'B' it's for bytes).
  • I just picked up the deb URL's from my file "/etc/apt/sources.list".
  • I skipped the "deb-src" url's because I wouldn't be needing the sources and it'll just eat up my space. It all depends on the need of  the mirror if you need 'em just include them in the package.
  • deb-urls: These are of the form "deb maverick-updates main restricted universe multiverse". Replace maverick with the codename that you want to mirror, maverick is the codename for Ubuntu 10.10. You can find out which version you are using by the command "lsb_release -a". If you just need the codename use "lsb_release -c". You can mirror any number of archives.
Start Mirroring:
  • I would recommend mirroring the archive as a normal user than as a root user using "sudo"
  • Run the command "apt-mirror" after you're done with the configuration, wait for it to complete
  • If you'd like to detach the command from the terminal you can issue the command "apt-mirror &"
  • Warning: Set the base_path variable only to the folder to which the current user or the user using whom you are trying to run "apt-mirror" has write permissions
Settting it for daily update using a cron job:
  • Edit the file "/etc/cron.d/apt-mirror". This file would be available at your disposal after successful installation of the "apt-mirror" package
  • You'll find some default commented lines you can change it and uncomment or you can insert "@daily myusername /usr/bin/apt-mirror > /home/myusername/apt-mirror/cron.log && /home/myusername/apt-mirror/var/"
  • Adding the above command will schedule a cron job in your operating system such that it'll start that command everyday at midnight which in-turn does the downloading for you and clean up older(if a newer deb package comes)/unwanted(if you change your configuration file after  download and re-run the script) packages.
  • Warning: Try the command manually before adding it to the cron job and check whether the download process is starting successfully and it is having write permissions. This is just a fail-safe
The mirroring is complete. What to do now?
  • Install Apache http server using the command "sudo apt-get install apache2".
  • Now all you have to do is create a symbolic link in the /var/www folder which is the root of apache2 web server in Ubuntu:
    • I'll create a new folder in /var/www with name ubuntu by using the command "sudo mkdir /var/www/ubuntu".
    • Now browse to that folder using "cd /var/www/ubuntu" and create symbolic links to mirrors by issuing the commands below:
      ln -s /home/myusername/apt-mirror/mirror/ archive-canonical
      ln -s /home/myusername/apt-mirror/mirror/ archive-ubuntu
      ln -s /home/myusername/apt-mirror/mirror/ extras-ubuntu
    • I've used the names archive-canonical, archive-ubuntu, extras-ubuntu only for my reference you are free to use whichever names you desire.
    • The folders under apt-mirror/mirror such as and etc are there because you have specified them in your configuration file i.e., mirror.list file. Suppose you specify a deb url as "deb maverick main" the script will create a folder under the apt-mirror/mirror directory in our case or under the path where you've specified the "mirror_path" variable.
  • Well ah, that is all for the configuration.
The final step using the mirror to update the packages:
  • Pre-requisite: Make sure that your port 80 is open.
  • Edit the /etc/apt/sources.list file in the host machine from (use the below only as an example).

    deb maverick partner
    deb maverick main
    deb maverick-security main restricted
    deb maverick-security universe
    deb maverick-security multiverse
  • To the following:
    deb http://localhost/ubuntu/archive-canonical/ubuntu maverick partner
    deb http://localhost/ubuntu/extras-ubuntu/ubuntu maverick main
    deb http://localhost/ubuntu/security-ubuntu/ubuntu/ maverick-security main restricted
    deb http://localhost/ubuntu/security-ubuntu/ubuntu/ maverick-security universe
    deb http://localhost/ubuntu/security-ubuntu/ubuntu/ maverick-security multiverse
  • Replace localhost with an IP address if on a network.
  • Run the commands:
    sudo apt-get update
    sudo apt-get upgrade
  • Well that's should do it your system is having the latest updates(if you've mirrored it recently) installed.

ssh Host key verification failed problem in Linux

I believe sometimes you might encounter the problem of Host key verification while doing an ssh to one your terminals.

You may see something like this:

Then, the most simple solution would be to, just open /home/your_username/.ssh/known_hosts file and delete the contents. Also you might want to re-establish all the ssh connections.

Then the smooth flow of actions would look something like this:

Sunday, May 15, 2011

Setup ieee 1394 camera with OpenCV support

Hi folks,
I'm sure that some of you might have tried to interface a IEEE 1394 firewire camera with Linux but found no success.

This is the way I could use Imaging Source IEEE 1394 camera with Ubuntu 10.4 OS using libdc1394 / Coriander.

Follow the steps:
  1. Plug your camera to the 6 pin IEEE 1394 port on your PC. (Obviously ;) )
  2. Install Libraw1394, Libdc1394 libraries and corriander. 

  3. Open a text file and save the following as bash script:

  4. You will most of the times need to execute the above script before running any program (especially if, something may have gone wrong in your program in runtime or if you have any segmentation fault after running the program, whatsoever). This script makes sure that everything is right for the your libdc1394 program to execute. (you don't have to chmod every time, so its basically, the first 2 lines of code that you want to use).
  5. Write your program in C++/C specification of libdc1394 and execute it.
  6. You can also make sure that your camera is working using coriander (just go to command line 
  7. terminal and execute the command $ coriander to open up coriander).

Code to convert dc1394video_frame_t to IplImage:

(thanks to Rohith Reddy for the following code):

Most of the documentation available on the web for very old versions of libdc1394. For latest documentation and working example programs visit

Old resources/documentation of libdc1394 is available on the following links:

Rahul Kavi.

Wednesday, April 27, 2011

Adaboost and SVM

The following is an interesting study on application of Adaptive Boosting with Support Vector Machine based classifier.

Authors claim better performance on imbalanced data-sets (ranging from 150:500 to 30: 500) when compared to regular SVM based approach. It performs better than Adaboost used with classifiers such as decision trees, neural networks. 

Sunday, April 24, 2011

JQuery Servlet Ajax application

In this post, I am demonstrating a simple ajax application using jquery and java servlets.

My other JQuery posts will be listed here.

In this Sample Project, I am trying to suggest the names of the countries starting with A.

So, if we type more than two letters, than it suggests the names of the countries.

The following is the screenshot for the above application.

First we define a html file as follows:

<%@page contentType="text/html" pageEncoding="UTF-8"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"

        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JQuery Example</title>
            <h3>Ajax Example using JQuery and Servlets</h3>
        <script type="text/javascript" src="jquery-1.5.1.js"></script>
        <script type="text/javascript">
                alert("Thanks for visiting!");
                    $country = $("#name").val();
                    if ($country.length > 1) {
                            url: 'SampleServlet',
                            type: "POST",
                            data: {country:$country },
                            error: function(){
                                alert("Data Error");
                            success: function (data){
                                $("#flag").html("Country Suggestion: " + data) ;
        <form id="sampleform" >
                Enter Country starting with A:  
                <input id="name" class="name" type="text" >                                                    
                <div id="flag"> </div>
                Type Atleast Two Letters *

The above program needs "jquery-1.5.1.js" file which is a JQuery library.

Save this file as .js file. This library should be placed in the same location where the jsp page is present.

This jsp program is calling SampleServlet Program as follows:

 * To change this template, choose Tools | Templates
 * and open the template in the editor.

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

 * @author Sriram
public class SampleServlet extends HttpServlet {
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        PrintWriter out = response.getWriter();
        try {
            /* TODO output your page here
            out.println("<title>Servlet SampleServlet</title>");  
            out.println("<h1>Servlet SampleServlet at " + request.getContextPath () + "</h1>");
        } finally { 

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the
            + sign on the left to edit the code.">
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
processRequest(request, response);

     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        String name = request.getParameter("country") ;
        String result = this.countryList(name) ;
        PrintWriter out = response.getWriter();

    public String countryList(String name) {
        String country[] = new String[5] ;
        country[0] = "America" ;
        country[1] = "Australia" ;
        country[2] = "Argentina" ;
        country[3] = "Africa" ;
        country[4] = "Antartica" ;

        int flag = name.length() ;
        for (int i = 0 ; i < country.length ; i++) {
            if (country[i].substring(0, flag).equalsIgnoreCase(name)) {
                return country[i] ;
        return "Country Not Available in List!!" ;

     * Returns a short description of the servlet.
     * @return a String containing servlet description
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>


The web.xml configuration for the SampleServlet program is as follows:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="" xmlns:xsi="" xsi:schemaLocation=" http://java.sun.

The jsp program atleast two let as parameter and sends it to POST method in SimpleServlet using ajax. There is a callback function which displays the output in an alert box and ultimatly in the jsp page.

There Source code is given as follows. It consists of a war file. Place this war file in the webapps folder of your server and paste http://localhost:8080/Sample/Sample.jsp in the url of the browser.

Saturday, April 23, 2011

Simple JQuery-Servlet Application

In this post, I am demonstrating a simple ajax application using jquery and java servlets.

Name the project as Sample.The jsp program here calls the GET method.

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"

        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JQuery Example</title>
            <h3>Getter Example using Servlets</h3>
        <script type="text/javascript" src="jquery-1.5.1.js"></script>
        <script type="text/javascript">
                alert("Thanks for visiting!");
                    alert($name) ;
                    $.get("SampleServlet", {name:$name}, function(data) {
                        alert(data) ;
                        $("#flag").html(data) ;
        <form id="sampleform" method="POST">
                Enter your Name:  <input id="name" class="name" type="text">  <br/><br/>
                <input class="Submit" name="Submit" type="button" value="Submit" id="Submit"> 
        <div id="flag"> </div>

** The above program needs "jquery-1.5.1.js" file which is a JQuery library.
Save this file as .js file. This library should be placed in the same location where the jsp page is present.

This jsp program is calling SampleServlet Program as follows:

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

 * @author Sriram
public class SampleServlet extends HttpServlet {

    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        String name = request.getParameter("name") ;
        PrintWriter out = response.getWriter();
        out.println("Hello " + name);

    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);

     * Returns a short description of the servlet.
     * @return a String containing servlet description
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>


The web.xml configuration for the SampleServlet program is as follows:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="" xmlns:xsi="" xsi:schemaLocation="">
The jsp program takes a name as parameter and sends it to GET method in SimpleServlet using ajax. There is a callback function which displays the output in an alert box and ultimatly in the jsp page.

There Source code is given as follows. It consists of a war file. Place this war file in the webapps folder of your server and paste http://localhost:8080/Sample/Sample.jsp in the url of the browser.

Download Source Code