SSD 图像处理数据增强方法。

可以采用ACDSee,在做如下变换的时候,搞平移,随机颜色,对比度,亮度,颜色增强 ,镜像,反转,旋转,

里面搞加噪声 等等,  少量图片 可以在ACDSee里面批量做,注意选择保留最好质量。或者矢量图模式。是的转换后的图片质量最好

 

----------------------------

import cv2
import numpy as np
import os.path
import copy
 
 
def rotate(image, angle, center=None, scale=1.0):
    (h, w) = image.shape[:2]
    # If no rotation center is specified, the center of the image is set as the rotation center
    if center is None:
        center = (w / 2, h / 2)
    M = cv2.getRotationMatrix2D(center, angle, scale)
    rotated = cv2.warpAffine(image, M, (w, h))
    return rotated
 
 
def noiseing(img):
    #img = cv2.cvtColor(rgbimg, cv2.COLOR_BGR2GRAY)
    param = 30
    grayscale = 256
    w = img.shape[1]
    h = img.shape[0]
    newimg = np.zeros((h, w, 3), np.uint8)
    #row and col
    for x in xrange(0, h):
        for y in xrange(0, w, 2): #Avoid exceeding boundaries
            r1 = np.random.random_sample()
            r2 = np.random.random_sample()
            z1 = param * np.cos(2 * np.pi * r2) * np.sqrt((-2) * np.log(r1))
            z2 = param * np.sin(2 * np.pi * r2) * np.sqrt((-2) * np.log(r1))
 
            fxy_0 = int(img[x, y, 0] + z1)
            fxy_1 = int(img[x, y, 1] + z1)
            fxy_2 = int(img[x, y, 2] + z1)
            fxy1_0 = int(img[x, y + 1, 0] + z2)
            fxy1_1 = int(img[x, y + 1, 1] + z2)
            fxy1_2 = int(img[x, y + 1, 2] + z2)
            # f(x,y)
            if fxy_0 < 0:
                fxy_val_0 = 0
            elif fxy_0 > grayscale - 1:
                fxy_val_0 = grayscale - 1
            else:
                fxy_val_0 = fxy_0
            if fxy_1 < 0:
                fxy_val_1 = 0
            elif fxy_1 > grayscale - 1:
                fxy_val_1 = grayscale - 1
            else:
                fxy_val_1 = fxy_1
            if fxy_2 < 0:
                fxy_val_2 = 0
            elif fxy_2 > grayscale - 1:
                fxy_val_2 = grayscale - 1
            else:
                fxy_val_2 = fxy_2
            # f(x,y+1)
            if fxy1_0 < 0:
                fxy1_val_0 = 0
            elif fxy1_0 > grayscale - 1:
                fxy1_val_0 = grayscale - 1
            else:
                fxy1_val_0 = fxy1_0
            if fxy1_1 < 0:
                fxy1_val_1 = 0
            elif fxy1_1 > grayscale - 1:
                fxy1_val_1 = grayscale - 1
            else:
                fxy1_val_1 = fxy1_1
            if fxy1_2 < 0:
                fxy1_val_2 = 0
            elif fxy1_2 > grayscale - 1:
                fxy1_val_2 = grayscale - 1
            else:
                fxy1_val_2 = fxy1_2
 
            newimg[x, y, 0] = fxy_val_0
            newimg[x, y, 1] = fxy_val_1
            newimg[x, y, 2] = fxy_val_2
            newimg[x, y + 1, 0] = fxy1_val_0
            newimg[x, y + 1, 1] = fxy1_val_1
            newimg[x, y + 1, 2] = fxy1_val_2
 
        #newimg = cv2.cvtColor(newimg, cv2.COLOR_GRAY2RGB)
    cv2.destroyAllWindows()
    return newimg
 
 
 
#i = 0
file_dir = "/home/xn/caffe/examples/facetestquestions/ImageDatainc/"
for class_name in os.listdir(file_dir):
#for index,name in enumerate(classes):
    class_path = file_dir+class_name+"/"
    for img_name in os.listdir(class_path):
        img_path = class_path + img_name
        image = cv2.imread(img_path)
 
        #Simple rotation 90 degrees
        rotated = rotate(image, 90)
        cv2.imwrite(class_path + '/' + img_name[0:7] +'_ro90.jpg', rotated)
 
        #Rotate 180 degrees and add Gaussian noise
        rotated = rotate(image, 180)
#        if __name__ == '__main__':
            #print 'load %s ...' % fn
            #img = cv2.imread(rotated)
#            coutn = 100000
#            for k in xrange(0, coutn):
                # get the random point
#                xi = int(np.random.uniform(0, rotated.shape[1]))
#                xj = int(np.random.uniform(0, rotated.shape[0]))
#                # add noise
#                if rotated.ndim == 2:
#                    rotated[xj, xi] = 255
#                elif rotated.ndim == 3:
#                    rotated[xj, xi, 0] = 25
#                    rotated[xj, xi, 1] = 20
#                    rotated[xj, xi, 2] = 20
            #cv2.namedWindow('img')
            #cv2.imshow('img', img)
            #cv2.waitKey()
#            cv2.destroyAllWindows()
        #newimg = skimage.util.random_noise(rotated, mode='salt', seed=None, clip=False)
        newimg = noiseing(rotated)
        #newimg = cv2.cvtColor(newing, cv2.COLOR_GRAY2BGR)
        cv2.imwrite(class_path + '/' + img_name[0:7] + '_rono.jpg', newimg)
 
        #Image processing
        size = image.shape
        #Get an image that is the same as the original image, note this to use deep copy
        iLR = copy.deepcopy(image)
        h = size[0]
        w = size[1]
        for i in range(h):  # row and col
            for j in range(w):
                iLR[i, w - 1 - j] = image[i, j]  # Mirror formula
        cv2.imwrite(class_path + '/' + img_name[0:7] + '_mirr.jpg', iLR)
————————————————
版权声明:本文为CSDN博主「xunan003」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/xunan003/article/details/80377373

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值