python - Cropping an image of handwritten digit

1.7k Views Asked by At

I'm trying to predict handwritten digits using MNIST as the dataset & python. Right now, I have to give already cropped images as input to the program. Further processing to make it to MNIST dataset format is done using the following function, but how to auto crop a random image given as input ?

def imageprepare(argv):
    """
    This function returns the pixel values.
    The imput is a png file location.
    """
    im = Image.open(argv).convert('L')
    width = float(im.size[0])
    height = float(im.size[1])
    newImage = Image.new('L', (28, 28), (255)) #creates white canvas of 28x28 pixels

    if width > height: #check which dimension is bigger
        #Width is bigger. Width becomes 20 pixels.
        nheight = int(round((20.0/width*height),0)) #resize height according to ratio width
        if (nheigth == 0): #rare case but minimum is 1 pixel
            nheigth = 1  
        # resize and sharpen
        img = im.resize((20,nheight), Image.ANTIALIAS).filter(ImageFilter.SHARPEN)
        wtop = int(round(((28 - nheight)/2),0)) #caculate horizontal pozition
        newImage.paste(img, (4, wtop)) #paste resized image on white canvas
    else:
        #Height is bigger. Heigth becomes 20 pixels. 
        nwidth = int(round((20.0/height*width),0)) #resize width according to ratio height
        if (nwidth == 0): #rare case but minimum is 1 pixel
            nwidth = 1
         # resize and sharpen
        img = im.resize((nwidth,20), Image.ANTIALIAS).filter(ImageFilter.SHARPEN)
        wleft = int(round(((28 - nwidth)/2),0)) #caculate vertical pozition
        newImage.paste(img, (wleft, 4)) #paste resized image on white canvas

    #newImage.save("sample.png")

    tv = list(newImage.getdata()) #get pixel values

    #normalize pixels to 0 and 1. 0 is pure white, 1 is pure black.
    tva = [ (255-x)*1.0/255.0 for x in tv] 
    return tva
1

There are 1 best solutions below

0
On BEST ANSWER

You can use OpenCV contours to locate potential digits within your actual image, some of the techniques will depend on the actual data you are working from. There is an example of digit candidate location at http://www.pyimagesearch.com/2017/02/13/recognizing-digits-with-opencv-and-python/ that can give you some pointers.

However, you may get problems with some scripts as I believe that while in all European scripts every digit is supposed to be contiguous and distinct I am not sure that both points apply to all scripts.