python实现_把图像去噪算法用python实现

#coding:utf-8

import sys,os

from PIL import Image,ImageDraw

#二值数组

t2val = {}

def twoValue(image,G):

for y in xrange(0,image.size[1]):

for x in xrange(0,image.size[0]):

g = image.getpixel((x,y))

if g > G:

t2val[(x,y)] = 1

else:

t2val[(x,y)] = 0

# 降噪

# 根据一个点A的RGB值,与周围的8个点的RBG值比较,设定一个值N(0

# G: Integer 图像二值化阀值

# N: Integer 降噪率 0

# Z: Integer 降噪次数

# 输出

# 0:降噪成功

# 1:降噪失败

def clearNoise(image,N,Z):

for i in xrange(0,Z):

t2val[(0,0)] = 1

t2val[(image.size[0] - 1,image.size[1] - 1)] = 1

for x in xrange(1,image.size[0] - 1):

for y in xrange(1,image.size[1] - 1):

nearDots = 0

L = t2val[(x,y)]

if L == t2val[(x - 1,y - 1)]:

nearDots += 1

if L == t2val[(x - 1,y)]:

nearDots += 1

if L == t2val[(x- 1,y + 1)]:

nearDots += 1

if L == t2val[(x,y - 1)]:

nearDots += 1

if L == t2val[(x,y + 1)]:

nearDots += 1

if L == t2val[(x + 1,y - 1)]:

nearDots += 1

if L == t2val[(x + 1,y)]:

nearDots += 1

if L == t2val[(x + 1,y + 1)]:

nearDots += 1

if nearDots < N:

t2val[(x,y)] = 1

def saveImage(filename,size):

image = Image.new("1",size)

draw = ImageDraw.Draw(image)

for x in xrange(0,size[0]):

for y in xrange(0,size[1]):

draw.point((x,y),t2val[(x,y)])

image.save(filename)

image = Image.open("d:/1.jpg").convert("L")

twoValue(image,100)

clearNoise(image,4,1)

saveImage("d:/5.jpg",image.size)

分享到:

sina.jpg

tec.jpg

2009-05-04 12:01

浏览 10529

评论

1 楼

ewwerpm

2012-10-12

有可供测试的图片吗?

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值