【飞桨】【图像分类】【PaddlePaddle】opencv 常用图像增强

opencv 常用图像增强方法

图像分类零基础训练营 (课程链接)
https://aistudio.baidu.com/aistudio/course/introduce/11939?directly=1&shared=1

1.显示

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

#读图 将图片转为RGB格式

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

在这里插入图片描述

2.缩放

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

    def __call__(self, img):
        return cv2.resize(img,self.size)
  
resize=Resize( (200, 300))
img2=resize(img)
plt.imshow(img2)

缩放至200*300大小
在这里插入图片描述

3.翻转

class Flip:
    def __init__(self, mode):
        self.mode=mode

    def __call__(self, img):
        return cv2.flip(img,self.mode)

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

在这里插入图片描述

4.图片旋转

class Rotate:
    def __init__(self, degree,size):
        self.degree=degree
        self.size=size

    def __call__(self, img):
        
        # 此处插入代码
        r,c = img.shape[:2]
        m = cv2.getRotationMatrix2D((c/2,r/2),self.degree,self.size)
        img2 = cv2.warpAffine(img,m,(c,r))
        return img2

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

在这里插入图片描述

5.亮度调节

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

    def __call__(self, img):
        img2 = np.uint8(np.clip((self.brightness_factor * img + 125*(1-self.brightness_factor)), 0, 255))
        return img2
      
brightness=Brightness(0.5)
img2=brightness(img)
plt.imshow(img2)

在这里插入图片描述

6.随机裁剪

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)
                img2 = img.copy()
                if img.shape[2] == 3:
                    img2[ x1:x1 + h, y1:y1 + w, 0] = self.mean[0]
                    img2[ x1:x1 + h, y1:y1 + w, 1] = self.mean[1]
                    img2[ x1:x1 + h, y1:y1 + w, 2] = self.mean[2]
                else:
                    img2[x1:x1 + h, y1:y1 + w,0] = self.mean[0]
                return img2
        return img2

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

在这里插入图片描述

7.加入噪点

import cv2
import numpy as np
from matplotlib import pyplot as plt
import random
 
prob=0.05
image = cv2.imread('6.jpg')

output = np.zeros(image.shape,np.uint8)
thres = 1- prob
for i in range(image.shape[0]):
    for j in range(image.shape[1]):
        rdn = random.random()
        if rdn < prob:
            output[i][j] = 0
        elif rdn > thres:
            output[i][j] = 255
        else:
            output[i][j] = image[i][j]


plt.subplot(121),plt.imshow(image),plt.title('Original')
plt.xticks([]), plt.yticks([])
plt.subplot(122),plt.imshow(output),plt.title('Averaging')
plt.xticks([]), plt.yticks([])
plt.show()

在这里插入图片描述

8.拼接

import  cv2

img1 =cv2.imread('5.jpg')
img2 =cv2.imread('6.jpg')

img3=cv2.hconcat([img2,img1])#水平拼接
img4=cv2.vconcat([img2,img1])#垂直拼接

cv2.imshow("img3", img3)
cv2.imshow("img4", img4)
cv2.waitKey(0)

在这里插入图片描述

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值