Skip to content

Instantly share code, notes, and snippets.

@arsho
Last active September 28, 2020 06:22
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save arsho/251ff14df1c0ed88c469bad17c767efe to your computer and use it in GitHub Desktop.
Save arsho/251ff14df1c0ed88c469bad17c767efe to your computer and use it in GitHub Desktop.
import numpy as np
import sys
def get_pixel(img, x, y, height, width):
if x<0 or y<0 or x>=height or y>=width:
return 0
else:
return img[x][y]
ar = [[18, 25, 14], [85, 25, 86], [45, 65, 14]]
height = len(ar)
width = len(ar[0])
new_ar = np.zeros((height, width), np.uint8)
for i in range(height):
for j in range(width):
p = ar[i][j]
a1 = get_pixel(ar, i-1, j-1, height, width)
a2 = get_pixel(ar, i-1, j+1, height, width)
a3 = get_pixel(ar, i+1, j+1, height, width)
a4 = get_pixel(ar, i+1, j-1, height, width)
b1 = get_pixel(ar, i-1, j, height, width)
b2 = get_pixel(ar, i, j+1, height, width)
b3 = get_pixel(ar, i+1, j, height, width)
b4 = get_pixel(ar, i, j-1, height, width)
#print(i,j,p,a1,b1,a2,b2,a3,b3,a4,b4,file=sys.stderr)
s = ""
if a1>a3:
s+="1"
else:
s+="0"
if a2>a4:
s+="1"
else:
s+="0"
if b1>b3:
s+="1"
else:
s+="0"
if b2>b4:
s+="1"
else:
s+="0"
s = int(s,2)
new_ar[i][j] = s
print(new_ar)
'''
[[ 1 0 0]
[ 5 9 8]
[ 7 14 10]]
[[ 1 0 1 1 0 0 1 1 0]
[ 7 10 14 15 14 14 7 7 10]
[ 7 14 14 14 14 14 15 15 10]]
if
ar = [
[88, 82, 84, 88, 95, 83, 80, 98, 102],
[88, 80, 78, 80, 80, 78, 73, 94, 100],
[85, 79, 80, 78, 77, 74, 65, 91, 99]
]
'''
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment