Getting Started with OpenCV (Java, MacOSx)

What is OpenCV?

The reason of you being on this page means either you know what is OpenCV or want to know more about its capabilities and uses. For the uninitiated ones, following is the summary and potential uses of the OpenCV  framework.

OpenCV [OpenCV] is an open source computer vision library, The library is written in C and C++ and runs under Linux, Windows and Mac OS X. There is active development on interfaces for Java,Python, Ruby, Matlab, and other languages.

OpenCV was optimized for computational efficiency and with a strong focus on realtime applications. One of OpenCV’s goals is to provide a easy-to-use computer vision infrastructure that helps people build fairly sophisticated vision applications quickly. The OpenCV library contains over 500 functions that span many areas in vision, including factory product inspection, medical imaging, security, user interface, camera calibration, stereo vision, and robotics. Because computer vision and machine learning often go hand in-hand, OpenCV also contains a full, general-purpose Machine Learning Library (MLL).

Getting Started Now …..

Okay enough of the technical mumbo-jumbo, let’s get started and get our hands dirty.

Although I would be compiling and building the OpenCV library on a MacOSx machine, the compilation will be exactly the same for any other operating system.

Step 1:

Goto and download the latest version for your appropriate OS.

Step 2:

Unzip the tar file in a directory of your choice

Step 3:

Goto the terminal or command window and cd to your unzipped directory.

Step 4:

Make a new directory build : mkdir build

Step 5:

This is a very important step who only want a JAVA build.



Notice the ..  at the end of the command , this make the command to look for the CMakeLists file in the parent directory of the current directory.

Step 6:

Once its confirmed that the configure file is ready, type the following command


It will take a while to get the library complied , once the progress reaches 100% you are ready to  create you first app.

Step 7:

Just to make sure the compilation has been done correctly. Refer to the

Your user library settings in Eclipse should look something like this


Step 8:

Compile and run a simple test program:

import org.opencv.core.Core;
import org.opencv.core.CvType;
import org.opencv.core.Mat;

public class Hello
public static void main( String[] args )
System.loadLibrary( Core.NATIVE_LIBRARY_NAME );
Mat mat = Mat.eye( 3, 3, CvType.CV_8UC1 );
System.out.println( “mat = ” + mat.dump() );


That is it, whenever you start a new project just add the OpenCV user library that you have defined to your project and you are good to go.

The next tutorial will be on creating your first Face Detection app in Java.


Creating a Custom Scalable background

The Android SDK provides a very powerful feature which helps you create cool looking backgrounds for your views and widgets. The best part is you do not have to create or resize any images to fit any device screens and the whole thing is based on simple xml files. And you don’t have to waste time in scaling a lot of images.

To create a custom background file create a custom XML file in the /res/drawable folder in the project and later it can be referenced using the android:background attribute of any view or widget.

You would need to use the <shape> element in the XML file and then define the attribute of the shape, the shape can be a rectangle, oval, line or ring. Though the most commonly used shape is rectangle.

In the example below we create a rectangle with rounded corners and use a gradient to color the background. Also the rounded rectangle has a border of 5 dip.

Step 1:

Right click on the res folder in the project >> New >> Android XML File

Step 2: Select the Resource Type as Drawable and select the Root Element as shape and name the file eg. Gradientbg.

This will create a gradientbg.xml under the drawable folder under the res folder and open the XML file in the code window.

Step 3: Modify the xml file as follows:

<?xml version="1.0" encoding="utf-8"?>

<shape xmlns:android=""

android:shape="rectangle" >

<gradient android:angle="270" android:endColor="#CEECF5" android:startColor="#00BFFF" android:type="linear" >

<corners android:radius="20dip" /><stroke android:width="5dip" android:color="#000" /></shape>

android:shape defines the shape of the background it coud be rectangle,oval,ring or line.

<gradient> tag provides the startcolor and the endcolor and the angle in multiples of 45, also type which could be linear,radial or sweep.

<corners> only applies to the shape rectangle and the radius provides the degree of roundness for the corners.

<stroke> provides a border to the rectangle which has a width and a color.

Once the XML file is compiled it can be used in any view or widget by defining the android:background = “@drawable/gradientbg” where gradientbg is the XML file name.

The following screen shot shows the background applied on the whole activity by applying the background on the root layout.

In the next screenshot the same background is applied on a button widget.

Is apple moving towards extinction?



Apple is going to be removing the YouTube app from the iPhone and the iPad. The separation between Apple and Google is getting wider.

On Monday, Apple released a test version of iOS 6, their newest operating system, and developers quickly noticed that the YouTube app was missing. This is a notable change because the YouTube app has been in the line-up of Apple’s built in apps since the conception of the first iPhone in 2007. Is the removal of the YouTube app the calm before the storm?

Apple said in a statement that “Our license to include the YouTube app in iOS has ended”. They added that owners of the device would still be able to use YouTube by going to their web browser, and that Google was working on a YouTube app that users could use in the future, through the Apple App Store.

YouTube said in a statement…

View original post 247 more words

Dynamically adding widgets to a User interface

Normally the views defined using the XML layout field can be simply inflated by using the setContetntView(R.layout.main).

The following code is used to add a button dynamically:

public void onCreate(Bundle savedInstanceState) {

   //Inflate the layout file res\layout\activity_uc.xml

   LinearLayout layout = (LinearLayout)getLayoutInflater().inflate(R.layout.main,null);

   //Initiate a new button

   Button reset = new Button(this);
   reset.setText("Reset Form");

   //Add the new button to the layout

   layout.addView(reset,new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,


   //Attach the view to the window



Interface created using a Layout XML 

Interface created after adding a button dynamically

Creating a One activity Application


We are going to design a simple application which basically converts between different units of Measurement.  And the user can select an option from a dropdown(called Spinner in Android) and enter the quantity needed to be converted in a Textbox and click on the Convert button and the converted value is displayed in the same textbox(EditText in Android). A Clear button clears the EditText and a Close button closes the application.

We will start with a rough prototype and then build on it in future and try to improve and optimize it in different ways.

Let’s get going

Designing the interface

As can be seen from the requirements the interface would need the following widgets:

1. EditText ( Textbox)

2.TextView (Label)

3. Spinner (Dropdown box)

4. Button

Let’s design a layout in the Eclipse. Open the main.xml in the res/layout directory

And drag and drop the widgets required , start with the LinearLayout from the layouts section. Try to emulate from the highlighted section. Do not worry about the appearance too much.

Once done with the interface, name the widgets properly(By selecting the widget in Outline section and going to properties section) so that you can reference them from the code.

Once you have added the widgets, its time to launch the application and see your work of Art .;)

Of course the buttons or the spinners won’t work because we haven’t added event handlers for them.

Step 2:

Populating the Spinner widget. (Android Spinner, XML resources, Adapter)

We will be populating the Spinner using values from an XML file. This is the best way to decouple the business logic from the User interface. Android provides options to create different types of XML resources , for the time being just goto the res/values and right click on it >> New >> Android XML File

Select the Resource Type as Values and click Finish.

Copy and Paste the XML below. As can be seen from the XML file we have defined 2 arrays :

  1. conversions : Used to populate the spinner.
  2. multipliers: Used in the conversion calculation.
<?xml version="1.0" encoding="utf-8"?>


	<string-array name="conversions">

		<item>Pounds to Kgs</span></item>

		<item>Kgs</span> to Pounds</item>

		<item>Kilometers to Miles</item>

		<item>Miles to Kilometers</item>

		<item>Degrees Celsius</span> to Degrees Fahrenheit</span></item>

		<item>Degrees Fahrenheit</span> to Degrees Celsius</span></item>


	<string-array name="multipliers">


		<item>2.20462</item> <!-- Acres to Square Miles -->

		<item>0.621371</item> <!--Atmospheres to Pascals</span>  -->

		<item>1.60934</item> <!--Bars to Pascals</span> -->

		<item>0</item> <!--Degrees Celsius</span> to Degrees Fahrenheit</span> -->

		<item>0</item> <!--Degrees Fahrenheit</span> to Degrees Celsius</span>  -->



Eclipse power tip : To Beautify any code in Eclipse IDE use Ctrl+Shift + f for windows and Command + shift+ f for Mac.

Save the XML file and get back to the code window.

Add the following code

// Getting a reference to the Spinner from the layout

final Spinner spnConversions = (Spinner) findViewById(;

// Creating the items from the Array resource

ArrayAdapter<CharSequence> aa = ArrayAdapter.createFromResource(this,



//Setting the spinner item look from android layouts


// Populating the spinner with the XML data


Run the application and you should see the items once you click on the small down arrow on the spinner.

Add the following code for attaching a listener to the spinner widget.

AdapterView.OnItemSelectedListener spinlistener = new OnItemSelectedListener() {

public void onItemSelected(AdapterView<?> arg0, View arg1,
int position, long id) {
//save the selected position
MainActivity.this.position = position;

public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub

//attaching the listener

The code snippet saves the position of selected item. Declare a private attribute in activity named position.

Next add the listeners for the Clear and the Close buttons.

// Adding listener for the close button

Button btnClose = (Button) findViewById(;

View.OnClickListener btnCloselistener = new View.OnClickListener() {

public void onClick(View v) {

//closing the application




// Attaching the listener to the button


// Adding listener for the close button

Button btnClear = (Button) findViewById(;

View.OnClickListener btnClearListener = new OnClickListener() {

public void onClick(View v) {

//clearing the text from EditText




// Attaching the listener to the button


This finishes the simple application; we will be building on the application in the next coming up tutorials.

Creating a plug and play Android development environment

This one is for the new Android developers or the ones who want to get into Android development. With the steps given below , anyone can create a self-sufficient environment for Android development. It is useful due to the following reasons:

  1. Deploy the environment across many PCs, update the APIs only once and and copy it to other systems.
  2. In case the working environment gets  corrupt , just replace it with the saved installation.

Let’s get going….

Step 1:

Download the latest Eclipse Ide from the Eclipse download site, I prefer the win-32 version even if I have 64-bit system as sometimes it leads to conflict with the JVM.

Step 2:

Download and install the Android SDK from, it will get installed in C:\Program Files (x86)\Android\Android-SDK. Make sure you know where you are installing the SDK.

  • Once the SDK is installed download all the needed APIs for Different platforms by running SDK Manager.exe from the installation folder.

Step 3:

Open Eclipse and install the ADT plugin .

  • Goto Help>> Install New Software

  • Select the Developer tools option and click on next , Accept the EULA and let it install.

Step 4:

Once the ADT plugin is installed we move to the next step, Goto C:\Program Files (x86)\Android\ and copy or cut the Android-SDK folder and paste it where the Eclipse installation is eg. C:\Data.

Step 5:

The last step is to update the SDK location in Eclipse ,  Go to  Window>> Preferences >> Android  >> SDK Location and update it to the new location where you moved your Android-SDK folder.

Once all the steps are complete you can create a zipped folder from the C:\Android folder and reuse it on other systems or keep it in a secure place for any contingencies later.  🙂

For the newbs who do not want so much configuration, they can download the pre-installed rar file here.

It includes: Eclipse Juno , Android SDK, Android APIs, and the ADT plugin .(Also included is the GIT plugin)

OUYA: A New Kind of Video Game Console

Cool Android gaming console be the first to get it


At first I wasn’t entirely sure how I felt about the OUYA, but it’s starting to look like a pretty legit system. What’s OUYA? So glad you asked.  It’s a console in the making that breaks away from “The Big 3”, using an android based, digital download format, that will be playable on your TV.

I’m still a little conflicted though. I suppose it really depends on where the video game market goes and how well they execute their format. Will this be a commercial success? I really don’t know. But I wouldn’t necessarily want it to be either. A “commercial success” can often feel soulless and cheapened. It also kind of feels like they’re trying to sell us something that we already have. It runs on android. If I really wanted to I could hook up my android tablet to my TV and a controller to that. Granted not…

View original post 415 more words