当前位置:网站首页>批量对数据添加噪声并生成新命名标注文件
批量对数据添加噪声并生成新命名标注文件
2022-06-11 10:13:00 【naca yu】
比赛中,通过随机添加高斯噪声和椒盐噪声并生成新命名的标注文件:
# -*- coding: utf-8 -*-
import cv2
import numpy as np
import os.path
import copy
import random
import shutil
# 椒盐噪声
def SaltAndPepper(src,percetage):
SP_NoiseImg=src.copy()
SP_NoiseNum=int(percetage*src.shape[0]*src.shape[1])
for i in range(SP_NoiseNum):
randR=np.random.randint(0,src.shape[0]-1)
randG=np.random.randint(0,src.shape[1]-1)
randB=np.random.randint(0,3)
if np.random.randint(0,1)==0:
SP_NoiseImg[randR,randG,randB]=0
else:
SP_NoiseImg[randR,randG,randB]=255
return SP_NoiseImg
# 高斯噪声
def addGaussianNoise(image,percetage):
G_Noiseimg = image.copy()
w = image.shape[1]
h = image.shape[0]
G_NoiseNum=int(percetage*image.shape[0]*image.shape[1])
for i in range(G_NoiseNum):
temp_x = np.random.randint(0,h)
temp_y = np.random.randint(0,w)
G_Noiseimg[temp_x][temp_y][np.random.randint(3)] = np.random.randn(1)[0]
return G_Noiseimg
# 图片文件夹路径
file_dir = r'./images/'
label_dir = r'/home/ubuntu/workspace/tao-experiments/data/data_augmentation/labels/'
cnt = 1
for img_name in os.listdir(file_dir):
img_path = file_dir + img_name
label_raw_name = './labels/' + img_name[:-4] + '.txt'
label_new_name = label_dir + img_name[:-4]+ '_noise' + '.txt'
img = cv2.imread(img_path)
rand_num = random.randint(1,10)
if rand_num >=5:
img_gauss = addGaussianNoise(img, 0.3)
else:
img_gauss = SaltAndPepper(img, 0.3)
cv2.imwrite(r'./data_augmentation/images/' + img_name[0:-4] + '_noise.jpg',img_gauss)
shutil.copyfile(label_raw_name, label_new_name)
if cnt == 210:
break
else:
cnt += 1
边栏推荐
- Start jar
- Tree topology networking structure of ZigBee module communication protocol
- 科技云报道:Web3.0浪潮下的隐私计算
- Some code fragments of a universal and confession wall system developed by PHP
- Chemical composition of q355hr steel plate
- 帝国CMS仿《游戏鸟》模板源码/92kaifa版大型手机游戏门户网站模板
- Datagrip 2022, datagrip function
- Database design and paradigm explanation
- Initial deployment of Servlet
- [Clickhouse column] user initialization of new library role
猜你喜欢

BeanFactoryPostProcessor 与BeanPostProcessor的区别

MySQL transaction

对于力士乐电磁换向阀的功能与作用,你知道多少

Mouse click coordinate transformation generation

Integer lifting example

Troubleshooting the error ora-12545 reported by scanip in Oracle RAC

ESP8266_ Mqtt protocol

Wechat applet ordering system with source code

深夜小酌,50道经典SQL题,真香~

Tenthousand words thoroughly learn heap and binary tree
随机推荐
Tenthousand words thoroughly learn heap and binary tree
宜居行星
puppeteer入门之 BrowserFetcher 类
[torch]: parallel training and can dynamically set the batch size of the first GPU
Es6新特性——箭头函数
Mysql--索引
puppeteer入门之 Browser 类
UGUI
Can station B make money?
MySQL permission management and backup
宝塔面板备份与恢复数据
What are the functions and functions of the EMG actuator
Dynamically render data and carousels
Q355HR钢板化学成分
Drink at night, 50 classic SQL questions, really fragrant~
An error will be reported when the RAC modifies the scanip to different network segments
鼠标点击坐标转换生成
手写代码call,apply,bind
RSA签名问题
Technology cloud report: privacy computing under the wave of Web3.0