Skip to main content

OpenCV and Expectation Maximisation

Recently I needed to extract a hand drawn object, like a child's doodle, from a camera image. The method that I found to be working reasonably good was OpenCV's EM (Expectation Maximisation) method.

The idea is that there will be pixels that are "close" enough that they are classified as one group, and hopefully with a doodle on paper, the algorithm will be able to cluster the colored pixels together against the white background of the paper.

First, I tried the algorithm on a random image and classifies it into three clusters. The result is as follows.


Hmm...looks good. The bright areas, almost white, are classified into one cluster (blue), whereas the darker and lighter colors are classified into two other clusters (red and green).

So what happens when used on a hand drawn doodle, and clustering it to two clusters (two separate background and desired object) ?

Not bad at all. There are some "holes" inside the contour since the coloring does not entirely cover the shape. This can be improved by dilation and erosion using OpenCV to obtain a final image mask.
This mask can then be applied back to the original image to extract the desired object.

If you are interested in the implementation, you can find it here.

Comments

Popular posts from this blog

Installing a custom ROM on Android (on the GT-N8013)

It's been a while since my last entry and since it is a new start in 2019, I thought I'd write something about "gone with the old and in with the new". I've had my Samsung Galaxy Note 10.1 (pnotewifi) since 2014, and it's one of the early Galaxy Note tablet series. It has served me well all this years but now it just sits there collecting dust. My old Samsung GT-N8013 I've known a long time about custom Android ROMs like CyanogenMod but has never had the motivation to try them out, until now ! Overview of the process For beginners like me, I didn't have an understanding of the installation process and so it looked complicated and it was one of the reasons I was put off in trying the custom ROM. I just want to say, it's not complicated at all!   Basically you will need to Prepare an SD card and install Android SDK (you need adb ). Install a custom boot loader ( TWRP is the de facto tool at the moment). Use adb to copy custom...

Building a native plugin for the Asus Xtion2 for Unity

During one of the projects in my company I needed to build a native plugin to let Unity communicate with the Asus Xtion2, specifically to get its depth data. We used to be able to do this pretty easily with the Kinect but since Microsoft discontinued it, we need to start looking for alternatives. Test Environment Windows 10 64 bit. Unity 2017.2.0f3 x64. Important! Choose x64 or x86 to match your Unity installation. OpenNI For Xtion2 SDK. The official SDK is somewhat different from the OpenNI SDK provided by Asus but it should behave the same. The one provided by Asus can be downloaded here . Make sure you choose the latest one. CMake 3.0 or higher Who is it for Someone who has been using Unity for some time and is comfortable with the concept of classes and objects. It will be very helpful if you know C++ and pointers too. Steps Since the code is provided, I will only go over the major steps. Let me know in the comments if I miss anything. Build and install OpenNI...

Pitfalls during Training and Object Detection with TensorFlow for Absolute Beginners

This article is based on the great tutorial here on how to train and detect custom objects with Tensorflow. I also referred to the official documentations here and  here  for running Tensorflow model building locally. It was my first custom detection project and I faced some hiccups along the way and this article is to log and share my finding so it can help other beginners like me. In the end, I managed to train a tensorflow model to detect Batsumaru , a character from Sanrio. This is how the detection will look like. The tools Windows 10 Pro 64 Tensorflow originally 1.7.1 and upgraded to 1.12.0. I will share the reason later. Python 3.5.4 LabelImg for image labeling PyCharm IDE Steps and Pitfalls Some of the mistakes I made and other discoveries when following the guide. I will not repeat the steps mentioned in the original guide, but only the parts where I had to deviate from the walkthrough and found out things by myself. The training and...