Saturday, May 12, 2012

Barcode Detection

Hi,

In this post, I would like to share a simple method on how to detect barcode in an image.

Method :

1) Convert image to grayscale, let it be 'img'

2) Now find derivative of the image in both horizontal and vertical direction, let them be 'imgx' and 'imgy' respectively.
imgx = d (img) / dx

imgy = d (img) / dy

3) Now subtract 'imgy' from 'imgx'.
res = imgx - imgy

     Peculiarity of barcode is that, it has high gradient in horizontal direction, and low gradient in vertical direction. So their difference gives maximum value at barcode region

Implementation Results:

result 1
result 2















It is just a basic implementation. So in case of noise or other problems, additional preprocessing methods should be done.

Another problem is the rotation of the barcode. It works well only if barcode is horizontal. Otherwise, other preprocessing methods should be done to make barcode horizontal.

Code :

I have shared the code for this in an answer to a question on stackoverflow.com. Please visit the page.

With Regards
ARK.

Skeletonization using OpenCV-Python

I see people asking an algorithm for skeletonization very frequently. At first, I had no idea about it. But today, I saw a blog which demonstrates simple method to do this. Code was in C++, so I would like to convert it to Python here.

What is Skeletonization?





Answer is just right in the term. Simply, it make a thick blob very thin, may be one pixel width. Visit the wikipedia page for more details : Topological Skeleton

Code : 

import cv2
import numpy as np

img = cv2.imread('sofsk.png',0)
size = np.size(img)
skel = np.zeros(img.shape,np.uint8)

ret,img = cv2.threshold(img,127,255,0)
element = cv2.getStructuringElement(cv2.MORPH_CROSS,(3,3))
done = False

while( not done):
    eroded = cv2.erode(img,element)
    temp = cv2.dilate(eroded,element)
    temp = cv2.subtract(img,temp)
    skel = cv2.bitwise_or(skel,temp)
    img = eroded.copy()

    zeros = size - cv2.countNonZero(img)
    if zeros==size:
        done = True

cv2.imshow("skel",skel)
cv2.waitKey(0)
cv2.destroyAllWindows()

Below is the result I got:






References : 

1) http://felix.abecassis.me/2011/09/opencv-morphological-skeleton/

Friday, May 11, 2012

Detecting Glass in OpenCV

OpenCV developers are busy in implementing "Detection of Glass". And it seems they are almost finished with this.

It is implementation of "A Geodesic Active Contour Framework for Finding Glass" by K. McHenry and J. Ponce, CVPR 2006. (Download paper)

You can visit their meeting notes for implementation progress.

OpenCV Meeting Notes Minutes 2012-03-05

OpenCV Meeting Notes Minutes 2012-02-21

OpenCV Meeting Notes Minutes 2012-04-24



With Regards

ARK

Monday, May 7, 2012

Solve this Puzzle in Python

Hi,

I would like to present you a very interesting puzzle. It was asked by maths teacher in high school ( not to solve in Python, but solve in pen and paper ). I could solve it back then ( where my "method" was not good and the way i found it is still a "mystery" to me. ). Even a few days back, I admit I couldn't find a method to solve this except brute forcing.

So I decided to apply brute-forcing for this puzzle in Python, and I asked this question in stackoverflow.com. But I was really surprised seeing the answers provided there. I would like to share this with you.

This is the puzzle :


A merchant has a 40 kg weight which he used in his shop. Once, it fell from his hands and was broken into 4 pieces. But surprisingly, now he can weigh any weight between 1 kg to 40 kg with the combination of these 4 pieces.
So question is, what are weights of those 4 pieces?
Try to solve this puzzle in Python. If you can't, find its answer here.
With Regards,
ARK.

Friday, May 4, 2012

Install OpenCV in Windows for Python

Hi Friends,

Here I will tell you how to install OpenCV 2.4x in Windows  for Python 2.7.

Pre-requisites ( need to be downloaded ) :

Python : Download latest version of Python 2.7 from Python site.
Numpy : Download Numpy for Python 2.7 from here.
OpenCV 2.4 : Download OpenCV for windows from here.

Install:


1)  First install Python 2.7. Leave all settings as default. In that case, Python will be installed in default folder C:\Python27\


2) Now install Numpy. Again leave everything default. Numpy will find Python directory and will be installed to most appropriate folder.

3) Now double-click OpenCV.exe. It will ask for extraction folder. Give it as just C:\. It will extract all files to C:\opencv\  . Wait until everything is extracted.


4) Now copy everything in the folder C:\opencv\build\python\x86\2.7\ ( most probably, there will be only one file cv2.pyd ) and paste it in the folder C:\Python27\Lib\site-packages\


5) Now open your "Python IDLE" ( from Start > All Programmes > Python 2.7 > Python IDLE ) and just type following :
import cv2


If everything OK, it will import cv2 module, otherwise an error message will be shown.

So it is very simple procedure. Try it yourself and let me know how it goes.

NB : Even if you are using 64-bit windows, do the same procedure. ( Better don't go for 64-bit Python and Numpy )

With Regards,
ARK