Monday, April 9, 2012

cvblob and opencv for ios

There are some really good tutorials and sample code out there for getting OpenCV working in ios.

All of these show how it is possible to build a static library/framework for ios.

However, I also wanted the cvblob plugin working alongside OpenCV.
Being relatively new to c++ and OpenCV, it was a struggle to get it working right away, and my solution may not be the most elegant.
Here are the steps that I followed, which may give you insight if you are doing the same thing.

Step 1: Get OpenCV working
I followed the steps here to get OpenCV built and working in my xcode project

If everything goes alright, you should have OpenCV working in xcode.

Fortunately, Khvedchenya Eugene also gives a walkthrough to get the libraries and header files working in your xcode project.

In my case, the MIN symbol was giving me trouble, even if I included OpenCV before the ios frameworks. So I cheated and replaced all instances of "MIN" with "CVMIN" in my OpenCV header files.

In my project, I also added some source from Yoshimasa Niwa for converting between IpImage and UIImage.

By the way, here's what I have for included opencv libraries.

Step 2 (FAILED): Build cvblob library
After making sure the OpenCV is working as it should, it is time to import cvblob. Initially I tried to create a library for cvblob by modifying Khvedchenya Eugene's build script. Essentially, I just copied the "ios" folder from the opencv source to the cvblob source, and added
-DOpenCV_DIR=/path/to/compiled/opencv \
to the arguments in each build command in the build script.
However, there were a couple of problems. First, the current version of cvblob uses dynamically linked libraries, which aren't supported in ios. This can be mitigated by using an earlier (static) version of cvblob, like this one:
Also, everything in cvblob/tests/CMakeLists has to be commented out, since there are some things in the test files that aren't supported in ios.

With these changes, I was able to build a library. However, the linker was giving me errors when I compiled my ios project, forcing me to temporarily abandon the cvblob library idea.

Step 2 (SUCCEEDED): Add cvblob source to project
Instead of building a cvblob library, I decided to import the cvblob source directly into my project. Download the latest version of cvblob:

Put the source somewhere accessible, and add the cvBlob subfolder to the header search path.

Put the following code at the top of the file where you will use cvblob.

And compile. You'll see this error:

There's a problem with the cvSaveImageBlob in cvblob.cpp (I think this was the cause of the library build not working either). Comment out the contents of cvBlob/cvblob.cpp::cvSaveImageBlob and try to compile again.

This time it should succeed. If it did, you can now play with cvblobs inside ios. Yay!

Since I found the problem with the cvSaveImageBlob, I'd like to try and build the library again when I get the time. I'll also be blogging again if I can actually get my project idea working.

You can see the project source code here.

Hope it helps!

Saturday, February 19, 2011

SQL and the Third Manifesto

I've been poking around with relational algebra a bit lately, and came across the supposedly infamous "Third Manifesto", a paper from 1995 which argues "unequivocally" for the elimination of SQL. Authors Hugh Darwen and C.J. Date say that SQL is a barrier to achieving the "true" Relational Model, put forth by E.F. Codd in 1969. They give several reasons, not least of which are SQL's tolerance for NULL values, duplicate rows, and the need for a user to manage the order of columns (for example, when doing an insert). They propose a language "Tutorial D", along with a set of prescriptions for authentically representing a relational model.

Dave Voorhi developed an implementation of Darwen and Date's "Tutorial D", called "Rel". I decided to download it and play around to see how it works. Coming from the SQL world, I thought I'd give some brief comparisons.

To demonstrate the differences, I will create a "zoo" database in MySQL and Tutorial D. The database consists of animals, which are members of a zoo. Each animal must also belong to a species.

Creating Relations


Tutorial D

Inserting Tuplets


Tutorial D

Also, inserting NULLs in Tutorial D is forbidden. So while this works in SQL

Tutorial D complains.

Selecting data


Tutorial D



Tutorial D

Running a similar query in SQL will return multiple rows, rendering the result meaningless.

I gotta say, SQL is indeed quite clunky. How can I get in on this manifesto! But Google searches to dig further return nothing but expired pages. Even finding the "Third Manifesto" was tricky. Why isn't there more interest in this subject?

Fortunately, there are a couple of useful links to better grasp Tutorial D's syntax.

Hugh Darwen's book, "An Introduction to Relational Database Theory" (free to download)

Ahmed Yusuf Almull's dissertation on translating between SQL and Tutorial D

Rel's download page

Saturday, January 15, 2011

Workaround for a broken down key on a MacBook Pro

Recently, I had the shocking revelation that my MacBook Pro's down key stopped working. It's a lot like they say about losing your pinky finger, you don't realize how much you need it until its gone. Man, I loved that down key. With the possible exception of the space bar, I probably use the down key more than any key on my keyboard (which may be why it wimped out on me).

The "guru" at the Apple Store told me that fixing it would require an entire replacement of my keyboard, at a cost of ¥30,000. Ouch.

That's just the excuse I needed to buy a new MacBook Air, but the frugal side of me insisted that I explore other options. Like a person losing a pinky finger accomplishing pinky tasks with a ring finger, I could go on if I found a suitable substitute key.

After doing a bit of Googling, it turns out many Mac adapters miss using their "home" and "end" keys. There were quite a few sites explaining how to customize the Mac keyboard to do all sorts of crazy things, including mapping "home" and "end" keys.

This is done by creating the "~/Library/KeyBindings/DefaultKeyBinding.dict" file, which allows Cocoa-based applications (like textmate) to recognize customized key allocations. Here's a good example.

So I followed the same process for the "down" key.

First, open up "Terminal" and type the following.

And add this bit of text.

In case you're not familiar with "vi", this is done by first typing "a", then copying/pasting the code above, then hitting "escape", then typing ":wq".

This was all done, mind you, without the use of a "down" key. Tricky business.

If you're curious, the commands ("moveDown:", etc) are all listed on Apple's developer center - here's the link

Now, "ctrl+(up arrow)" mimics the use of the down key. Similarly, adding the "command" key or "shift" key on top of this also allows you to go to the end of a document, and highlight.

Mind you, this only works for Cocoa-based applications. For now, this will have to do.

Saturday, January 8, 2011

Kyoto Cabinet/Kyoto Tycoon for Python and Mac Snow Leapord

Just playing around with Kyoto Cabinet/Tycoon, the successor to Tokyo Cabinet/Tokyo Tyrant, with apparently better support for concurrency.

Getting it up and running was a breeze in Python/Mac Snow Leapord. I just followed these steps:

Following this tutorial from Kelvin Wong, the steps are the same for Kyoto Cabinet/Kyoto Tyrant as they are for their counterparts in Tokyo.
  1. Get MacPorts
  2. Open a terminal window and enter the following:
  3. Start the Kyoto Cabinet server, by entering the following:
  4. For now, leave the server running. Download and unzip KAMEDAkyosuke's Python implementation of Tycoon.
  5. Save the "" file in a folder called "PyTycoon".
  6. Open a new terminal window, and cd to "/path/to/PyTycoon/..". Run the following code:
If you look at the server instance, you should see a response like the following, indicating that the key was successfully retrieved from the database.

Wallah! Python is now talking to Kyoto Tycoon.

Sunday, December 19, 2010

iPhone face up/face down: Quick and Dirty UIAccelerometer

One thing I love about smartphones is all the little trinkets built into the device... even something as trivial as detecting if the device is "face up" or "face down".

For the iPhone, the natural way to detect the device orientation is to use the "UIDeviceOrientation" class... like so:

This is great, but I found that this method wasn't always giving me exactly what I wanted. I needed a way to detect the exact moment in time when the user flipped the phone, but it didn't seem to work as I had planned.

Instead, I found a just-as-simple way to detect the phone being face up/face down using the UIAccelerometer class. You need to do three things:

First, tell your view controller you will be using the delegate

Next, write your UIAccelerometer delegate.

If you play around with the accelerometer, you'll see that the "z" coordinate is around -1.0 when the device is face up, and around 1.0 when the device is face down (and somewhere in between as the device is being moved). I'm going to assume the device is "face up" when the z value is less than 0, otherwise I will assume it is "face down".

Finally, set up the UIAccelerometer and tell it to use your delegate:

You can get the source here.

Saturday, December 4, 2010

HTML 5 game: catch the ball

(demo game)

Well, I decided to add on to my "project" only to find that my app threw an exception if the user hadn't logged in to Facebook yet. Oops. Guess that's only one of the many reasons nobody's reading my blog.

Anyway, after getting that fixed, I decided it's about time to start playing around with HTML 5. Here in Japan, there's talk of big social networks including Gree and Mobage moving towards platforms that integrate with smartphones. What a better time than to understand canvas and SVG?

In this blog, I'm going to start simple and create some basic interactions using canvas and SVG.

To get started I shamelessly copied a blogger's code - this time it was Disorganized's demo canvas Pong game.

The code worked beautifully on the first try. I just made a few tweaks. First, I changed the game from "Pong" to a kind of rescue game, where you need to catch the ball before it falls to the bottom. This involved adding some event listeners to let the user control the direction of the catcher.

Since I want this game to work on my iPhone, I can't use the traditional "mouseup" or "mousedown" operations for standard browsers. Instead, I used the "a" tag with an "onclick" to call a Javascript function. The onclicks called functions like so:

Pretty straight-forward. To fully jump on the HTML 5 bandwagon, I also wanted to play with some SVG. Inside the "moveLeft" and "moveRight" tags, I added some polygons in the shape of arrows using the HTML 5 "polygon" tag.

To play with the HTML 5 SVG elements, I downloaded the beta version of Firefox 4. Unfortunately, since these tags aren't yet supported in most browsers, I also had to add standard "left" and "right" buttons as pngs as well.

The game moves much slower on iPhone, but works nonetheless.

Here's the game in action, and the source, if you are curious. It's not finished yet, but I hope to improve on it next time I get a chance. Again, thanks to Disorganized for the original pong game source, which you can get here.

Saturday, November 13, 2010

Google App Engine and Facebook Connect

So, I've had a hankering to start playing with HTML 5 for some time. Specifically, I'd like to make a spiffy app (maybe a game???) that works on any decent smart phone.

There are a couple prerequisites to getting there, however. Namely:
1. A good idea for an app
2. A place to host it
3. A way to get people to use it

I don't quite have prerequisite 1 yet, so I'll skip that for now. Today, I'm going to focus on problems 2 and 3.

Being a guy with a budget of 0 YEN (for those in the US, that translates to about $0 USD), Google App Engine is the perfect answer to prereq #2. I haven't given Google my credit card number, but I can make up to 10 free, fully scalable applications. (Of course, if this idea ever takes off, I'll have to pay for the bandwidth, but not a concern right now)

As for prereq 3, I figure Facebook would be a great way to get my app "viral". I have no idea what the app will be, but I know it will at least allow users to interact with a community of other users, and it will provide some incentive for users to invite their friends.

By the way, since I'm using Google App Engine, I could instead use the Google User API, and grow my app with those who have Google IDs. However, I'm going to stick with Facebook for now. I just saw the movie "The Social Network" and I want to be a part of the fun.

So, some Google searches brought me two really great references that I shamelessly took for my app.

First is Chris Riccomini's tutorial on integrating Google App Engine with Facebook connect. After getting the Google App Engine SDK, setting up my App Engine Account, and registering an app on Facebook, I was able to see my Facebook profile picture on my site in a matter of minutes.

Another quick grab was some CSS from Harry Roberts , which is custom made for iPhone displays. This might come in handy later on.

With the wiring in place to connect with Facebook, all I needed to do was focus on making the application. I decided to start off with a simple message board (yawn). The message board works just like any other message board, except that it shows the person's Facebook name and profile picture.

Essentially, I just took Chris Riccomini's tutorial as a base, and added some datastore models to the mix. Here's the source if you're curious to see.

Here is a demo of the app (working as of today, I make no promises it will work tomorrow).

Although this message board isn't all that exciting, I now have the foundations in place for my new game. All I need is a great idea.

More to come.