Pages

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

Tuesday, 20 September 2016

Hiding data in Images

Here is a still from my favourite movie
The Martian
Now, what if I told you that I have hidden a big message inside this image. Would you be able to find out what it is?

Monday, 6 June 2016

Expectation Maximization for Gaussian Mixture Model in OpenCV

I recently wrote code for Gaussian Mixture Model (GMM) based clustering in C++. As always, I found it much convenient to use OpenCV for manipulating matrices. Although there already exist an implementation of Expectation Maximization-based GMM, I tried to understand it by writing my own implementation.

The basic idea of GMM is to first randomly assign each sample to a cluster. This provides initial mixture model for clustering. This is then optimized using Expectation - or the probability/score of assigning each sample to each component in GMM - and Maximization - or updating the characteristics of each mixture component with the given probability/score . An attractive attribute of GMM is its ability to cluster data that does not have clear boundaries for clusters. This is achieved by having a probability/score for each sample from each cluster component.

Tuesday, 17 May 2016

A Random Walk

It is fascinating to see the use of the word 'random' and its resemblance to one of the most basic ingredients in some computer algorithms. One may ask what is it that makes something random?

- "So you just made a random deal?"
- "Students were randomly chosen to take part in a drama."
- "He figured out that he still had an hour to his departure, so he went for a random walk."

Monday, 9 May 2016

Matlab script for checking and deleting folders

Just putting this simple but extremely useful matlab script for my future self and anyone trying to handle folders using matlab. This script checks all the sub directory within the starting directory and then deletes the one that do not satisfy a given criteria. In my case this was the number of image samples within a folder.

% script for deleting folders with less than a certain number of files
close all
clear all
clc

% count the number of png files
D = dir(' ');


numFoldersOrFiles = size(D, 1);

thresholdFiles = 30;

% skipping the first two which are just . and ..
for i = 3: numFoldersOrFiles
    
    if D(i).isdir
        
        Ds = dir([D(i).name '\*.png']);
        numFiles = size(Ds, 1) / 3;
        if numFiles < thresholdFiles
            
            rmdir(D(i).name, 's');
            
        end
    end
end


% all done :)

Saturday, 16 April 2016

OpenCVKinect 2.0 - Acquiring Kinect depth stream in OpenCV

It has been almost two years since I first wrote the code for OpenCVKinect. It has been really good to know that it has been used by a number of other students/developers at GitHub for collecting and analysing Kinect depth streams in OpenCV. I have had some feedback about a possible bug and some students have asked how they can visualize the depth maps in a better way. So today, after a long time, I am releasing the first official update to this project.


Thursday, 14 April 2016

Particle Filtering - Survival of the fittest

I recently studied dynamic system models such as Kalman and Particle Filters.
For Kalman Filter I followed a Matlab demo that can be found here.

In this demo, the simple problem of tracking a ball is addressed using a Kalman Filter. The input sequence is of a ball, which is travelling at varying velocity and which is occluded in some frames by a box. I think this is a great example to demonstrate the power of dynamic system  models, especially the occluded frames can be used to test how good a dynamic model is. Here is the actual sequence:


As you can see the ball goes underneath the box and comes out of the other end. If our dynamic model is accurate it will be able to predict the state of the ball even when it is not visible, and should match the position when the ball comes out.

Monday, 28 March 2016

Designing an algorithm - from ideas to code

I had always been interested in solving sudoku puzzles, partly because there are too many combinations that make each Sudoku unique. Since my work involves writing and using programming in different scenarios, I thought why not try using my skills on Sudoku. So there I was on a London Underground train to Barbican - looking at a Sudoku puzzle at the back of a morning newspaper, wondering how I can write an algorithm to solve it. I figured out a few simple tricks that I have always used in algorithm design. Here I explain what thoughts I had while designing my very own Sudoku solver and how I transformed those ideas into a working prototype.

First of all lets have a look at a typical Sudoku puzzle and some basic rules:

Sudoku Puzzle
Yes - it has got everything to do with numbers!! lots of numbers!

A Sudoku puzzle typically has 81 boxes where each box can have a number between 1 to 9. However, all these boxes follow some rules that make it all interesting. You may have noticed 3x3 squares grouping the number boxes. A correct solution of Sudoku ensures no repetition of numbers from 1 to 9 inside each of the 3x3 squares, in each horizontal line and each vertical line. When solving a Sudoku puzzle, this is exactly where I look for a solution, and exactly where my thought process starts for my Sudoku solver algorithm.

Sunday, 20 December 2015

Long Exposure Shots with a GoPro and Matlab

I recently got  a GoPro. You know to get cool selfies, videos and all :D I am very much impressed by all the cool things you can do with it but was specifically impressed by the fact that one can create a time lapse video.

After giving a couple of tries to time lapse videos, I wanted to go beyond. I had always seen photographers make a long exposure shots by using specific DSLR cameras. I wanted to create just that using the only camera I had, a GoPro. However I had something much more than the camera, I knew how to write a code that deals with a number of images (I am a Computer Vision Engineer).

Wednesday, 7 October 2015

Different types of Randomness..

Randomness is an integral part of a number of programming algorithms. In fact most of my research until now has been based on a computer algorithm that only uses random numbers for learning a specific task.

A while back I was trying to write some code to define and visualize different Random number generation models. For this task I was playing with a number of different algorithms and visualizing the output on a (kind of a ) probability map. [Not to mention that I use OpenCV for this project as well].

Some of these maps looked pretty cool, so I am just embedding them in this post - also if anyone is interested I have shared the link to this project at the bottom of this post, check that and modify --> make your own random distributions :D

So the input to each of my functions is a Uniform Distribution which looks like this:
A 2D Uniform Distribution

Sunday, 24 May 2015

What if I told you, you can use OpenCV code with Matlab mex!!



Matlab is probably one of the best tools for quickly prototyping and testing your research ideas. As quick and flexible it is, sometimes Matlab code can consume a lot of execution time. This is specifically a big hurdle when multiple experiments need to be run. A real-time execution alternative is to implement Matlab compatible C++ code and compile it with mex-compiler. While this works most of the time, it is well known that quickly implementing ideas in C++ is not possible.

Monday, 16 March 2015

Executing Matlab scripts on different Operating Systems

Just a quick post about making matlab scripts run on different OS.

Writing a matlab code that works on both Windows and Linux is a little challenging, especially when accessing the disk both OS use a slightly different syntax for filesystem.

One solution to this is using computer string to check the OS. Once checked you can use if condition statements to execute relevant code on each system.

The script for this is pretty straight forward and is listed below:

%compile everything
if strcmpi(computer,'PCWIN') |strcmpi(computer,'PCWIN64')
   compile_windows
else
   compile_linux
end



Sunday, 15 March 2015

Estimating Pi with OpenCV

Yesterday was Pi day where the date and time specifically corresponded to the value of Pi, i.e. 3/14/15 9:26  <===> 3.1415926 . What made this day extraordinary was how different ideas and videos came out, some explaining its significance while others showing fun ways to estimate its value.

One such video caught my eye in which @thephysicsgirl and @veritasium calculated the value of Pi using Monte Carlo sampling method but with a fun twist. Instead of using random particles they decided to use random darts and a modified dart board. They explain the idea in a very simple and intuitive way in the video.

Friday, 31 October 2014

Capturing OpenGL Rendered window using OpenCV

When working with graphics I wanted to store whatever was on the output of my OpenGL render window as a set of images. Typically this is achieved by using glReadPixels to read the pixels in the rendered window and store them into a byte array. This array is then converted and saved using a helper library.

I thought of doing the same thing, however, since I am more familiar with OpenCV, I wanted to use cv::Mat to do this.

Turns out it is really straight foward, all you need to do is to initialize the Mat with the required size and store the data directly onto its data container. Here is the code:

//Get dimensions of the image
RECT dimensions = rGameWindow.GetDimensions();
int width = dimensions.right - dimensions.left;
int height = dimensions.bottom - dimensions.top; 

// Initialise a Mat to contain the image
cv::Mat temp = cv::Mat::zeros(height, width, CV_8UC3); cv::Mat tempImage;   

// Read Image from buffer
glReadPixels(0,0, width, height, GL_RGB, GL_UNSIGNED_BYTE,temp.data);

// Process buffer so it matches correct format and orientation
cv::cvtColor(temp, tempImage, CV_BGR2RGB); 
cv::flip(tempImage, temp, 0);

// Write to file
cv::imwrite("savedWindow.png", temp);

Thats all! You do need to do some data conversions to match the format of OpenCV images, but its pretty straightforward and self explanatory.

Monday, 31 March 2014

Compiling OpenCV-3.0 with Matlab Support

A big uppercase HELLO to everyone!  I am back and after a long time (yet again) I am going to write a tutorial. The thing I am able to achieve here is awesome for us computer vision researchers. Yes! you heard it correct, exciting stuff.

I have been using OpenCV for quite sometime now. As good as it is for real-time computer vision applications, it can also be time consuming when it comes to exploring and implementing new research designs. Matlab on the other hand has always been flexible and a quick work around to achieve my research goals. The only problem, though, with matlab is that it is not real-time or even worse is that if you plan to implement code in OpenCV for real-time application, you would have to write the algorithms all over again as the usage of Matlab toolboxes is different than using the same methods in OpenCV.

Now comes the fun part, what if you can access OpenCV function calls within Matlab code? What if you can have easily transferable code from Matlab to C++?  This is all possible now with the OpenCV 3.0 Dev including matlab mex wrappers, which really is a good big step in the right direction. So lets start compiling the code.

Wednesday, 5 February 2014

Algorithm to check Sudoku puzzle!

I have got a couple of interviews this week, which I love preparing for as it a good way to refresh my C++ and at the same time, I get to implement some pretty interesting algorithms.

As most of us would do, I have been searching for the past few days about frequently asked interview questions and have been trying to solve most of the algorithm design questions myself. This post is about an interview question asked by google interviewers for an internship position. The question is about checking if a Sudoku Solution is correct or not.


Puzzle picture taken from: www.puzzles411.com

Thursday, 16 January 2014

Reading a Kinect Depth Image in OpenCV

While reading a captured Kinect Depth Image, it is important to read it in a way that retains the original data. Using a regular cv::imread function call can significantly modify the data stored in a Kinect Depth Image. This is because of the fact that a regular cv::imread function call uses default method, which assumes:
  1. The input image is color (three channels: RGB)
  2. The depth (number of bits per pixel) of input image is UCHAR (CV_8U) or 8bits/pixel
This is, obviously, not true for a Kinect Depth Image, for this image is a special type of grayscale image. The Kinect Depth Image contains only one channel (like any other grayscale image), however the depth of this image is actually UINT16 or unsigned int (CV_16UC) instead of UCHAR. This difference in depth is because of the fact that a Kinect Depth Image contains more values and hence it requires more bits per pixel to store this information (i.e. 16bits/pixel). Now that we know what makes it different, lets see how it can be read inside OpenCV code.

To read a Depth Image use cv::imread function with CV_LOAD_IMAGE_UNCHANGED flag. This will not change the data, reading it in its original state.
e.g. cv::imread("DepthInput.png", CV_LOAD_IMAGE_UNCHANGED);

I will be updating this post later to include details on how to capture and store a data stream from Kinect using OpenCV.


Sunday, 29 December 2013

OpenCVKinect: Acquiring Kinect Data Streams in OpenCV

Click here to go to code download step directly
_______________________________________________________________________________________________

Edit (26/05/2016) : I have updated the OpenCVKinect to fix some bugs and make use of different visualization for depth maps. Details of this newer version can be seen here. All other details mentioned in this blog post still apply to the updated version.
_______________________________________________________________________________________________

Holiday season is here, and I have finally found sometime to write something for this blog. Sometime back I wrote a guide for compiling OpenCV with OpenNI support. Since that post a lot has been changed in the newer OpenNI 2.x SDK, there has been a lot of improvements for using Microsoft Kinect Sensor. One of the major change is the Object Oriented Implementation as opposed to pure C implementation in previous OpenNI 1.x versions. I have had many requests asking me to write a tutorial for compiling OpenCV with the new OpenNI 2.x support. Although this is not possible with the current SDK and OpenCV, this blog post introduces an implementation for acquiring Kinect Data Streams in OpenCV.

After a long time, I recently started working with Kinect Sensor again, and therefore I wanted to access the data streams in a more efficient manner. While it looked straightforward to use the built in function calls in OpenNI 2.x, I was more comfortable with using OpenCV format instead. Therefore I wanted to hide all that detail of OpenNI Objects and their methods in a simple and convenient way. To achieve this, I wrote my own implementation and this blog post officially releases this code as open source, for anyone to use/modify/update to. Right now I have some basic methods, but in future I am thinking of adding simple image processing/data recording methods too (to make it easier for beginners).

Monday, 12 August 2013

Setting up freeglut and GLTools with Visual Studio 2010

It's good to be writing a tutorial after a long time and there are a number of reasons for that. First of all, I have been really busy with a lot of work and research (well actually I still am!). On the other hand, it is only until recently that I have been struggling with a setup which has little tutorials documented, while there seems to be a lot of beginner developers facing the same problem as I am.

This particular tutorial deals with setting up a Microsoft Visual C++ 2010 Project for use with examples found in the OpenGL Superbible 5th Edition. The book has a section which details the same process for a Visual C++ 2008 project, which is completly different than this tutorial. As always, I have tried to keep everything simple and straightforward so even a person who has no knowledge about these settings can make them work.

Monday, 10 June 2013

Programming Inception - Function within a function

This post is about a small but vital part in one of my projects in the past. This specific part of my project dealt with interpolation of quantized silhouette images, using a simple averaging based recursive interpolation.
 
Okay, I know its difficult to understand, but don't just stop reading yet. The most interesting part about this is that it can be related to the concept of "programming inception". You might be wondering this is something very difficult or deep. Don't concentrate too hard like cobb here, from the movie inception. This concept is as simple as it can get, its just that at first its difficult to get.
 

If you are like me and have watched "Inception" several times, then recursion in programming can be thought of having a dream within a dream. The more number of levels of dream you go into, the more specific to details you get. Likewise, a recursive function calls itself with its input and uses the outputs in multiple layers of levels to get deeper and closer to the answer, just like cobb did in the movie to plot an idea in a deep dream level.