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')

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, 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 as spio
             dict(matlabVarName = pythonVarName, matlabVarName1 = pythonVarName1))

Here savemat from 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;

This is very useful and I think I will try to update all my codes in previous posts with this.

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 -; 

// 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,;

// 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.