完整的图片去噪代码(python)

#coding:utf-8
import sys,os
from PIL import Image,ImageDraw

#二值判断,如果确认是噪声,用改点的上面一个点的灰度进行替换
#该函数也可以改成RGB判断的,具体看需求如何
def getPixel(image,x,y,G,N):
    L = image.getpixel((x,y))
    if L > G:
        L = True
    else:
        L = False

    nearDots = 0
    if L == (image.getpixel((x - 1,y - 1)) > G):
        nearDots += 1
    if L == (image.getpixel((x - 1,y)) > G):
        nearDots += 1
    if L == (image.getpixel((x - 1,y + 1)) > G):
        nearDots += 1
    if L == (image.getpixel((x,y - 1)) > G):
        nearDots += 1
    if L == (image.getpixel((x,y + 1)) > G):
        nearDots += 1
    if L == (image.getpixel((x + 1,y - 1)) > G):
        nearDots += 1
    if L == (image.getpixel((x + 1,y)) > G):
        nearDots += 1
    if L == (image.getpixel((x + 1,y + 1)) > G):
        nearDots += 1

    if nearDots < N:
        return image.getpixel((x,y-1))
    else:
        return None

# 降噪 
# 根据一个点A的RGB值,与周围的8个点的RBG值比较,设定一个值N(0 <N <8),当A的RGB值与周围8个点的RGB相等数小于N时,此点为噪点 
# G: Integer 图像二值化阀值 
# N: Integer 降噪率 0 <N <8 
# Z: Integer 降噪次数 
# 输出 
#  0:降噪成功 
#  1:降噪失败 
def clearNoise(image,G,N,Z):
    draw = ImageDraw.Draw(image)

    for i in xrange(0,Z):
        for x in xrange(1,image.size[0] - 1):
            for y in xrange(1,image.size[1] - 1):
                color = getPixel(image,x,y,G,N)
                if color != None:
                    draw.point((x,y),color)

#测试代码
def main():
    #打开图片
    image = Image.open("d:/1.jpg")

    #将图片转换成灰度图片
    image = image.convert("L")

    #去噪,G = 50,N = 4,Z = 4
    clearNoise(image,50,4,4)

    #保存图片
    image.save("d:/result.jpg")


if __name__ == '__main__':
    main()

  • 15
    点赞
  • 103
    收藏
    觉得还不错? 一键收藏
  • 13
    评论
CNN(卷积神经网络)是一种深度学习模型,可用于图像去噪。下面是一个简单的CNN图像去噪Python代码示例: ```python import tensorflow as tf import numpy as np import matplotlib.pyplot as plt # 构建卷积神经网络模型 def build_model(): model = tf.keras.models.Sequential([ tf.keras.layers.Conv2D(32, (3, 3), activation='relu', padding='same', input_shape=(None, None, 1)), tf.keras.layers.Conv2D(64, (3, 3), activation='relu', padding='same'), tf.keras.layers.Conv2D(1, (3, 3), activation='sigmoid', padding='same') ]) return model # 加载数据和预处理 def load_data(): # 加载带有噪声的图片数据 # 假设你已经有了一个包含噪声图片的数据集 noisy_images = ... # 加载原始无噪声的图片数据 # 假设你已经有了一个包含原始无噪声图片的数据集 clean_images = ... # 进行数据预处理,将像素值归一化到0-1之间 noisy_images = noisy_images / 255.0 clean_images = clean_images / 255.0 return noisy_images, clean_images # 训练模型 def train_model(model, noisy_images, clean_images): model.compile(optimizer='adam', loss='mse') model.fit(noisy_images, clean_images, epochs=10, batch_size=32) # 对图片进行去噪 def denoise_image(model, noisy_image): # 将图片转化为模型输入的格式 noisy_image = np.expand_dims(noisy_image, axis=0) noisy_image = np.expand_dims(noisy_image, axis=3) # 对图片进行去噪预测 denoised_image = model.predict(noisy_image) # 将去噪结果转化为常规图片格式 denoised_image = np.squeeze(denoised_image, axis=0) denoised_image = np.squeeze(denoised_image, axis=2) return denoised_image # 主函数 def main(): # 加载数据 noisy_images, clean_images = load_data() # 构建模型 model = build_model() # 训练模型 train_model(model, noisy_images, clean_images) # 对图片进行去噪 noisy_image = ... denoised_image = denoise_image(model, noisy_image) # 输出结果 plt.figure(figsize=(12, 6)) plt.subplot(1, 2, 1) plt.title('Noisy Image') plt.imshow(noisy_image, cmap='gray') plt.subplot(1, 2, 2) plt.title('Denoised Image') plt.imshow(denoised_image, cmap='gray') plt.show() # 运行主函数 if __name__ == '__main__': main() ``` 请注意,上述代码仅为简单示例,实际使用中可能需要根据具体需求进行适当的修改和调整。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值