【问题标题】:Python - Image Detection of DigitsPython - 数字的图像检测
【发布时间】:2020-01-07 03:49:27
【问题描述】:

我希望第一张图片像第二张一样剪切。uncut cut 图像的各种尺寸都可能吗?它必须适用于每种图像尺寸,而且我们不知道第一个黑色像素在哪里?它用于项目机器学习。我们需要机器学习数字识别的图像切割。

from PIL import Image
import numpy as np
import matplotlib.pyplot as plt

#couper l'image (entre le 1e et le dernier pixel)
def cuttingImage(image):

    # ouverture d’une image entant que noir/blanc:
    imageSource=Image.open(image).convert('L')

    #dimension de l'image
    width, height = imageSource.size
    #test
    #print(self.width,self.height)
    #prendre tous les pixels d'une image dans un array
    #diviser par 255 pour avoir 0 ou 1 comme valeur des pixels
    pixels=np.asarray(imageSource,dtype=np.float32)/255
    print("matriz de l'image",image)
    print(pixels)

    #créer un variable imageArray pour le tableau des pixels
    #plus lisible
    imageArray=pixels

    #longeur maximum
    maxWidth=width-1

    #une valeur maximal entre les dimensions de l'image diviser par 20
    #utilser pour couper l'image
    space=max(width//20,height//20)

    #rechercher le premier pixel
    for top, row in enumerate(imageArray):
        for left, pix in enumerate(row):
            if pix>=0.7:
               break

    #recherche le dernier pixel
    for down, row in enumerate(reversed(imageArray)):
        for right, pix in enumerate(reversed(row)):
            if maxWidth - right == left:
              # Image impossible
              break        
            elif pix>=0.7:
                break
    #longeur de l'image
    crop_width = abs(right - left) 
    # hauteur de l'image
    crop_height = abs(down - top) 
    #premier pixel sur l'axe des abscisses
    crop_center_x = int(left + crop_width/2)
    #premier pixel sur l'axe de l'ordonné
    crop_center_y = int(top + crop_height/2) 

    #tester
    #print(space)
    #definier les dimensions du nouveau image
    if(width<height):
        if(space>=50 and space<60):
            box=(((width-crop_center_x//2)//2,height-crop_center_y+3*space,crop_width-1.5*space,height+crop_height-3*space))
        elif(space<200):
            box=((-space+(width-crop_center_x//2)//2,height-crop_center_y+space,crop_width-space,height+crop_height-space))
        else:
            box=((-space+(width-crop_center_x//2)//2,height-crop_center_y+8*space,crop_width-1.5*space,height+crop_height-8*space))
    else:
        box=((-2*space+width-crop_center_x//2,height-crop_center_y+2*space,crop_width-3.5*space,height+crop_height-2*space))


    #Couper l'image avec l'aide de variable box
    imageSource=imageSource.crop(box)

    imageSource.save("ImageNorm_img/new-test.jpg")

    #fermer l'image
    imageSource.close()

【问题讨论】:

标签: python image python-imaging-library


【解决方案1】:

我想你只是想要这样的东西。

#!/usr/bin/env python3

from PIL import Image
import numpy as np

# Load image, and make into Numpy array
im = Image.open('text.jpg')
na = np.array(im.convert('L'))

# Stretch the contrast to range 0..255 to maximize chances of splitting digits from background
na = ((na.astype(np.float)-na.min())*255.0/(na.max()-na.min())).astype(np.uint8)

# Threshold image to pure black and white
blk = np.array([0],  np.uint8)
wht = np.array([255],np.uint8)
thr = np.where(na>128, blk, wht)

# Go back to PIL Image from Numpy array
res = Image.fromarray(thr)

# Get bounding box from thresholded image
bbox = res.getbbox()
print('Bounding box:',bbox)

# Apply bounding box to original image and save
result = im.crop(bbox)
result.save('result.png')

【讨论】:

    猜你喜欢
    • 2021-03-17
    • 2019-07-27
    • 2021-03-12
    • 2020-02-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-19
    • 1970-01-01
    • 2014-10-30
    相关资源
    最近更新 更多