blueintegral
blueintegral

Reputation: 1271

Cropping out simple blobs in matlab

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

Answers (1)

Rethunk
Rethunk

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:

  1. Convert your current cropped image from color to grayscale.
  2. Find a binarization threshold (using Otsu method or similar) to convert image to black and white.
  3. Run connected components image on black and white.
  4. From each component (a.k.a. blob), find the left, top, right, bottom, or the bounding rectangle.
  5. Crop each blob from the image.

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

Related Questions