opencv——图片去噪(批量去噪)

import numpy as np
import cv2
from torch.utils.data import Dataset
from PIL import Image
import os
class MyData(Dataset):
    # 初始化 root_dir大的路径和label_dir具体内容/获取地址
    def __init__(self, root_dir, label_dir):
        # 创建全局变量
        self.root_dir = root_dir
        self.label_dir = label_dir
        # 获取一个路径地址 join()作用为拼接地址
        self.path = os.path.join(self.root_dir + "/" + self.label_dir).replace("jpg", "png")
        # bmp
        # self.path = os.path.join(self.root_dir + "/" + self.label_dir).replace("bmp", "png")
        # 获取路径下的所有列表
        self.img_path = os.listdir(self.path)
        print(self.img_path)
        print("ok")

    # idex作为一个编号
    def __getitem__(self, idx):
        # 读取其中的一个图片
        img_name = self.img_path[idx]
        print(img_name)
        # 程序的相对路径
        img_iten_path = os.path.join(self.root_dir, self.label_dir, img_name)
        # 图片打开
        # img = Image.open(img_iten_path)
        img=cv2.imread(img_iten_path,0)
        # 直方图均衡化处理
        # clipLimit颜色对比度的阈值, titleGridSize进行像素均衡化的网格大小,即在多少网格下进行直方图的均衡化操作
        clahe = cv2.createCLAHE(clipLimit=4.0, tileGridSize=(8, 8))
        cl1 = clahe.apply(img)

        # 闭与开相反
        he = np.ones((2, 2), np.uint8)
        bi = cv2.morphologyEx(cl1, cv2.MORPH_CLOSE, he)

        # 均值滤波
        jun = cv2.blur(bi, (2, 2))

        # # 双边滤波
        blur = cv2.bilateralFilter(jun, 0, 90, -100)

        # 进行傅里叶变换
        dft = cv2.dft(np.float32(blur), flags=cv2.DFT_COMPLEX_OUTPUT)
        # 进行低频位置移动到中心
        dshift = np.fft.fftshift(dft)
        # 找到行列的值
        rs, cs = blur.shape
        # 计算中心的行和列
        cr, cc = int(rs / 2), int(cs / 2)

        mask = np.zeros((rs, cs, 2), np.uint8)
        for i in range(rs):
            for j in range(cs):
                mask[i][j] = 1

        m = -20  # 越小越清晰,越大越糊
        n = 185  # 越小越糊,越大越清晰
        mask[int(rs / 2 - m):int(rs / 2 + m), int(cs / 2 - m):int(cs / 2 + m)] = 0  # 高通
        mask[int(rs / 2 - n):int(rs / 2 + n), int(cs / 2 - n):int(cs / 2 + n)] = 1  # 低通
        md = dshift * mask
        # 逆傅里叶变换
        imd = np.fft.ifftshift(md)
        io = cv2.idft(imd)
        io = cv2.magnitude(io[:, :, 0], io[:, :, 1])

        # 图像归一化处理
        io = (io - np.min(io)) / (np.max(io) - np.min(io))

        # 反归一化处理
        io = io * 255
        io = np.uint8(io)
        clahe1 = cv2.createCLAHE(clipLimit=1.0, tileGridSize=(6, 6))
        cl2 = clahe1.apply(io)
     # 图片保存
        if not os.path.exists("../piliang/tu1"):
            os.mkdir("../piliang/tu1/")
            print("目录已经创建")
        cv2.imwrite("../piliang/tu1/" + img_name.replace("jpg", "png").replace("bmp", "png"),cl2)


        # 它的一个文件
        label = self.label_dir
        return img, label

    # 返回列表的一个长度
    def __len__(self):
        return len(self.img_path)


# 数据集路径
root_dir = "../piliang"
# 路径要处理的图片
label_dir = "LR"

# 数据集
low_dataset = MyData(root_dir, label_dir)

print("图片数量:", len(low_dataset))
a=1
for x in low_dataset:
    a+=1
    print("当前运行图片:",[a,len(low_dataset)])
    img, label = x


处理后:在这里插入图片描述

效果图:在这里插入图片描述

  • 2
    点赞
  • 25
    收藏
    觉得还不错? 一键收藏
  • 7
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值