15 December 2013

Setting up OpenCV in Visual Studio 2012

I had previously written a post on setting up OpenCV 2.3.1 on VS 2010. It's been long since I updated it. So here's the new post on setting up OpenCV in Visual Studio 2012.

I'll be demonstrating this with OpenCV 2.4.6 on Visual Studio 2012 Express. I assume that you've got Visual Studio 2012 already installed.

Part 1

  1. Download the OpenCV installer for Windows from the official download page. Using the version just previous to the latest release is generally a good idea.
  2. Run the downloaded .exe. Select the 'Extract to' directory and press 'Extract'. (Selecting a path without spaces is a good idea.)
  3. A folder named opencv must have been created in your selected directory. Rename it to 'opencv<your-version-number>'. For ex, with OpenCV 2.4.6, I renamed it to 'opencv2.4.6'
Part 2

We need to set two environment variables. Refer this if you haven't setup environment variables before.
  1. Create a new User Variable named 'OPENCV' and set its value to the directory where you extracted earlier. 
  2. Edit the variable named 'Path'. Add ";%OPENCV%\build\x64\vc11\bin" (without quotes) if you're running a 64 bit machine or ";%OPENCV%\build\x86\vc11\bin" (without quotes) if you're running a 32 bit machine. If the Path variable doesn't exist, create a new one named 'Path'.

Part 3

Start VS 2012. If it was open previously, exit it and restart.
  1. Click on New Project. (Ctrl+Shift+N)
  2. Under Visual C++, choose Win32 Console Application. Give your project a name. Click on OK.
  3. A Win32 Application Wizard must have popped up. Click Next.
  4. On the next window, make sure you tick 'Empty Project' under Additional Options. Click on Finish.
Extra Steps for 64 bit machines. If you're on a 32 bit machine, skip to Part 4.
  1. If you're on a 64 bit machine, click on Build Menu and select 'Configuration Manager'.
  2. Click the combo box under 'Active solution platform' and select New 
  3. In the next window, under new platform, select x64. Click OK. Close the configuration manager. 

Part 4
  1. Download this zip. Extract its contents to your project folder.
  2. If you're using a version of OpenCV other than 2.4.6, open the OpenCV2.4.6.props file(Choose 32 or 64 bit accordingly) in a text editor. Find and Replace all '246' with your OpenCV version number. For ex, I would replace all 246 to 247 for OpenCV 2.4.7.
  3. In VS, open the Property Manager window. (View  Other Windows  Property Manager)
  4. Click on 'Add Existing Property Sheet'. Select the OpenCV .props file(Choose 32 or 64 bit accordingly). Click Open.
  5. In the 'Solution Explorer Window', right click on 'Source Files' folder → Add → Existing Item. Select main.cpp that you had copied to your project folder.
  6. Set the build target to Release. If you followed all steps correctly, hit the F5 key. The camera should fire up. Press Esc key to close the program.
If you face any problems, leave a comment. I'll try to help. 


2 December 2013

Propeller Display

This is a cool LED display project I built back in my 3rd semester. It's based on the principle of Persistence of Vision. When a strip of 8 LEDs is rotated at a high rpm and switched at precise intervals, a static pattern can be produced.

So many lightning scars!

Persistence of vision is the phenomenon of the eye by which an 'afterimage' is thought to persist for approximately one twenty-fifth of a second on the retina. A common example is a television set, in which each image is re-scanned 25 times per second, thus appearing continuous.

Rotating a glowing object at high speed appears as a glowing circular pattern to the human eye. Building on this basic idea, I fixed 8 LEDs linearly at equal distances from each other on a shaft attched to a high rpm dc motor. When these are rotated at high speed, 8 concentric circles are visible.

Strip of 8 LEDs
Main components:

  1. LED Strip
  2. Arduino Duemilanove
  3. 25000 RPM, 12 V DC motor
  4. Hall Effect Sensor (Wikipedia)
  5. 9 V battery to power the assembly
  6. 12 V wallwart adapter for the DC motor
  7. Aluminium Base for the setup

To display the pattern consistently at a fixed angle, it was necessary to know when a full rotation has been completed. To get this feedback, I attached a small permanent magnet to the base. A hall effect sensor was attached to the underside of the shaft such that the it passed directly over the magnet once per rotation. Thus I was able to get a signal per rotation.

I didn't have access to a proper workshop so had to improvise on a lot of stuff used to build this. The setup was mounted to the motor axle using M-Seal. The led strip and Arduino were fixed with double sided tape and dendrite glue. The LED strip had to be secured with additional rubber bands. The final important component was the aluminium base. It weighed nearly 2kgs. It had to be made heavy because otherwise the motor caused a lot of vibrations, putting a lot of strain on the whole assembly. The motor was fixed to the base as shown in the pictures.

Now that the hardware was ready, I needed to design software to make it function. Initially, I wrote a basic Arduino program to display a pattern. The pattern had to be saved in an array of ints as hex values. I'd designed each alphabet's hex pattern to show regular alphabets. But it was very cumbersome if I wanted to display any non textual pattern. To overcome this challenge, I designed the Canvas Designer program.

Canvas Designer is a cross-platform GUI desktop program that allows the user to visualize the LED pattern on the PC as it would appear on the display. Each black dot represents an LED position as it would appear in the rotating display. Each dot can be clicked to switch on the LED, at that particular position. The display can be rotated by dragging the green slider. Text can be entered manually to generate a pattern for the same. The ‘Generate Code’ button generates the hex pattern which is used by the Arduino program. It was written in Java using the Processing library for graphics.

Designed Pattern

Pattern obtained on display

And finally, a demo video (P.S - The video was shot before the paint job.) 

The project won the best display award for at the Propeller Display competition at Troika 2012. =)

22 September 2013

Announcing PDE X!

I'm really excited to announce the first public beta of my GSoC '13 project - PDE X!

PDE X is a Processing Mode that brings along powerful IDE features to the Processing Development Environment:

Intelligent Code Completion

Code Completion list pops up automatically after typing 2 characters. Press Enter to enter the current suggestion, or Esc to close the list. Chained completions like getCar().getID().print() are supported.

Quick Renaming(Refactoring)

You can now rename variables, methods or classes by right clicking on the name and selecting 'Rename'.
Also, you can right click on a variable, method or class name and select 'Show Usage' to see all occurrences in sketch.

Quick Navigation

Press Ctrl(Cmd on OS X) + L to bring up the sketch outline view. Just click on a name from the outline or type the name of the element in the search box and hit 'Enter' to scroll quickly to its definition. You can also press Ctrl(Cmd on OS X) + Left Click on a variable, function or class name in the editor to scroll to its definition.

PDE X also features suggestions for missing imports, an integrated debugger and live error checker. For a full list of features, check out the Getting Started wiki.


You'll need Processing 2.0.2 or higher. Click on the down arrow besides the mode switch button, and select Add Mode. In the Mode Manager window, select PDE X and click 'Install'.

(PDE X is hosted on GitHub)

Go ahead, give it a shot! For bug reports and feature requests, head over to the issues page.


20 July 2013

Experimental Mode Plus - GSoC 2013, Part 1

I’ve been selected for the second time in Google Summer of Code to work for Processing! I'll be working with my last year's mentor Dan Shiffman. In this blog post I’ll be talking about my GSoC project, its goals and major challenges. I’m also pleased to announce that I’m now an official member of the Processing developer team! :)

Project Idea

There are some important features of popular IDEs that I've always missed in the PDE, which make me switch to Eclipse whenever I think of writing a big sketch. Last year I had developed XQMode which now ships with Processing as Experimental Mode (along with Debug Mode). It was appreciated, but it mostly benefits users who are learning to code in Processing. I had always felt that it still isn't good enough to really attract advanced Processing users and make them choose the PDE(Processing Development Environment) over Eclipse for writing sketches. The job seemed half done. Some of the most sought after features were still lacking. This year I’m working to further improve the PDE as a modern code editor and implement more advanced features – intelligent code competition, basic refactoring, live colour editor, quick navigation and a few other features to improve the overall editor experience.

Major Goals

  • Intelligent Code Completion
  • Refactoring for variables and methods
  • Suggestions for missing import statements
  • Enhanced code navigation - Ctrl + Click to jump to declaration, Quick Find - as seen in Sublime Text, Eclipse, etc.
(Follow the progress on GitHub)

Quick Navigation

I decided to implement this first hand, since locating the declaration of every variable/method or class was fundamental for supporting code completion. Using Eclipse’s JDT SDK, I obtained the AST of the sketch code. AST stands for Abstract Syntax Tree - a tree representation of the entire code. The main java class is the root node, its methods and fields are child nodes, local variables defined inside methods are child nodes of the method nodes, etc. So by using regular tree traversal techniques, one can find the required node as well as its declaration.

Offset mapping between PDE and Java Code

Processing tries to make Java a bit more beginner friendly by slightly enhancing the Java syntax. So instead of starting hex values (colour codes) with 0xff, a # literal is used. Similarly, for casting to default types, int(), float(),boolean(), etc methods are supported in Processing. This is bit of a syntax trickery; before compiling the sketch code, Processing’s pre-processor finds these enhancements in code and substitutes them with their Java equivalent. So all # literals are replaced with 0xff, int() is replaced with PApplet.parseInt(), etc. But since all this is done behind the scenes, the user doesn’t have to bother. There are some other enhancements involved like scrapping imports, taking care of method scope, etc.

But these pre-processer enhancements create a challenge when using the Eclipse JDT SDK. The AST provide offsets for each node, but all of these offsets are w.r.t to Java version of the sketch code. Whereas the user only sees the sketch code in the PDE. For ex, the following line in Processing code:

color topColor = #ff00ff; int count = int(23.5);

Would be converted to pure Java as:

int topColor = 0xffff00ff; int count = PApplet.parseInt(23.5f);

So, obtaining a clear mapping between Processing code in the editor and Java code(used internally in Experimental Mode) was essential. I’d already implemented PDE code line number to Java code line number mapping in the previous version of Experimental Mode. This time I’d to match offsets in a single line. This proved to be a bit tricky but I worked it out in the end. The important tools for auto-completion were now ready.

Intelligent Code Completion

This is the most challenging  goal of the GSoC project. Some regular code editors (ex, gedit on Ubuntu) provide basic code completion, which consists of scanning the text buffer and creating a prediction list from the most used words. But my goals is to provide code completion that is "intelligent" - i.e., it should be context aware. It should provide meaningful and valid suggestions depending on the current code's context.

This consists of providing completion for two kinds of code:  compiled code (Java libraries in .jar files) as well as local code (present in the editor). Both of these required a different approach. I’ll be talking about local code completion in this post.

The AST of the sketch forms the backbone of code completion. Code completion is accomplished through clever traversal of this tree.

Local Code: I initially started working on code completion for local code to test the feasibility of the idea. Proper usage of Eclipse’s AST features turned out to be very difficult in the beginning because of the myriad of options and the incredible complexity (after all, Eclipse is designed by some of the most brilliant minds of the software industry). But as I worked my way through, I began to get the hang of things and how its classes were related. Initially I started with providing completions for single words like the name of a local variable, method or class; the most basic type of completion. After this, I tried to provide completions for class member access, i.e., supporting the dot operator. For ex: Let’s say I’ve defined a local class named Network. 

Network netw = new Network(); 

network is an object of a local class Network. And did you notice the awesome new toolbar background?

This was not too difficult, I had to first infer the type of the element before the dot operator. If it was an object of a class, get the class type. If it was a method, get its return type. Then look within this type for members – fields and methods.

Then I moved on to supporting dot operator chaining. For ex: 


This expression needs to be resolved step-by-step, starting with determining the type of a, look for b() within it, find b()’s return type, then find c() within that and so on, to finally obtain the return type of beyond(). Although I had a rough idea of a solution to the problem, writing it down in code proved to be no easy task. It took me quite some time to work out all the edge cases. After some iterations, I finally made a single method - resolveExpression() that got the job done using some clever recursion. But note that this was all just for local code only. Let’s say I want to show completions for ‘network.toString().’, it won’t recognize it yet, since toString() is a member of Object class – a pre-defined class, whose definition is not present in the editor code. The code completion feature was just halfway done. I’ll talk about completion for predefined classes, and some other project goals in my next blog post.



10 July 2013

Updates: Processing 2.0, Experimental Mode

It's been a while since I last updated my blog. I'll be sharing some of the major updates in this post.

The biggest of them being Processing has now moved on to a new stable version 2.0! Along with an awesome new logo and PDE interface, this version introduces many new features, and major improvements to its core libraries. You can check out the change log here. If you've been keeping up with the alpha and beta releases, you'd know that the Processing team has been hard at work, pushing out new beta versions every few months. The Processing community and forum has been supportive in the entire journey.

New logo for 2.0, designed by Casey Reas
Processing code-base has now moved to GitHub. The move from svn to git is a welcome change and collaboration is now much more easier. GitHub pull requests FTW! The google-code project page will be used for releases.

Last summer Martin and I had worked on two separate GSoC Processing projects - Debug mode and XQMode. These modes brought important features to the PDE. But even though their functionality were complimentary, they couldn't be used together. Ben Fry took the decision to merge the two into a single mode. I was assigned with this task. After a few small bumps in the road, I was successful in integrating them together. After some more fixes during the beta versions, it was ready to ship. We're calling it 'Experimental Mode'. It's been added as a core mode and is a part of Processing 2.0. It was an amazing feeling to see my code ship as a part of a stable release! :)

I've also been selected this year in Google Summer of Code '13 to work for Processing. I'll be sharing the details of my project in the next post.