Last active
December 18, 2015 23:59
-
-
Save naoyat/5865551 to your computer and use it in GitHub Desktop.
PRML §8.3.3
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
# | |
# add_noise.py - 割合を指定して画像にノイズを付加 | |
# | |
# usage: python add_noise.py <orig> <noise-rate> [<output>] | |
# | |
import sys | |
import random | |
import Image # PIL | |
from myutil import save_or_show | |
# 正確に noise_rate の分だけノイズを入れる | |
def add_noise(img_orig, noise_rate): | |
width, height = img.size | |
S = width * height | |
n = int(S * noise_rate + 0.5) | |
to_toggle = [False] * S | |
for i in range(n): to_toggle[i] = True | |
random.shuffle(to_toggle) | |
pix = img.load() | |
for y in range(height): | |
for x in range(width): | |
i = y * width + x | |
if to_toggle[i]: | |
pix[x,y] = 255 - pix[x,y] | |
return img | |
# 確率的にノイズを入れるので noise_rate とすこし離れる | |
def add_noise0(img_orig, noise_rate): | |
width, height = img.size | |
pix = img.load() | |
for y in range(height): | |
for x in range(width): | |
if random.random() < noise_rate: | |
pix[x,y] = 255 - pix[x,y] | |
return img | |
if __name__ == '__main__': | |
if len(sys.argv) < 3: | |
print "usage: python %s <orig> <noise-rate> [<output>]" % sys.argv[0] | |
sys.exit() | |
infile = sys.argv[1] | |
noise_rate = float(sys.argv[2]) / 100 | |
if len(sys.argv) == 4: | |
outfile = sys.argv[3] | |
else: | |
outfile = None | |
img = Image.open(infile) | |
img2 = add_noise(img, noise_rate) | |
save_or_show(img2, outfile) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
# | |
# make_image.py - 元画像を作成 | |
# | |
# usage: python make_noise.py [<output>] | |
# | |
import sys | |
import Image | |
import ImageDraw | |
import ImageFont | |
from myutil import save_or_show | |
if __name__ == '__main__': | |
img = Image.new('1', (320, 180), "white") | |
# font = ImageFont.truetype('/Library/Fonts/MovieFont-Home.ttf', 48, encoding='utf-8') | |
font = ImageFont.truetype('/Library/Fonts/sazanami-gothic.ttf', 48, encoding='utf-8') | |
draw = ImageDraw.Draw(img) | |
draw.text((20, 20), u'ぷるむる', font=font, fill='black') | |
draw.text((20, 100), u'復々習レーン', font=font, fill='black') | |
if len(sys.argv) == 2: | |
outfile = sys.argv[1] | |
else: | |
outfile = None | |
save_or_show(img, outfile) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
# | |
# myutil.py | |
# | |
import sys | |
import os | |
import time | |
import Image # PIL | |
def save_or_show(img, outfile=None): | |
if outfile: | |
img.save(outfile) | |
else: | |
outfile = "myutil_tmp.png" | |
img.save(outfile) | |
os.system("open %s" % outfile) | |
time.sleep(1) | |
os.system("rm -f %s" % outfile) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
# | |
# remove_noise.py - ノイズを除去する | |
# | |
# usage: python remove_noise.py <input> [<output>] | |
# | |
import sys | |
import os | |
import Image # PIL | |
from myutil import save_or_show | |
H = 0.01 | |
BETA = 0.1 | |
ETA = 0.1 | |
EPS = 1e-5 | |
def E(xs, ys, width, height): | |
S = width * height | |
s0 = s1 = s2 = 0 | |
for i in range(S): | |
s0 += xs[i] | |
s2 += xs[i] * ys[i] | |
for y in range(height-1): | |
for x in range(width-1): | |
i = y*width + x | |
s1 += xs[i] * xs[i+1] + xs[i] * xs[i+width] | |
e = H * s0 - BETA * s1 - ETA * s2 | |
return e | |
def remove_noise(img): | |
width, height = img.size | |
S = width * height | |
pix = img.load() | |
xs = [0]*S | |
ys = [0]*S | |
for x in range(width): | |
for y in range(height): | |
i = y*width + x | |
xs[i] = ys[i] = 1 if pix[x,y] > 0 else -1 | |
def de(i): | |
s0 = xs[i] | |
s1 = 0 | |
if i > 0: | |
s1 += xs[i] * xs[i-1] | |
if i < S-1: | |
s1 += xs[i] * xs[i+1] | |
if i >= width: | |
s1 += xs[i] * xs[i-width] | |
if i < S-width: | |
s1 += xs[i] * xs[i+width] | |
s2 = xs[i] * ys[i] | |
curr_e = H * s0 - BETA * s1 - ETA * s2 | |
toggled_e = -curr_e | |
return toggled_e < curr_e | |
def reflect(): | |
for i in range(S): | |
x = i % width | |
y = i / width | |
pix[x,y] = 255 if xs[i] == 1 else 0 | |
energy = E(xs, ys, width, height) | |
print 0, energy | |
for j in range(10): | |
for i in range(S): | |
if de(i): xs[i] = -xs[i] | |
new_energy = E(xs, ys, width, height) | |
print 1+j, new_energy | |
if energy - new_energy < EPS: break | |
energy = new_energy | |
reflect() | |
return img | |
if __name__ == '__main__': | |
if len(sys.argv) < 2: | |
print "usage: python %s <input> [<output>]" % sys.argv[0] | |
sys.exit() | |
infile = sys.argv[1] | |
if len(sys.argv) == 3: | |
outfile = sys.argv[2] | |
else: | |
outfile = None | |
img = Image.open(infile) | |
img2 = remove_noise(img) | |
save_or_show(img2, outfile) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
import Image | |
import numpy as np | |
import sys | |
if __name__ == '__main__': | |
if len(sys.argv) < 3: | |
print "usage: python %s <orig> <noised> <removed>" % sys.argv[0] | |
sys.exit() | |
img1 = Image.open(sys.argv[1]) | |
img2 = Image.open(sys.argv[2]) | |
img3 = Image.open(sys.argv[3]) | |
width1, height1 = img1.size | |
width2, height2 = img2.size | |
width3, height3 = img3.size | |
if width1 != width2 or height1 != height2 or width1 != width3 or height1 != height3: | |
print "0.0 - size mismatch." | |
sys.exit() | |
pix1 = img1.load() | |
pix2 = img2.load() | |
pix3 = img3.load() | |
res = [0.0] * 8 | |
for x in range(width1): | |
for y in range(height1): | |
p1 = 1 if pix1[x,y] == 255 else 0 | |
p2 = 1 if pix2[x,y] == 255 else 0 | |
p3 = 1 if pix3[x,y] == 255 else 0 | |
res[p1*4 + p2*2 + p3] += 1 | |
# 1*1 0*0 | |
yes = res[0] + res[2] + res[5] + res[7] | |
no = res[1] + res[3] + res[4] + res[6] | |
rate = yes / (yes + no) | |
print "復元率: %g%%" % (rate * 100) | |
# 0*0, 0*1 | |
ji_yes = res[0] + res[2] | |
ji_no = res[1] + res[3] | |
ji_rate = ji_yes / (ji_yes + ji_no) | |
print "(文字部分復元率: %g%%, " % (ji_rate * 100), | |
# 101, 100 | |
wh_yes = res[5] | |
wh_no = res[4] | |
wh_rate = wh_yes / (wh_yes + wh_no) | |
print "白地部分ノイズ除去率: %g%%)" % (wh_rate * 100) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment