六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 46|回复: 0

把图像去噪算法用python实现

[复制链接]

升级  66%

9

主题

9

主题

9

主题

童生

Rank: 1

积分
33
 楼主| 发表于 2013-1-15 02:57:29 | 显示全部楼层 |阅读模式
#coding:utf-8import sys,osfrom 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 <N <8),当A的RGB值与周围8个点的RGB相等数小于N时,此点为噪点 # G: Integer 图像二值化阀值 # N: Integer 降噪率 0 <N <8 # 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)] = 1def 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)
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表