Reputation: 1271
I have a picture that looks like this: https://i.sstatic.net/3O4Rt.png
And I'm trying to crop out each of the numbers individually, so I'll end up with an image array that contains only the number 0, another that has only 1, etc. After cropping and grayscaling the orginal image, I have the following code:
for i = 1:351
if(capture_crop_gray(:, i, :) > 100)
capture_crop(:, i, 1) = 255; %red
else
%we found the front of a character
for j = 1:301
if(capture_crop_gray(j, i, :) > 100)
capture_crop(j, i, 1) = 255;
end
end
end
end
And this is the result I get: https://i.sstatic.net/Ue2vc.png
So I'm able to color in everything except the black numbers, but I'm not sure how to crop each one out on it's own. I have the Image Processing Toolkit, so if there's a shortcut using that, please let me know.
Upvotes: 0
Views: 405
Reputation: 4093
If you can run a connected components algorithm on the image you should be able to find each individual digit in the image, identify the rectangular bounds, and then crop/copy accordingly. There are different ways to approach this problem, but the following should be fairly straightforward:
Although I would expect global binarization to work with your current result image, if it doesn't you could try some other local binarization methods, or adaptive binarization.
If a digit break into multiple parts after binarization--for example, if the "1" becomes two separate blobs--then run a few iterations of morphological close (dilate, then erode) on black pixels before running connected components.
It looks like these functions might help:
http://www.mathworks.com/help/images/ref/bwconncomp.html
http://www.mathworks.com/help/images/ref/imcrop.html
Upvotes: 1