图像分类基础

介绍

常用图像增广方法主要有:左右翻转(上下翻转对于许多目标并不常用),随机裁剪,变换颜色(亮度,对比度,饱和度和色调)等等,我们拟用opencv-python实现部分数据增强方法。
结构如下:

import cv2
import numpy as np
from matplotlib import pyplot as plt
%matplotlib inline

filename = '1.jpg'
## [Load an image from a file]
img = cv2.imread(filename)
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
plt.imshow(img)

print(img.shape)
(352, 456, 3)

1.图片缩放

class Resize:
    def __init__(self, size: tuple=(int, int)):
        self.size=size

    def __call__(self, img: np.ndarray = (int, int, int)):

        # 此处插入代码
        return cv2.resize(img, self.size, fx=0.25, fy=0.25, interpolation=cv2.INTER_NEAREST)

resize=Resize( (600, 600))
img2=resize(img)
plt.imshow(img2)

2.图片翻转

class Flip:
    # 水平翻转 1
    # 垂直翻转 0
    # 水平加垂直翻转 -1
    def __init__(self, mode: int = 0):
        self.mode=mode

    def __call__(self, img: np.ndarray = (int, int, int)):

        # 此处插入代码
        return cv2.flip(img, self.mode)

flip=Flip(mode=0)
img2=flip(img)
plt.imshow(img2)

3.图片旋转

from math import *
class Rotate:
    def __init__(self, degree: int = 45, size: tuple = (int, int)):
        self.degree = degree
        self.size = size

    def __call__(self, img: np.ndarray = (int, int, int)):

        # 此处插入代码
        height = img.shape[0] * self.size
        width = img.shape[1] * self.size

        degree = self.degree
        #旋转后的尺寸
        heightNew=int(width*fabs(sin(radians(degree)))+height*fabs(cos(radians(degree))))
        widthNew=int(height*fabs(sin(radians(degree)))+width*fabs(cos(radians(degree))))

        matRotation=cv2.getRotationMatrix2D((width/2,height/2),degree,1)

        matRotation[0,2] += (widthNew-width) / 2 #重点在这步,目前不懂为什么加这步
        matRotation[1,2] += (heightNew-height) / 2 #重点在这步
        
        imgRotation = cv2.warpAffine(img, matRotation, (widthNew, heightNew), borderValue = (255,255,255))
        return imgRotation

rotate=Rotate( 45, 0.7)
img2=rotate(img)
plt.imshow(img2)

4.图片亮度调节

class Brightness:
    def __init__(self, brightness_factor):
        self.brightness_factor = brightness_factor

    def __call__(self, img):

        # 此处插入代码
        rows, cols, channels = img.shape
        dst = img.copy()
        a = self.brightness_factor
        b = 0
        for i in range(rows):
            for j in range(cols):
                for c in range(3):
                    color = img[i,j][c] * a + b
                    if color > 255:           # 防止像素值越界(0~255)
                        dst[i,j][c] = 255
                    elif color<0:           # 防止像素值越界(0~255)
                        dst[i,j][c] = 0
                    else:
                        dst[i,j][c] = color
        return dst

brightness = Brightness(0.6)
img2 = brightness(img)
plt.imshow(img2)

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]
                return img
        return img


erase = RandomErasing()
img2=erase(img)
plt.imshow(img2)    

参考

飞桨领航团图像分类零基础训练营>[作业]图像分类第一课作业

https://aistudio.baidu.com/aistudio/course/introduce/11939?directly=1&shared=1

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Zda天天爱打卡

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值