python调整图片大小reshape_Scipy旋转和缩放图像而不更改其尺寸

For my neural network I want to augment my training data by adding small random rotations and zooms to my images. The issue I am having is that scipy is changing the size of my images when it applies the rotations and zooms. I need to to just clip the edges if part of the image goes out of bounds. All of my images must be the same size.

def loadImageData(img, distort = False):

c, fn = img

img = scipy.ndimage.imread(fn, True)

if distort:

img = scipy.ndimage.zoom(img, 1 + 0.05 * rnd(), mode = 'constant')

img = scipy.ndimage.rotate(img, 10 * rnd(), mode = 'constant')

print(img.shape)

img = img - np.min(img)

img = img / np.max(img)

img = np.reshape(img, (1, *img.shape))

y = np.zeros(ncats)

y[c] = 1

return (img, y)

解决方案

scipy.ndimage.rotate accepts a reshape= parameter:

reshape : bool, optional

If reshape is true, the output shape is adapted so that the input

array is contained completely in the output. Default is True.

So to "clip" the edges you can simply call scipy.ndimage.rotate(img, ..., reshape=False).

from scipy.ndimage import rotate

from scipy.misc import face

from matplotlib import pyplot as plt

img = face()

rot = rotate(img, 30, reshape=False)

fig, ax = plt.subplots(1, 2)

ax[0].imshow(img)

ax[1].imshow(rot)

Things are more complicated for scipy.ndimage.zoom.

A naive method would be to zoom the entire input array, then use slice indexing and/or zero-padding to make the output the same size as your input. However, in cases where you're increasing the size of the image it's wasteful to interpolate pixels that are only going to get clipped off at the edges anyway.

Instead you could index only the part of the input that will fall within the bounds of the output array before you apply zoom:

import numpy as np

from scipy.ndimage import zoom

def clipped_zoom(img, zoom_factor, **kwargs):

h, w = img.shape[:2]

# For multichannel images we don't want to apply the zoom factor to the RGB

# dimension, so instead we create a tuple of zoom factors, one per array

# dimension, with 1's for any trailing dimensions after the width and height.

zoom_tuple = (zoom_factor,) * 2 + (1,) * (img.ndim - 2)

# Zooming out

if zoom_factor < 1:

# Bounding box of the zoomed-out image within the output array

zh = int(np.round(h * zoom_factor))

zw = int(np.round(w * zoom_factor))

top = (h - zh) // 2

left = (w - zw) // 2

# Zero-padding

out = np.zeros_like(img)

out[top:top+zh, left:left+zw] = zoom(img, zoom_tuple, **kwargs)

# Zooming in

elif zoom_factor > 1:

# Bounding box of the zoomed-in region within the input array

zh = int(np.round(h / zoom_factor))

zw = int(np.round(w / zoom_factor))

top = (h - zh) // 2

left = (w - zw) // 2

out = zoom(img[top:top+zh, left:left+zw], zoom_tuple, **kwargs)

# `out` might still be slightly larger than `img` due to rounding, so

# trim off any extra pixels at the edges

trim_top = ((out.shape[0] - h) // 2)

trim_left = ((out.shape[1] - w) // 2)

out = out[trim_top:trim_top+h, trim_left:trim_left+w]

# If zoom_factor == 1, just return the input array

else:

out = img

return out

For example:

zm1 = clipped_zoom(img, 0.5)

zm2 = clipped_zoom(img, 1.5)

fig, ax = plt.subplots(1, 3)

ax[0].imshow(img)

ax[1].imshow(zm1)

ax[2].imshow(zm2)

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值