当前位置:网站首页>批量对数据添加噪声并生成新命名标注文件
批量对数据添加噪声并生成新命名标注文件
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
边栏推荐
- Empire CMS imitates DIY handmade website source code of craft activity /92kaifa imitates self-adaptive mobile phone version template of craft activity
- 【高并发】关于线程池,蚂蚁金服面试官问了我这些内容!!
- Beginning simple blog emlog theme template V3
- Q1营收超华尔街预期,挚文集团的价值等待回归
- Chemical composition of q355hr steel plate
- puppeteer入门之 Puppeteer 类
- Oracle 11g RAC disk group has space and cannot add data files?
- 关于CI框架批量导出至压缩文件
- php 中使用exec显示报错
- 知识点滴 - 性格分析-四类法
猜你喜欢

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

外包干了四年,废了...

Detailed explanation of Lora module wireless transceiver communication technology

图表背后的秘密 | 技术指标讲解:唐奇安通道

Introduction to steamvr

Leetcode brushing questions - hand tearing binary tree

How much do you know about the functions and functions of Rexroth solenoid directional valve

ESP8266_ SNTP(Simple Network Time Protocol)

BCGControlBar库专业版,完整记录的MFC扩展类

RAC expdp export error: ora-31693, ora-31617, ora-19505
随机推荐
WordPress website backup
【torch】: 并行训练并且可以动态设置第一个gpu的batch size
鼠标点击坐标转换生成
Q1营收超华尔街预期,挚文集团的价值等待回归
不卷了!入职字节跳动一周就果断跑了。
Dotween usage
Using exec in PHP to display errors
面试复习之---闭包
Understanding polymorphism?
基于SSM+Vue+OSS的“依伴汉服”商城设计与开发(含源码+论文+ppt+数据库)
浅谈wandfluh比例阀的功能与作用
BeanFactory 与FactoryBean的区别
ESP8266_ Mqtt protocol
Es6新特性——箭头函数
An error can't locate env pm in @INC
Servlet 的初次部署
帝国CMS仿《游戏鸟》模板源码/92kaifa版大型手机游戏门户网站模板
Picture rule page turning
What are the application fields of MAG gear pump? To sum up
MD5学习