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. =)