import cv2
import numpy as np
import os.path
import copy
# 椒盐噪声
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
# 昏暗
def darker(image, percetage=0.9):
image_copy = image.copy()
w = image.shape[1]
h = image.shape[0]
# get darker
for xi in range(0, w):
for xj in range(0, h):
image_copy[xj, xi, 0] = int(image[xj, xi, 0] * percetage)
image_copy[xj, xi, 1] = int(image[xj, xi, 1] * percetage)
image_copy[xj, xi, 2] = int(image[xj, xi, 2] * percetage)
return image_copy
# 亮度
def brighter(image, percetage=1.5):
image_copy = image.copy()
w = image.shape[1]
h = image.shape[0]
# get brighter
for xi in range(0, w):
for xj in range(0, h):
image_copy[xj, xi, 0] = np.clip(int(image[xj, xi, 0] * percetage), a_max=255, a_min=0)
image_copy[xj, xi, 1] = np.clip(int(image[xj, xi, 1] * percetage), a_max=255, a_min=0)
image_copy[xj, xi, 2] = np.clip(int(image[xj, xi, 2] * percetage), a_max=255, a_min=0)
return image_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 flip(image):
flipped_image = np.fliplr(image)
return flipped_image
# 图片文件夹路径
file_dir = r'D:/desket/gf_test/landslide/landslide/image/'
for img_name in os.listdir(file_dir):
img_path = file_dir + img_name
img = cv2.imread(img_path)
# cv2.imshow("1",img)
# cv2.waitKey(5000)
# 旋转
# rotated_90 = rotate(img, 90)
# cv2.imwrite(file_dir + img_name[0:-4] + '_r90.png', rotated_90)
# rotated_180 = rotate(img, 180)
# cv2.imwrite(file_dir + img_name[0:-4] + '_r180.png', rotated_180)
# # 镜像
# flipped_img = flip(img)
# cv2.imwrite(file_dir + img_name[0:-4] + '_fli.png', flipped_img)
#增加噪声
img_salt = SaltAndPepper(img, 0.2)
cv2.imwrite(file_dir + img_name[0:-4] + '_salt.png', img_salt)
img_gauss = addGaussianNoise(img, 0.3)
cv2.imwrite(file_dir + img_name[0:-4] + '_noise.png', img_gauss)
# # 变亮、变暗
# img_darker = darker(img)
# cv2.imwrite(file_dir + img_name[0:-4] + '_darker.png', img_darker)
# img_brighter = brighter(img)
# cv2.imwrite(file_dir + img_name[0:-4] + '_brighter.png', img_brighter)
#
# blur = cv2.GaussianBlur(img, (7, 7), 1.5)
# # cv2.GaussianBlur(图像,卷积核,标准差)
# cv2.imwrite(file_dir + img_name[0:-4] + '_blur.png', blur)
没有合适的资源?快使用搜索试试~ 我知道了~
python1008文件上传
共12个文件
py:7个
xml:4个
iml:1个
需积分: 0 0 下载量 162 浏览量
2023-10-08
00:52:49
上传
评论
收藏 8KB ZIP 举报
温馨提示
python1008文件上传
资源推荐
资源详情
资源评论
收起资源包目录
python程序.zip (12个子文件)
voc_annoation.py 2KB
picture_deal
gray_convert.py 922B
sjzq.py 4KB
torch_sjzq.py 198B
.idea
workspace.xml 6KB
misc.xml 199B
inspectionProfiles
profiles_settings.xml 174B
picture_deal.iml 335B
modules.xml 283B
black_white_zh.py 590B
jpgtpng.py 476B
renamed2.py 2KB
共 12 条
- 1
资源评论
快退休了
- 粉丝: 0
- 资源: 2
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功