Skip to content

Instantly share code, notes, and snippets.

@a-recknagel
Created November 8, 2018 18:31
Show Gist options
  • Save a-recknagel/28fc953b8da407e290fec09310a79b00 to your computer and use it in GitHub Desktop.
Save a-recknagel/28fc953b8da407e290fec09310a79b00 to your computer and use it in GitHub Desktop.
shifted image similarity feature
image_a = [
[1,0,0,0,1,0,1],
[0,1,0,1,0,0,1],
[0,1,0,0,0,1,1],
[0,0,1,1,1,0,0],
[0,0,0,0,1,1,1],
[0,1,0,0,0,1,1],
[0,0,0,1,1,1,0],
]
image_b = [
[0,0,0,1,0,1,1],
[1,0,1,0,0,1,0],
[1,0,0,0,1,1,0],
[0,1,1,1,0,0,0],
[0,0,0,1,1,1,0],
[1,0,0,0,1,1,0],
[0,0,1,1,1,0,0],
]
def chunkify(image, chunk_size=3):
border = chunk_size//2
for i in range(border, len(image)-border):
for j in range(border, len(image[i])-border):
#print(j, border)
yield [
image[i-border][j-border:j+border+1],
image[border][j-border:j+border+1],
image[i+border][j-border:j+border+1]
]
def compare_images(image, other):
chunks = [*chunkify(image)] # this should be a counter-dict
total_count = len(chunks)
same_count = 0
for chunk in chunkify(other):
try:
chunks.remove(chunk)
same_count += 1
except ValueError:
pass
return same_count / total_count
print(compare_images(image_a, image_b)) # 0.84
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment