我已經根據resize_image()函數的解析對原圖像與resize圖像進行了解析,
若有讀者想對原圖像與目標圖像不同尺寸驗證,可根據以下代碼,調整函數參數,
其細節如下:
import cv2 as cv
import numpy as np
img=cv.imread('D:\\MASKRCNN\\mask-rcnn-me\\MASKRCNN_myself\\0.bmp')
def resize_image(image, min_dim=230, max_dim=220, min_scale=2, mode="square"):
"""Resizes an image keeping the aspect ratio unchanged.
min_dim: if provided, resizes the image such that it's smaller dimension == min_dim
max_dim: if provided, ensures that the image longest side doesn't exceed this value.
min_scale: if provided, ensure that the image is scaled up by at least
this percent even if min_dim doesn't require it.
mode: Resizing mode.
none: No resizing. Return the image unchanged.
square: Resize and pad with zeros to get a square image of size [max_dim, max_dim].
pad64: Pads width and height with zeros to make them multiples of 64.
If min_dim or min_scale are provided, it scales the image up
before padding. max_dim is ignored in this mode.
The multiple of 64 is needed to ensure smooth scaling of feature
maps up and down the 6 levels of the FPN pyramid (2**6=64).
crop: Picks random crops from the image. First, scales the image based
on min_dim and min_scale, then picks a random crop of
size min_dim x min_dim. Can be used in training only.
max_dim is not used in this mode.
Returns:
image: the resized image
window: (y1, x1, y2, x2). If max_dim is provided, padding might
be inserted in the returned image. If so, this window is the
coordinates of the image part of the full image (excluding
the padding). The x2, y2 pixels are not included.
scale: The scale factor used to resize the image
padding: Padding added to the image [(top, bottom), (left, right), (0, 0)]
"""
# Keep track of image dtype and return results in the same dtype
image_dtype = image.dtype
print('original_image_shape=',image.shape)
# Default window (y1, x1, y2, x2) and default scale == 1.
h, w = image.shape[:2]
window = (0, 0, h, w)
scale = 1
padding = [(0, 0), (0, 0), (0, 0)]
crop = None
if mode == "none":
return image, window, scale, padding, crop
# Scale?
if min_dim:
# Scale up but not down
scale = max(1, min_dim / min(h, w)) # h, w是原始圖片的高與寬
if min_scale and scale < min_scale: # min_scale是最小填充倍數的,至少要大於它
scale = min_scale
# Does it exceed max dim?
if max_dim and mode == "square":
image_max = max(h, w)
if round(image_max * scale) > max_dim: # 最終原圖片最大邊擴充不能超過最大max_dim維度,否則重新選擇scale
scale = max_dim / image_max
# Resize image using bilinear interpolation
print('scale=',scale)
if scale != 1:
image = cv.resize(image, (round(h * scale), round(w * scale)))
print('resize_image=',image.shape)
# 上一行代碼對圖像做了resize,那么會改變圖像的尺寸,這是我不願意看到的,我覺的這樣會對缺陷特征有損失,
# 或者出現變異,因此小心這里的變化
# Need padding or cropping?
if mode == "square":
# Get new height and width
h, w = image.shape[:2] # 此時已經將原圖按照scale進行了改變
top_pad = (max_dim - h) // 2
bottom_pad = max_dim - h - top_pad
left_pad = (max_dim - w) // 2
right_pad = max_dim - w - left_pad
padding = [(top_pad, bottom_pad), (left_pad, right_pad), (0, 0)]
print('padding=',padding)
print('before_pad.shape=',image.shape)
image = np.pad(image, padding, mode='constant', constant_values=0) # 將改變的圖片進行了填充
print('after_pad.shape=', image.shape)
window = (top_pad, left_pad, h + top_pad, w + left_pad) # 保存經過resize后圖片的真實大小
print('window=',window)
return image.astype(image_dtype), window, scale, padding
print('輸入圖像尺寸小於max_dim的尺寸')
resize_image(img, min_dim=230, max_dim=280, min_scale=2, mode="square")
print('輸入圖像尺寸大於max_dim的尺寸')
resize_image(img, min_dim=200, max_dim=220, min_scale=2, mode="square")
結果如下:

