Pyhton实现图像扭曲

0 背景

在要对一批正常图像进行扭曲处理,相当于为了做一些数据增强,代码参考了一篇帖子(附在文章末尾),我在基础上做了一些符合自己场景的调整:处理中文路径、彩色图像、批量处理、随机调整幅度、随机扭曲方式等。

1 依赖

numpy、random、math、tqdm、opencv-pyhton

2 封装功能

包含定义的用来限制最大范围的全局变量和初始化的函数、水平扭曲(幅度随机)、垂直扭曲(幅度随机)、垂直和水平扭曲(幅度随机)、图像倾斜(幅度随机)

import numpy as np
import random
import math
import cv2

global MAX_V
global MAX_H
global MAX_R

def Init(max_v,max_h,max_r):
    global MAX_V
    global MAX_H
    global MAX_R
    MAX_V = max_v
    MAX_H = max_h
    MAX_R = max_r
    
    

#####################
# Vertical wave
def VerWave(img):
    rows, cols, chs = img.shape
    img_output = np.zeros(img.shape, dtype=img.dtype)
    randa = random.randint(0,MAX_V)
    for k in range(chs):      
        for i in range(rows):
            for j in range(cols):
                offset_x = int(randa * math.sin(2 * 3.14 * i / 180))
                offset_y = 0
                if j+offset_x < cols:
                    img_output[i,j][k] = img[i,(j+offset_x)%cols][k]
                else:
                    img_output[i,j][k] = 0
    return img_output


#####################
# Horizontal wave
def HorWave(img):
    rows, cols, chs = img.shape
    randa = random.randint(0,MAX_H)
    img_output = np.zeros(img.shape, dtype=img.dtype)
    for k in range(chs):
        for i in range(rows):
            for j in range(cols):
                offset_x = 0
                offset_y = int(randa* math.sin(2 * 3.14 * j / 150))
                if i+offset_y < rows:
                    img_output[i,j][k] = img[(i+offset_y)%rows,j][k]
                else:
                    img_output[i,j][k] = 0
    return img_output
#####################
# Both horizontal and vertical 
def BothVH(img):
    rows, cols, chs = img.shape
    randa = random.randint(0,MAX_V)
    randb = random.randint(1,MAX_H)
    img_output = np.zeros(img.shape, dtype=img.dtype)
    for k in range(chs):
        for i in range(rows):
            for j in range(cols):
                offset_x = int(randa * math.sin(2 * 3.14 * i / 150))
                offset_y = int(randb * math.cos(2 * 3.14 * j / 150))
                if i+offset_y < rows and j+offset_x < cols:
                    img_output[i,j][k] = img[(i+offset_y)%rows,(j+offset_x)%cols][k]
                else:
                    img_output[i,j][k] = 0
    return img_output
#####################
# Concave effect
def Con(img):
    rows, cols, chs = img.shape
    randx = random.randint(-MAX_R,MAX_R)
    img_output = np.zeros(img.shape, dtype=img.dtype)
    for k in range(chs):
        for i in range(rows):
            for j in range(cols):
                offset_x = int(randx * math.sin(2 * 3.14 * i / (2*cols)))
                offset_y = 0
                if j+offset_x < cols:
                    img_output[i,j][k] = img[i,(j+offset_x)%cols][k]
                else:
                    img_output[i,j][k] = 0
    return img_output

3 运行调用

自定义读取和保存路径、扭曲、倾斜幅度限制范围。

对于单张图片,随机倾斜移,随机采用水平扭曲、垂直扭曲、水平和垂直扭曲、对处理后的图片进行保存。

from a import Con,VerWave,HorWave,BothVH,Init
from tqdm import tqdm
import numpy as np
import random
import cv2
import os
"""
Path: 待处理的图片路径
SavePath: 图片保存路径
Max_v: 水平扭曲幅度 随机最大范围 int
Max_h: 垂直扭曲幅度 随机最大范围 int
Max_r: 倾斜幅度 [-Max_r Max_r] int 
"""
Path = "./ye/"
SavePath ="./yea/"
Max_v = 10
Max_h = 10
Max_r = 50

Init(Max_v,Max_h,Max_r)

names = os.listdir(Path)


for name in tqdm(names):
    image_name = Path+name
    #img = cv2.imread(image_name)
    img = cv2.imdecode(np.fromfile(image_name,dtype=np.uint8),-1)#可读取中文路径
    img = Con(img)
    opt = random.randint(0,2)
    if opt==0:
        img = VerWave(img)
    elif opt == 1:
        img = HorWave(img)
    else:
        img = BothVH(img)
    #cv2.imwrite(SavePath+name,img)
    cv2.imencode('.jpg', img)[1].tofile(SavePath+name)#可保存中文路径

参考:https://zhuanlan.zhihu.com/p/99729357

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值