python_增加图像数据集方法

1.旋转

dir_path='/Users/xxxx/Desktop/Share Folder/png&dat/timg.jpeg‘
img=cv2.imread(dir_path)

imgInfo = img.shape
height= imgInfo[0]
width = imgInfo[1]
deep = imgInfo[2]
matRotate = cv2.getRotationMatrix2D((height*0.5, width*0.5), 45, 1) 
dst = cv2.warpAffine(img, matRotate, (height, width))

原图:
python_增加图像数据集方法
旋转后的图像:
python_增加图像数据集方法

2.翻转

dir_path='/Users/xxxx/Desktop/Share Folder/png&dat/timg.jpeg‘
img=cv2.imread(dir_path)
imgInfo = img.shape
height= imgInfo[0]
width = imgInfo[1]
deep = imgInfo[2]
matRotate = cv2.getRotationMatrix2D((height*0.5, width*0.5), 180, 1) 
dst = cv2.warpAffine(img, matRotate, (height, width))

得到图像为:
python_增加图像数据集方法

3.缩放

dir_path='/Users/xxxx/Desktop/Share Folder/png&dat/timg.jpeg‘
img=cv2.imread(dir_path)
dstHeight = int(height * 0.5)
dstWeight = int(width * 0.5)

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

得到图像为:
python_增加图像数据集方法

4.裁剪:

dir_path='/Users/xxxx/Desktop/Share Folder/png&dat/xiaoya.png'
img=cv2.imread(dir_path)
dst = img[100:400,50:500]

原图:
python_增加图像数据集方法
生成图像:
python_增加图像数据集方法

5:平移:

dir_path='/Users/xxxxx/Desktop/Share Folder/png&dat/xiaoya.png'
img=cv2.imread(dir_path)

imgInfo = img.shape
height= imgInfo[0]
width = imgInfo[1]
deep = imgInfo[2]

dst = np.zeros(imgInfo, np.uint8)

for i in range( height ):
    for j in range( width - 100 ):
        dst[i, j + 100] = img[i, j]

得到图像:
python_增加图像数据集方法

6:加噪_椒盐噪声盐点

def salt(src,percentage):
    NoiseImg=src
    rows,cols,_=NoiseImg.shape
    NoiseNum=int(percentage*rows*cols)
    for i in range(NoiseNum):
        randX=np.random.randint(0,rows-1)
        randY=np.random.randint(0,cols-1)
        if random.randint(0,1)<=0.5:
            NoiseImg[randX,randY]=255
        else:
            NoiseImg[randX,randY]=NoiseImg[randX,randY]

    return NoiseImg
dir_path='/Users/xxxxx/Desktop/Share Folder/png&dat/xiaoya.png'
img=cv2.imread(dir_path)
img=salt(img,0.01)

得到图像为:
python_增加图像数据集方法