Created
December 2, 2015 06:32
-
-
Save sysatom/1bcd93a021dc853622c3 to your computer and use it in GitHub Desktop.
hamming
This file contains 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
#encoding:utf-8 | |
import Image | |
img = 'c5c07bd19d6a01def7a8920a805b5376_m.jpg' | |
im = Image.open(img) | |
im = im.resize((8, 8), Image.ANTIALIAS).convert('L') | |
print im.getdata() | |
# 平均值 | |
avg = reduce(lambda x, y: x + y, im.getdata()) / 64. | |
val = reduce(lambda x, (y, z): x | (z << y), | |
enumerate(map(lambda i: 0 if i < avg else 1, im.getdata())), 0) | |
# print im.histogram() | |
print val | |
def hamming(h1, h2): | |
h, d = 0, h1 ^ h2 | |
while d: | |
h += 1 | |
d &= d - 1 | |
return h | |
def hamdist(str1, str2): | |
diffs = 0 | |
for ch1, ch2 in zip(str1, str2): | |
if ch1 != ch2: | |
diffs += 1 | |
return diffs | |
def hamming_distance(s1, s2): | |
assert len(s1) == len(s2) | |
return sum(ch1 != ch2 for ch1, ch2 in zip(s1, s2)) | |
print hamdist('1011101', '1001001') | |
print hamming_distance('1011101', '1001001') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment