Pages

Friday 21 November 2014

Saving Numpy arrays to Matlab compatible files

Working in Python but want to use you data in Matlab too.

A simple function call can do this. Here is the code:

import scipy.io as spio
spio.savemat('saveSymmetricPose.mat', 
             dict(matlabVarName = pythonVarName, matlabVarName1 = pythonVarName1))

Here savemat from scipy.io stores a number of arrays to a Mat file, that can be easily opened in Matlab. Hope this could help someone.

Source: converting-numpy-arrays-to-matlab-and-vice-versa

Thursday 20 November 2014

Hello to SyntaxHighlighter

I am trying to check if I can make SyntaxHighlighter work on my blog. This would really help me in a number of future blogposts.

#include <iostream>

int main()
{
     std::cout << "Hello SyntaxHighlighter" << std::endl;
     return 0;
}
I use html encode from here to encode the < > characters as they conflict with the HTML Tag Syntax . The code is copied directly into HTML code for this post using the <pre> Tag as:
 
<pre class="brush:cpp">
#include <iostream>

int main()
{
std::cout << "Hello SyntaxHighlighter" << std::endl;

return 0;
}
</pre>


This is very useful and I think I will try to update all my codes in previous posts with this.
Source: http://www.craftyfella.com/2010/01/syntax-highlighting-with-blogger-engine.html

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.

Saturday 27 September 2014

Saying hello to the Internet of Things!

A while back I signed up for Microsoft Developer Program for Internet of Things ( #iot for more info ). As much as I love exploring new things this was extremely exciting thing for me.

I have always had the curiosity to know more and try to hack things my own way. Even as a kid I had an investigative mind which always tried to discover more about how everything works. You can imagine this curiosity by the fact that I got severe electric shock as a kid, when I tried to cut a live wire from "Clothes Iron". This curiosity grew more and more in me, to a point that I did an engineering degree (Yes! I was born with an engineer's mind). I have always been interested in hacking different devices to make something more useful out of it.

Friday 26 September 2014

Gif animation using ImageMagick Command-Line

I have used GIFs in a number of presentations I have done and they are very useful if, like me, you are working with images/data which changes over time. Luckily I have found a very easy method of converting an image sequences into a GIF.

All you need is a working installation of ImageMagick.

Sunday 6 July 2014

Déjà vu

I have been yet again busy with a lot of work, this time its quite exciting stuff.

Anyways, today I saw something really amazing that I had to share it. If you guys have seen my older posts, you might have come across a post showing the journey of my sketches. I would like to show a sketch titled "Lost!?" which I did in 2010:


This sketch was totally a creation of my imagination, I had never seen any place like this before. That is only until today, when I was left completely speechless. I  was in Hyde Park and what I saw was completely fascinating:


How did this even happen? I have no explanation to this. Undoubtedly this is a moment when you can perfectly use the phrase "Déjà vu"

Sunday 25 May 2014

The journey of my sketches

Okay, this post is not related to anything this blog is about, however I have a feeling that this might be interesting for atleast someone.

Well when I started to write this post, it was more about how I learned to sketch with all the sketches I did and the reasons why I did them. Now after writing this post, it looks more like a journey, a journey which started sometime back. I had no skill, no proper tools, no knowledge about sketching and at some point I never thought I would do it at all. This all started one day when I was extremely bored and free, and I thought it would be cool to learn how to sketch. I started looking at other people's sketches on deviantart and tried to replicate most of the things they had done in their sketches.

My first sketch had soldiers in them. At the time I used to watch TV series "Band of brothers" and thought it would be cool to do one of the wallpapers in pencil. It was hard at first, but gradually I learned a few tricks. This was pretty basic and required not a lot of skill.

click to enlarge

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.

Tuesday 18 February 2014

Creating "Mood lights" animation with OpenCV

The other day I went on a typical London walk near Thames, and as always loved the lights, reflections and the view. It was amazing! One thing I really liked was the RGB mood lights on the bridge that transformed from every possible color in a way that it made the whole experience amazing!! Here is a glimpse from my instagram.



Since there was a sequence of colors involved, I thought I would at least try to replicate these mood lights using OpenCV. Turns out its not very difficult to make this animation at all. I wrote an algorithm for doing this using some clever tricks that did make it simple and interesting. Here is a gif showing how cool the animation looks when you execute the code.



Saturday 15 February 2014

Interview experience with Google

This may sound unbelievable but it all started a few months back in late 2013, when I saw a really cool movie called "The internship". Of course I had been thinking about applying for a software eng. intern position at Google for a long time, but it was this movie which made me get up from my couch of epic laziness and start preparing for the experience that was to follow! Unlike the movie, my story doesn't end with me getting a position at all, but it certainly is about the interesting path for a learning experience. So after watching the movie, I started preparing my application and submitted it within a few days. Pretty standard details were required for submission at this point. 

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.