Last active
September 28, 2020 06:22
-
-
Save arsho/251ff14df1c0ed88c469bad17c767efe to your computer and use it in GitHub Desktop.
lgp implementation based on http://stackoverflow.com/questions/43920567/local-gradient-pattern-implementation/
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
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