Friday 12 June 2015

Object Tracking using MATLAB.

This might sound like a tough one but believe not that much of a rocket science.

I wore a red cloth on my finger and took a video of me moving the finger.(Still don't have webcam hardware support; do share me a link to download this externally, not from MATLAB site because my software is cracked). This video was fed in MATLAB by following the examples in documetation of how to take in video inputs using VideoReader() function. It makes a video object from which frames can be extracted in a structure in the following way,

















So what we do here is create a structure(which is analogous to C structures) which contains frames in fields called 'cdata' while its colormap in 'colormap'. I am working with a RGB image so it doesn't need colormap. A loop is run until no frames are left in the object and structure stores frames in it during every iteration.

This makes our frames ready to be image processed. I took out the R, G and B frame separately from every frame(RGB images are formed by stacking three layers of Red, Green and Blue images). But just picking high Red value from a frame doesn't define it Red. Say for example, white has RGB components (255,255,255); so all my white colors will also be segmented, which I don't want.

A red component means high R, low G and low B component. Use impixelinfo on a frame to see the practical values.

imshow(s(150).cdata); impixelinfo;

Keeping the values in mind, threshold all three frames and then add them up to show only red regions of frame.


Now it's time to filter the minor noises using connected component analysis as did in Sudoku Solver project and dilate the remaining red component to form a good shape. Run all this in a for loop till the last structure element arrives and keep outputting image using imshow() and pause(0.01) functions. Pausing is necessary to see the video.

Now all you have to do is take the mean of all white pixels and show shape on its gray-scale image.
Happy object tracking!
The following video is a demonstration of the object-tracking. The yellow spot on finger follows the finger in x and y both, while the bar on left takes only y components. I aim to use this to play a ping-pong game.

No comments:

Post a Comment