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

这段Python代码实现了图片降噪功能,通过比较像素点及其周围8个点的灰度值,设定阈值和降噪率来决定是否替换当前点。在给定的示例中,代码打开一个灰度图片,进行多次降噪处理,并将结果保存为新的图片。
摘要由CSDN通过智能技术生成

#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

# G: Integer 图像二值化阀值

# N: Integer 降噪率 0

# 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()

分享到:

18e900b8666ce6f233d25ec02f95ee59.png

72dd548719f0ace4d5f9bca64e1d7715.png

2009-05-04 15:51

浏览 12505

评论

2 楼

ewwerpm

2012-10-12

楼主要是能提供一个测试图片就最好了。我试了一下,似乎效果不理想。可能对噪声的定义不一样吧。

1 楼

huazhouji

2010-08-26

38687d1a1ad71d37c86f287056834d1a.gif

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() ``` 请注意,上述代码仅为简单示例,实际使用中可能需要根据具体需求进行适当的修改和调整。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值