【图像处理】python图片随机缩放(resize)和随机裁剪(crop)的三种方式

python图片随机缩放和随机裁剪

随机缩放和裁剪通常用于深度学习的数据增强;

一、random_crop:

对图片随机0.6~1.0比率大小的区域裁剪,保持长宽比不变

效果如图:
【图像处理】python图片随机缩放(resize)和随机裁剪(crop)的三种方式_第1张图片
【图像处理】python图片随机缩放(resize)和随机裁剪(crop)的三种方式_第2张图片代码:

import random
import numpy as np

def random_crop(image, min_ratio=0.6, max_ratio=1.0):

    h, w = image.shape[:2]
    
    ratio = random.random()
    
    scale = min_ratio + ratio * (max_ratio - min_ratio)
    
    new_h = int(h*scale)    
    new_w = int(w*scale)
    
    y = np.random.randint(0, h - new_h)    
    x = np.random.randint(0, w - new_w)
    
    image = image[y:y+new_h, x:x+new_w, :]
    
    return image

二、random_crop_and_resize:

将一张图片裁剪出随机大小的224×224大小的正方形区域。

效果如图:
【图像处理】python图片随机缩放(resize)和随机裁剪(crop)的三种方式_第3张图片
【图像处理】python图片随机缩放(resize)和随机裁剪(crop)的三种方式_第4张图片
代码:

import random
import numpy as np
import cv2


def random_crop_and_resize(image, size=224):

    image = resize_image(image)

    h, w = image.shape[:2]

    y = np.random.randint(0, h-size)
    x = np.random.randint(0, w-size)

    image = image[y:y+size, x:x+size, :]

    return image


def resize_image(image, size=224, bias=5):

    image_shape = image.shape

    size_min = np.min(image_shape[:2])
    size_max = np.max(image_shape[:2])

    min_size = size + np.random.randint(1, bias)

    scale = float(min_size) / float(size_min)

    image = cv2.resize(image, dsize=(0, 0), fx=scale, fy=scale)

    return image

二、random_crop_corner:

裁剪出图片四角上和中间的共5个正方形。

效果如图:
【图像处理】python图片随机缩放(resize)和随机裁剪(crop)的三种方式_第5张图片
【图像处理】python图片随机缩放(resize)和随机裁剪(crop)的三种方式_第6张图片
代码:

import random
import numpy as np
import cv2

def random_crop_corner(image, size=224):
    
    image = resize_image(image)
    
    images = []
   
    h, w = image.shape[:2]
    
    Ys = (0, 0, h-size, h-size, int((h-size)/2))    
    Xs = (0, w-size, 0, w-size, int((h-size)/2))
    
    for y, x in zip(Ys, Xs):
        value = image[y:y+size, x:x+size, :]        
        images.append(value)
        
    return images

def resize_image(image, size=224, bias=10):
    
    image_shape = image.shape
    
    size_min = np.min(image_shape[:2])    
    size_max = np.max(image_shape[:2])
    
    min_size = size + bias
    
    scale = float(min_size) / float(size_min)
    
    image = cv2.resize(image, dsize=(0, 0), fx=scale, fy=scale)
    
    return image

你可能感兴趣的:(图像处理,深度学习-目标检测,深度学习-图像识别)