对图像进行增强操作(旋转,缩放,剪切,平移,加噪声)

对图片进行旋转,缩放,剪切,平移,夹噪声等操作,对图片进行增强。
可批量操作,参见我另一篇博客:https://mp.csdn.net/mdeditor/95445913#

原图为:
                                      在这里插入图片描述

import cv2
import numpy as np
import random

def rotate(img, angle): # 对图片执行旋转操作
    imgInfo = img.shape
    height= imgInfo[0]
    width = imgInfo[1]
    matRotate = cv2.getRotationMatrix2D((width*0.5,height*0.5), angle, 1)
    dst = cv2.warpAffine(img, matRotate, (width,height))
    return  dst

def resize_pic(img, scale): # 对图片执行resize操作
    shape_img = img.shape
    dstHeight = int(shape_img[0] * scale)
    dstWeight = int(shape_img[1] * scale)

    dst = cv2.resize(img, (dstWeight, dstHeight))
    return dst

def cut(img, val): # 对图片执行剪切操作
    shape_img = img.shape
    dst = img[int(shape_img[0]*val):shape_img[0] - int(shape_img[0]*val), int(shape_img[1]*val*2):int(shape_img[1] - shape_img[1]*val*2)]
    return dst

def shift(img, val):  # 对图片执行平移操作
    imgInfo = img.shape
    height = imgInfo[0]
    width = imgInfo[1]
    dst = np.zeros(imgInfo, np.uint8)
    for i in range(height):
        for j in range(width - val):
            dst[i, j + val] = img[i, j]

    return dst

def gasuss_noise(image, mean=0, var=0.002): #  对图片添加高斯噪声
    '''
        添加高斯噪声
        mean : 均值
        var : 方差
    '''
    image = np.array(image/255, dtype=float)
    noise = np.random.normal(mean, var ** 0.5, image.shape)
    out = image + noise
    if out.min() < 0:
        low_clip = -1.
    else:
        low_clip = 0.
    out = np.clip(out, low_clip, 1.0)
    out = np.uint8(out*255)
    #cv.imshow("gasuss", out)
    return out

def sp_noise(image,prob):  #对图片添加椒盐噪声
    '''
    添加椒盐噪声
    prob:噪声比例
    '''
    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]
    return output

if __name__ == "__main__":

    img = cv2.imread('/home/yasin/cat.jpg')

    new_rotated45 = rotate(img, 45)
    resize_img = resize_pic(img, 0.5)
    cut_img = cut(img, 0.1)
    shift_img = shift(img, 100)
    gasuss_noised_img = gasuss_noise(img)
    sp_noise_img = sp_noise(img,0.01)

    cv2.imshow('new_rotated45', new_rotated45)
    cv2.waitKey()

    cv2.imshow('resize_img',resize_img)
    cv2.waitKey()

    cv2.imshow('cut_img', cut_img)
    cv2.waitKey()

    cv2.imshow('shift_img', shift_img)
    cv2.waitKey()

    cv2.imshow('gasuss_noised_img', gasuss_noised_img)
    cv2.waitKey()

    cv2.imshow('sp_noise_img', sp_noise_img)
    cv2.waitKey()

旋转结果为:

                                      在这里插入图片描述
resize结果为:
                                                                   在这里插入图片描述
剪切结果为:
                                                            在这里插入图片描述

平移结果为:
                                      在这里插入图片描述
添加高斯噪声结果为:
                                      在这里插入图片描述
添加椒盐噪声结果为:
                                      在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

南洲.

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

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

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

打赏作者

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

抵扣说明:

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

余额充值