Created
June 1, 2012 04:36
-
-
Save mecab/2848805 to your computer and use it in GitHub Desktop.
cv4
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 itertools | |
image = [[10,10,10,10,10,10,10,10,10], | |
[10,10,10,10,10,10,10,10,10], | |
[10,10,10,10,10,250,250,250,250], | |
[10,10,10,10,10,250,250,250,250], | |
[10,10,250,250,250,250,250,250,250], | |
[10,10,10,250,250,250,250,250,250], | |
[10,10,10,10,250,250,250,250,250], | |
[10,10,10,10,250,250,250,250,250], | |
[10,10,10,10,250,250,250,250,250]] | |
def apply_moravec(): | |
value = [[0] * 9 for _ in range(9)] | |
for y, x in itertools.product(range(9), range(9)): | |
v = image[y][x] | |
sum_ = 0 | |
for ty, tx in itertools.product([y - 1, y, y + 1], [x - 1, x, x + 1]): | |
if tx < 0 or ty < 0 or tx >= 9 or ty >= 9: | |
continue | |
tv = image[ty][tx] | |
sum_ += (tv - v) ** 2 | |
value[y][x] = sum_ / 8 | |
return value | |
def apply_susan(): | |
thresh = 10 | |
value = [[0] * 9 for _ in range(9)] | |
for y, x in itertools.product(range(9), range(9)): | |
v = image[y][x] | |
count = 0 | |
for ty, tx in itertools.product([y - 1, y, y + 1], [x - 1, x, x + 1]): | |
if tx < 0 or ty < 0 or tx >= 9 or ty >= 9: | |
count += 1 | |
continue | |
tv = image[ty][tx] | |
if abs(tv - v) < 10: | |
count += 1 | |
value[y][x] = count | |
return value | |
def apply_fast(): | |
thresh = 10 | |
value = [[0] * 9 for _ in range(9)] | |
for y, x in itertools.product(range(9), range(9)): | |
v = image[y][x] | |
pos = [(y - 1, x - 1), (y - 1, x), (y - 1, x + 1), (y , x + 1), (y + 1, x + 1), (y + 1, x), (y + 1, x - 1), (y, x - 1)] | |
for i in range(len(pos)): | |
for j in range(5): | |
k = (i + j) % 8 | |
ty, tx = pos[k] | |
if tx < 0 or ty < 0 or tx >= 9 or ty >= 9: | |
break | |
tv = image[ty][tx] | |
if abs(tv - v) >= 10: | |
pass | |
else: | |
break | |
else: | |
value[y][x] = 1 | |
break | |
return value | |
def apply_tomashi_kanade(): | |
import math | |
def _dydy(): | |
prev = image[y - 1][x] if (y - 1) > 0 else image[y][x] | |
next = image[y + 1][x] if (y + 1) < 9 else image[y][x] | |
return prev - 2 * image[y][x] + next | |
def _dxdx(): | |
prev = image[y][x - 1] if (x - 1) > 0 else image[y][x] | |
next = image[y][x + 1] if (x + 1) < 9 else image[y][x] | |
return prev - 2 * image[y][x] + next | |
def _dxdy(): | |
prevx = image[y][x - 1] if (x - 1) > 0 else image[y][x] | |
prevy = image[y - 1][x] if (y - 1) > 0 else image[y][x] | |
return prevx - 2 * image[y][x] + prevy | |
value = [[0] * 9 for _ in range(9)] | |
for y, x in itertools.product(range(9), range(9)): | |
dydy = _dydy() | |
dxdx = _dxdx() | |
dxdy = _dxdy() | |
t = dxdx + dydy | |
l1 = abs(t + math.sqrt(t ** 2 - 4 * (t - dxdy ** 2))) | |
l2 = abs(t - math.sqrt(t ** 2 - 4 * (t - dxdy ** 2))) | |
value[y][x] = min(l1, l2) | |
return value | |
if __name__ == '__main__': | |
print apply_moravec() | |
print apply_susan() | |
print apply_fast() | |
print [map(lambda y: round(y, 1), x) for x in apply_tomashi_kanade()] | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
[[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], [0.0, 0.0, 0.0, 0.0, 0.0, 240.0, 148.328, 148.328, 148.328], [0.0, 0.0, 0.0, 0.0, 240.0, 148.328, 0.0, 0.0, 0.0], [0.0, 0.0, 134.773, 148.328, 148.328, 0.0, 0.0, 0.0, 0.0], [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], [0.0, 0.0, 0.0, 0.0, 148.328, 0.0, 0.0, 0.0, 0.0], [0.0, 0.0, 0.0, 0.0, 148.328, 0.0, 0.0, 0.0, 0.0], [0.0, 0.0, 0.0, 0.0, 148.328, 0.0, 0.0, 0.0, 0.0]]