图像分类——OpenCV常用图像增强方法

更新时间:2021-03-09 15:36:21点击次数:410次
图像增广
常用图像增广方法主要有:左右翻转(上下翻转对于许多目标并不常用),随机裁剪,变换颜色(亮度,对比度,饱和度和色调)等等,拟用opencv-python实现部分数据增强方法。
结构如下:
class FunctionClass:
    def __init__(self, parameter):
        self.parameter=parameter
    def __call__(self, img):       
import cv2
import numpy as np
from matplotlib import pyplot as plt
%matplotlib inline
filename = 'work/1.jpg'
## [Load an image from a file]
img = cv2.imread(filename)
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
plt.imshow(img)
<matplotlib.image.AxesImage at 0x7f1488222cd0>
print(img.shape)
(350, 350, 3)
1.图片缩放
class Resize:
    def __init__(self, size):
        self.size=size
    def __call__(self, img):
        # 此处插入代码
        return cv2.resize(img, self.size)
resize=Resize( (600, 600))
img2=resize(img)
plt.imshow(img2)
<matplotlib.image.AxesImage at 0x7f148810cf10>
2.图片翻转
class Flip:
    def __init__(self, mode):
        self.mode=mode
    def __call__(self, img):
        # 此处插入代码
        return cv2.flip(img, self.mode)
img = cv2.imread(filename)
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
flip=Flip(mode=0)
img2=flip(img)
plt.imshow(img2)
<matplotlib.image.AxesImage at 0x7f14400938d0>
3图片旋转
class Rotate:
    def __init__(self, degree,size):
        self.degree=degree
        self.size=size
    def __call__(self, img):
        # 此处插入代码
        rows, cols = img.shape[:2]
        M = cv2.getRotationMatrix2D((cols / 2, rows / 2), 45, self.size)
        return cv2.warpAffine(img, M, (cols, rows))
img = cv2.imread(filename)
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
rotate=Rotate( 45, 0.7)
img2=rotate(img)
plt.imshow(img2)
<matplotlib.image.AxesImage at 0x7f1440727410
4.图片亮度调节
class Brightness:
    def __init__(self,brightness_factor):
        self.brightness_factor=brightness_factor
    def __call__(self, img):
        # 此处插入代码
        return np.uint8(np.clip((self.brightness_factor * img + 125*(1-self.brightness_factor)), 0, 255))
img = cv2.imread(filename)
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
brightness=Brightness(0.6)
img2=brightness(img)
plt.imshow(img2)
<matplotlib.image.AxesImage at 0x7f144024a150>
5.图片随机擦除
import random
import math
class RandomErasing(object):
    def __init__(self, EPSILON=0.5, sl=0.02, sh=0.4, r1=0.3,
                 mean=[0., 0., 0.]):
        self.EPSILON = EPSILON
        self.mean = mean
        self.sl = sl
        self.sh = sh
        self.r1 = r1
    def __call__(self, img):
        if random.uniform(0, 1) > self.EPSILON:
            return img
        for attempt in range(100):
            area = img.shape[0] * img.shape[1]
            target_area = random.uniform(self.sl, self.sh) * area
            aspect_ratio = random.uniform(self.r1, 1 / self.r1)
            h = int(round(math.sqrt(target_area * aspect_ratio)))
            w = int(round(math.sqrt(target_area / aspect_ratio)))            
            # 此处插入代码
            if w < img.shape[0] and h < img.shape[1]:
                x1 = random.randint(0, img.shape[1] - h)
                y1 = random.randint(0, img.shape[0] - w)
                if img.shape[2] == 3:
                    img[ x1:x1 + h, y1:y1 + w, 0] = self.mean[0]
                    img[ x1:x1 + h, y1:y1 + w, 1] = self.mean[1]
                    img[ x1:x1 + h, y1:y1 + w, 2] = self.mean[2]
                else:
                    img[x1:x1 + h, y1:y1 + w,0] = self.mean[0]
                return img
            return img
img = cv2.imread(filename)
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
erase = RandomErasing()
img2=erase(img)
plt.imshow(img2)   
<matplotlib.image.AxesImage at 0x7f14283a9e10>
6.图片随机裁剪
class RandCropImage(object):
    def __init__(self, size, scale=None, ratio=None, interpolation=-1):
        self.interpolation = interpolation if interpolation >= 0 else None
        if type(size) is int:
            self.size = (size, size)  # (h, w)
        else:
            self.size = size
        self.scale = [0.08, 1.0] if scale is None else scale
        self.ratio = [3. / 4., 4. / 3.] if ratio is None else ratio
    def __call__(self, img):
        size = self.size
        scale = self.scale
        ratio = self.ratio
        aspect_ratio = math.sqrt(random.uniform(*ratio))
        w = 1. * aspect_ratio
        h = 1. / aspect_ratio
        img_h, img_w = img.shape[:2]
        bound = min((float(img_w) / img_h) / (w**2),
                    (float(img_h) / img_w) / (h**2))
        scale_max = min(scale[1], bound)
        scale_min = min(scale[0], bound)
        target_area = img_w * img_h * random.uniform(scale_min, scale_max)
        target_size = math.sqrt(target_area)
        w = int(target_size * w)
        h = int(target_size * h)
        i = random.randint(0, img_w - w)
        j = random.randint(0, img_h - h)
        img = img[j:j + h, i:i + w, :]
        if self.interpolation is None:
            return cv2.resize(img, size)
        else:
            return cv2.resize(img, size, interpolation=self.interpolation)
img = cv2.imread(filename)
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
crop = RandCropImage(350)
size, interpolation=self.interpolation)
img = cv2.imread(filename)
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
crop = RandCropImage(350)
plt.imshow(crop(img))
<matplotlib.image.AxesImage at 0x7f14284a7a90>

本站文章版权归原作者及原出处所有 。内容为作者个人观点, 并不代表本站赞同其观点和对其真实性负责,本站只提供参考并不构成任何投资及应用建议。本站是一个个人学习交流的平台,网站上部分文章为转载,并不用于任何商业目的,我们已经尽可能的对作者和来源进行了通告,但是能力有限或疏忽,造成漏登,请及时联系我们,我们将根据著作权人的要求,立即更正或者删除有关内容。本站拥有对此声明的最终解释权。

  • 项目经理 点击这里给我发消息
  • 项目经理 点击这里给我发消息
  • 项目经理 点击这里给我发消息
  • 项目经理 点击这里给我发消息