CV——批量图片加椒盐噪声

说明:

1.使用cv2获取图片的大小
2.在图片大小范围内生存随机的黑白点
3.使用os库完成批量处理

代码:

import numpy as np
import cv2
import os

old_path = 'old_datasets'
new_path = 'new_datasets'

# if not exist make dir
if not os.path.exists(new_path):
    os.mkdir(new_path)
# get the image list
img_list = os.listdir(old_path)

for i in range(len(img_list)):
    print(i)
    # get the path of every image
    old_image_path = os.path.join(old_path, img_list[i])
    img = cv2.imread(old_image_path, cv2.IMREAD_UNCHANGED)
    rows, cols, dims = img.shape
    # add noise to image
    for j in range(10000):
        x = np.random.randint(0, rows)
        y = np.random.randint(0, cols)
        img[x, y, :] = 255
    for k in range(10000):
        x = np.random.randint(0, rows)
        y = np.random.randint(0, cols)
        img[x, y, :] = 0
    # save the new image
    new_image_path = os.path.join(new_path, img_list[i])
    cv2.imwrite(new_image_path, img, [int(cv2.IMWRITE_JPEG_QUALITY), 100])
    # show the image
    # cv2.imshow("Demo", img)
    # cv2.waitKey(0)
    # cv2.destroyAllWindows()
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值