【python代码】对图片进行数据增强(直方图均衡和加噪声)

文章目录


1. 代码

import os
import cv2
import albumentations as A
from tqdm import tqdm
from glob import glob
import numpy as np

# 方法1:加入噪声
trans2 = A.Compose([
    A.RandomBrightnessContrast(p=0.5),
    A.HueSaturationValue(p=0.5),
    A.OneOf([ A.AdvancedBlur(p=0.5),
                A.Blur(p=0.5),
                A.Defocus(p=0.5),
                A.GaussianBlur(p=0.5),
                A.GlassBlur(p=0.5),
                A.MedianBlur(p=0.5),
                A.MotionBlur(p=0.5),],p=1.0),
                A.GaussNoise(p=0.5),
    ])

# 方法2:直方图均衡化:对比度受限自适应直方图均衡
trans3 = A.Compose([
    A.CLAHE(p=1),  # 对比度受限自适应直方图均衡
    A.RandomGamma(p=0.5),])


def create_dir(path):
    if not os.path.exists(path):
        os.makedirs(path)

def load_data_aug(path):
    # todo:train imgs
    train_x = sorted(glob(os.path.join(path, "train/images", "*.png")))
    train_y = sorted(glob(os.path.join(path, "train/masks/0", "*.png")))

    # todo:val imgs
    val_x = sorted(glob(os.path.join(path, "val/images", "*.png")))
    val_y = sorted(glob(os.path.join(path, "val/masks/0", "*.png")))

    # test_x = sorted(glob(os.path.join(path, "test/images", "*.png")))
    # test_y = sorted(glob(os.path.join(path, "test/masks/0", "*.png")))
    return (train_x, train_y),  (val_x, val_y)

def augment_data(images, masks, save_path, augment=False):
    for idx, (x, y) in tqdm(enumerate(zip(images, masks)), total=len(images)):

        name = x.split("/")[-1].split(".")[0]
        img = cv2.imread(x)
        # img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
        # img = img[:, 520:3368, :]   # 对图片进行裁剪

        # mask = cv2.imread(y, 0) # todo:将图像读取为单通道的灰度图像
        # mask = mask[:, 520:3368]    # 对图片进行裁剪
        mask = cv2.imread(y)
        
        # for i, m in zip(img, mask):
        #     print(i.shape)
        #     print(m.shape)


        transformed = trans3(image=img, mask=mask) # todo:select methods
        img= transformed['image']
        mask = transformed['mask']

        # img = trans3(image=img)['image']
        # i1 = cv2.resize(img, (512, 512))
        # m1 = cv2.resize(mask, (512, 512))


        tmp_image_name = f"{name}_trans3.png"  # todo:rename
        tmp_mask_name = f"{name}_trans3.png"   # todo:rename

        image_path = os.path.join(save_path, "images", tmp_image_name)
        mask_path = os.path.join(save_path, "masks", "0",  tmp_mask_name)

        cv2.imwrite(image_path, img, [int(cv2.IMWRITE_PNG_COMPRESSION), 0])
        cv2.imwrite(mask_path, mask, [int(cv2.IMWRITE_PNG_COMPRESSION), 0])

if __name__ == '__main__':
    root_path = 'path/to/数据集根目录'
    out_path ='path/to/保存路径(数据集根目录)'

    (train_x, train_y), (val_x, val_y) = load_data_aug(root_path)
    # print(train_x)
    # print(train_y)
    # print(test_x)
    # print(test_y)
    # create_dir(out_path + "/train/images/")
    # create_dir(out_path + "/train/masks/0")
    # create_dir(out_path + "/val/images/")
    # create_dir(out_path + "/val/masks/0")
    # create_dir(out_path + "/test/images/")
    # create_dir(out_path + "/test/masks/0")

    augment_data(train_x, train_y, out_path + "/train/", augment=False)

    augment_data(val_x, val_y, out_path + "/val/", augment=False)

    # augment_data(test_x, test_y, out_path + "/test/", augment=False)
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
以下是使用Python代码实现上述操作的示例: ```python import cv2 import numpy as np from matplotlib import pyplot as plt # 读取原图像 img = cv2.imread('flower1.jpg') # 显示原图像及其直方图 plt.subplot(221), plt.imshow(cv2.cvtColor(img, cv2.COLOR_BGR2RGB)) plt.title('Original Image'), plt.xticks([]), plt.yticks([]) plt.subplot(222), plt.hist(img.ravel(), 256, [0, 256]) plt.title('Original Histogram'), plt.xlim([0, 256]) # 生成高斯噪声图像 gaussian_noise = np.random.normal(0, 30, img.shape) gaussian_noise = gaussian_noise.astype('uint8') gaussian_img = cv2.add(img, gaussian_noise) # 显示高斯噪声图像及其直方图 plt.subplot(223), plt.imshow(cv2.cvtColor(gaussian_img, cv2.COLOR_BGR2RGB)) plt.title('Gaussian Noise Image'), plt.xticks([]), plt.yticks([]) plt.subplot(224), plt.hist(gaussian_img.ravel(), 256, [0, 256]) plt.title('Gaussian Histogram'), plt.xlim([0, 256]) # 去噪 denoised_img = cv2.medianBlur(gaussian_img, 5) # 显示去噪后的图像及其直方图 plt.figure() plt.subplot(221), plt.imshow(cv2.cvtColor(denoised_img, cv2.COLOR_BGR2RGB)) plt.title('Denoised Image'), plt.xticks([]), plt.yticks([]) plt.subplot(222), plt.hist(denoised_img.ravel(), 256, [0, 256]) plt.title('Denoised Histogram'), plt.xlim([0, 256]) # 直方图均衡化 equalized_img = cv2.equalizeHist(cv2.cvtColor(denoised_img, cv2.COLOR_BGR2GRAY)) # 显示均衡化后的图像及其直方图 plt.subplot(223), plt.imshow(equalized_img, cmap='gray') plt.title('Equalized Image'), plt.xticks([]), plt.yticks([]) plt.subplot(224), plt.hist(equalized_img.ravel(), 256, [0, 256]) plt.title('Equalized Histogram'), plt.xlim([0, 256]) plt.show() ``` 注释已经写在代码中,运行后会显示原图像及其直方图、高斯噪声图像及其直方图、去噪后的图像及其直方图均衡化后的图像及其直方图
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Cpdr

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值