2017-09-06 13 views
0

私はデータセット& pythonとしてMNISTを使用して手書きの数字を予測しようとしています。今、私はプログラムに入力として既に切り取った画像を与えなければなりません。 MNISTデータセット形式にするためのさらなる処理は、次の関数を使用して行われますが、入力として与えられたランダムイメージを自動トリミングする方法はありますか?python - 手書き数字の画像を切り取る

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 

答えて

0

あなたは、技術のいくつかは、あなたが作業しているから、実際のデータに依存しますあなたの実際の画像の中に潜在的な数字を見つけるためにOpenCVの輪郭を使用することができます。あなたにいくつかの指針を与えることができるhttp://www.pyimagesearch.com/2017/02/13/recognizing-digits-with-opencv-and-python/ に桁の候補の場所の例があります。

しかし、すべてのヨーロッパのスクリプトでは、すべての数字が連続していて別個であるはずですが、両方の点がすべてのスクリプトに当てはまるとは確信していないので、いくつかのスクリプトで問題が発生する可能性があります。

関連する問題