Tensorflow:裁剪图像的最大中央正方形区域 [英] Tensorflow: Crop largest central square region of image

查看:429
本文介绍了Tensorflow:裁剪图像的最大中央正方形区域的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的网络拍摄的图像尺寸为100 x 100像素.因此,我必须调整数据集大小不同的图像的大小.我希望能够从给定图像中提取最大的中央正方形区域,然后将其调整为100 x 100.

My network takes images of size 100 x 100 pixels. Therefore I have to resize the images of my dataset which are of different size. I want to be able to extract the largest central square region from a given image and then resize it to 100 x 100.

更准确地说,假设图像的宽度为200像素,高度为50像素.然后,我想提取最大的中央正方形区域,在此示例中为50 x 50,然后将图像调整为100 x 100像素.

To be more precisely, let's say an image has a width of 200 pixels and a height of 50 pixels. Then I want to extract the largest central square region which is in this example 50 x 50 followed by resizing the image to 100 x 100 pixels.

使用Tensorflow的正确方法是什么?现在,我正在使用tf.image.resize_images()扭曲图像,我想摆脱它.

What is the right way to do that using Tensorflow? Right now I am using tf.image.resize_images() which distorts the image and I want to get rid of that.

推荐答案

类似 crop_to_bounding_box 正在执行您需要的操作:

Sounds like crop_to_bounding_box is doing what you need:

import tensorflow as tf

def crop_center(image):
    h, w = image.shape[-3], image.shape[-2]
    if h > w:
        cropped_image = tf.image.crop_to_bounding_box(image, (h - w) // 2, 0, w, w)
    else:
        cropped_image = tf.image.crop_to_bounding_box(image, 0, (w - h) // 2, h, h)
    return tf.image.resize_images(cropped_image, (100, 100))

这篇关于Tensorflow:裁剪图像的最大中央正方形区域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆