英文:
Python - Image Detection of Digits
问题
我希望第一张图片像第二张图片一样剪裁。这是否适用于图像的所有尺寸?它必须适用于每个图像尺寸,而且我们不知道第一个黑色像素在哪里?这用于机器学习项目。我们需要为机器学习的数字识别剪裁图像。
from PIL import Image
import numpy as np
import matplotlib.pyplot as plt
# 剪裁图像(在第一个和最后一个像素之间)
def cuttingImage(image):
# 打开图像并转换为黑白图像:
imageSource = Image.open(image).convert('L')
# 图像尺寸
width, height = imageSource.size
# 将图像像素存储到数组中,并将像素值除以255以获得0或1的像素值
pixels = np.asarray(imageSource, dtype=np.float32) / 255
print("图像矩阵", image)
print(pixels)
# 创建一个imageArray变量来存储像素数组,以使代码更清晰
imageArray = pixels
# 最大长度
maxWidth = width - 1
# 在图像尺寸的20分之一范围内寻找第一个像素
space = max(width // 20, height // 20)
# 查找第一个像素
for top, row in enumerate(imageArray):
for left, pix in enumerate(row):
if pix >= 0.7:
break
# 查找最后一个像素
for down, row in enumerate(reversed(imageArray)):
for right, pix in enumerate(reversed(row)):
if maxWidth - right == left:
# 图像不可分割
break
elif pix >= 0.7:
break
# 图像长度
crop_width = abs(right - left)
# 图像高度
crop_height = abs(down - top)
# 图像X轴上的第一个像素
crop_center_x = int(left + crop_width / 2)
# 图像Y轴上的第一个像素
crop_center_y = int(top + crop_height / 2)
# 定义新图像的尺寸
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))
# 使用box变量剪裁图像
imageSource = imageSource.crop(box)
imageSource.save("ImageNorm_img/new-test.jpg")
# 关闭图像
imageSource.close()
请注意,我已经将代码部分保留在原文中,未进行翻译。如果您需要进一步的帮助,请随时提出。
英文:
I want that the first picture are cutting like the second.uncut
cut
It is possible with all sort of dimensions of the image? It has to work for every image size also we don't know where the first pixel black is? It is used for a projet machine learning. We need the image cut for machine learning digit recognition.
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()
答案1
得分: 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')
英文:
I think you just want something like this.
#!/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')
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论