Reputation: 67
I am new in matlab and I am trying to write a code that can separate hand written numbers form a squared page background. I have tried using kmeans to differ the numbers from the lines and the squers , but it doesn’t work on all images . Any ideas how can I do it? Tahnks!
Upvotes: 1
Views: 388
Reputation: 8742
I'm using the EBImage package in R, but I'm sure you can find the equivilent in matlab:
Starting with the original image:
# Read and extract greyscale image # Run kmeans with 3 centers km = kmeans(as.vector(x), 3) c = km$centers # 1 0.4936797 # 2 0.2841005 # 3 0.6456494 # Extract smallest cluster 2 (your numbers) as binary image t = (km$cluster==2) r = matrix(t, nrow(x))
r now looks like:
# Apply morphological opening (erode then dilate) with smallest possible structural element kern = makeBrush(3, 'box') #[,1] [,2] [,3] #[1,] 1 1 1 #[2,] 1 1 1 #[3,] 1 1 1 z = openingGreyScale(r, kern)
z now looks like
Upvotes: 2