如何从标准化顶点裁剪正方形图像
How to crop a square image from normalized vertices
我正在使用此代码来识别照片的顶部和底部:
(截至目前,我只对上衣起作用。一次做一件事;))
def get_file(path):
client = vision.ImageAnnotatorClient()
for images in os.listdir(path):
# # Loads the image into memory
with io.open(images, "rb") as image_file:
content = image_file.read()
image = types.Image(content=content)
objects = client.object_localization(image=image).localized_object_annotations
im = Image.open(images)
width, height = im.size
print("Number of objects found: {}".format(len(objects)))
for object_ in objects:
if object_.name == "Top":
print("Top")
l1 = object_.bounding_poly.normalized_vertices[0].x
l2 = object_.bounding_poly.normalized_vertices[0].y
l3 = object_.bounding_poly.normalized_vertices[2].x
l4 = object_.bounding_poly.normalized_vertices[3].y
left = l1 * width
top = l2 * height
right = l3 * width
bottom = l4 * height
im = im.crop((left, top, right, bottom))
im.save('new_test_cropped.tif', 'tiff')
im.show()
if __name__ == '__main__':
parser = argparse.ArgumentParser(description="Script to automatically crop images based on google vision predictions of 'tops' and 'bottoms'")
parser.add_argument('--path', help='Include the path to the images folder')
args = parser.parse_args()
get_file(args.path)
打开图像,识别服装,然后裁剪图像并保存到新文件中。 (到目前为止,它们已在循环中被覆盖,但我稍后会修复)
我想不通的是如何使裁剪比例达到 1:1。我需要将它们保存为方形裁剪后放在我们的网站上。
老实说,normalized_vertices 对我来说毫无意义。这就是我遇到麻烦的原因。
起始图片:
输出:
期望的输出:
“归一化”表示坐标除以图像的宽度或高度,因此归一化坐标 [1, 0.5]
将指示图像的所有路径 (1) 和半路径 (0.5)。
对于 1:1 纵横比,您希望 right - left
等于 top - bottom
。所以你想知道你需要增加哪个尺寸(宽度或高度),以及增加多少。
height = abs(top - bottom)
width = abs(right - left)
extrawidth = max(0, height - width)
extraheight = max(0, width - height)
如果height > width
,我们想增加宽度而不是高度。由于 height - width > 0
,正确的值将进入 extrawidth
。但是因为 width - height < 0
,extraheight
将是 0
.
现在假设我们想要围绕原始裁剪矩形对称地增加图像的尺寸。
top -= extraheight // 2
bottom += extraheight // 2
left -= extrawidth // 2
right += extrawidth // 2
最后,做裁剪:
im = im.crop((left, top, right, bottom))
对于您的图像,假设您得到 left = 93
、right = 215
、top = 49
和 bottom = 205
之前:
之后:
我正在使用此代码来识别照片的顶部和底部: (截至目前,我只对上衣起作用。一次做一件事;))
def get_file(path):
client = vision.ImageAnnotatorClient()
for images in os.listdir(path):
# # Loads the image into memory
with io.open(images, "rb") as image_file:
content = image_file.read()
image = types.Image(content=content)
objects = client.object_localization(image=image).localized_object_annotations
im = Image.open(images)
width, height = im.size
print("Number of objects found: {}".format(len(objects)))
for object_ in objects:
if object_.name == "Top":
print("Top")
l1 = object_.bounding_poly.normalized_vertices[0].x
l2 = object_.bounding_poly.normalized_vertices[0].y
l3 = object_.bounding_poly.normalized_vertices[2].x
l4 = object_.bounding_poly.normalized_vertices[3].y
left = l1 * width
top = l2 * height
right = l3 * width
bottom = l4 * height
im = im.crop((left, top, right, bottom))
im.save('new_test_cropped.tif', 'tiff')
im.show()
if __name__ == '__main__':
parser = argparse.ArgumentParser(description="Script to automatically crop images based on google vision predictions of 'tops' and 'bottoms'")
parser.add_argument('--path', help='Include the path to the images folder')
args = parser.parse_args()
get_file(args.path)
打开图像,识别服装,然后裁剪图像并保存到新文件中。 (到目前为止,它们已在循环中被覆盖,但我稍后会修复) 我想不通的是如何使裁剪比例达到 1:1。我需要将它们保存为方形裁剪后放在我们的网站上。 老实说,normalized_vertices 对我来说毫无意义。这就是我遇到麻烦的原因。
起始图片:
输出:
期望的输出:
“归一化”表示坐标除以图像的宽度或高度,因此归一化坐标 [1, 0.5]
将指示图像的所有路径 (1) 和半路径 (0.5)。
对于 1:1 纵横比,您希望 right - left
等于 top - bottom
。所以你想知道你需要增加哪个尺寸(宽度或高度),以及增加多少。
height = abs(top - bottom)
width = abs(right - left)
extrawidth = max(0, height - width)
extraheight = max(0, width - height)
如果height > width
,我们想增加宽度而不是高度。由于 height - width > 0
,正确的值将进入 extrawidth
。但是因为 width - height < 0
,extraheight
将是 0
.
现在假设我们想要围绕原始裁剪矩形对称地增加图像的尺寸。
top -= extraheight // 2
bottom += extraheight // 2
left -= extrawidth // 2
right += extrawidth // 2
最后,做裁剪:
im = im.crop((left, top, right, bottom))
对于您的图像,假设您得到 left = 93
、right = 215
、top = 49
和 bottom = 205
之前:
之后: