如何解决如何从规范化的顶点裁剪正方形图像
我正在使用此代码来识别照片的顶部和底部: (截至目前,我只使它工作于顶端。一次只做一件事;))
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
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。