各种resize

opencv

  • python
resize(src, dsize, dst, fx, fy, interpolation)

其中dsize和fx&fy二选一即可,dsize为(width, height)模式, interpolation为插值方式,默认为INTER_LINEAR

代码 插值方式
INTER_NEAREST 最近邻插值(速度快,但会出现锯齿)
INTER_LINEAR 双线性插值(默认设置,速度较快,效果还可以)
INTER_AREA 使用像素区域关系进行重采样(图像缩小推荐)
INTER_CUBIC 4x4像素邻域的双三次插值(速度较慢,效果好)
INTER_LANCZOS4 8x8像素邻域的Lanczos插值
img = cv2.imread('test.jpg')
height, width = img.shape[:2] #shape是(height, width, channel)模式
img_resize = cv2.resize(img, (width / 2, height / 2), interpolation=cv2.INTER_NEAREST) #dsize是(width, height)模式
img_resize = cv2.resize(img, (0, 0), None, fx=0.5, fy=0.5)
  • c
#include 
#include 
cv::Mat img = cv::imread("test.jpg", 1);
cv::Mat img_resize;
cv::resize(img, img_resize, cv::Size(200, 100), 0, 0, cv::INTER_LINEAR);

PIL

缩放方式有: NEAREST, BOX, BILINEAR, HAMMING, BICUBIC, LANCZOS, 默认为NEAREST,size的模式是(width, height)

import PIL.Image as Image
img = Image.open('test.jpg')
height, width = img.height, img.width
img_resize = img.resize((width / 2, height / 2), Image.BILINEAR)

Keras

size的模式是(height, width),使用的是PIL中的resize方法

image_size = (image_height, image_width)
train_datagen = ImageDataGenerator(rescale=1.0/255)
train_generator = train_datagen.flow_from_directory(
        'datasets/mytrain',  # this is the target directory
        target_size=image_size,  # all images will be resized to 224x224
        batch_size=16,
        class_mode='binary')

Tensorflow

tf.image.resize_images(
    images,
    size,
    method=ResizeMethod.BILINEAR,
    align_corners=False
)

size的模式是(height, width),插值方式有:
ResizeMethod.BILINEAR:双线性插值(默认)
ResizeMethod.NEAREST_NEIGHBOR:最近的邻居插值.
ResizeMethod.BICUBIC:双三次插值.
ResizeMethod.AREA:区域插值.

img_raw = cv2.imread('test.jpg')
h, w, _ = img_in.shape
tf_img_in = tf.placeholder(dtype=tf.float32, shape=(None, None, 3))
scale = 0.5
tf_img_op1 = tf.image.resize_images(tf_img_in, [h * scale, w * scale], method=tf.image.ResizeMethod.NEAREST_NEIGHBOR) #处理的是tensor

你可能感兴趣的:(机器学习)