python - How to get value of pixels which are at a certain angle from another pixel? -
i'm trying implement method have obtain values of pixels forms line @ angle through pixel (i, j)
consider following code snippet
sum = image.getpixel(((i - 7), (j + 2))) + image.getpixel(((i - 6), (j + 2))) + image.getpixel( ((i - 5), (j + 1))) + image.getpixel( ((i - 4), (j + 1))) + image.getpixel(((i - 3), (j + 1))) + image.getpixel(((i - 2), (j + 1))) + image.getpixel( ((i - 1), j)) + image.getpixel((i, j)) + image.getpixel(((i + 1), j)) + image.getpixel( ((i + 2), (j - 1))) + image.getpixel(((i + 3), (j - 1))) + image.getpixel(((i + 4), (j - 1))) + image.getpixel( ((i + 5), (j - 1))) + image.getpixel(((i + 6), (j - 2))) + image.getpixel(((i + 7), (j - 2))) avg_sum = sum / 15
now in above code know pixels relative i, j forms line @ angle 15 degrees. hence able values of pixels.
now there easy way because code find sum of grey level of 15 pixels forms line @ 15 degree through i, j. want code flexible can find sum of line of length 15 pixels or 13 pixels or 11 pixels , on.
you can use trigonometry. recall sin(angle) = y/x
, y = x*sin(angle)
. create array of x
values long want, , plug formula y
values. you'll of course need round y
values afterwards. , can translate of them starting location line.
>>> import numpy np >>> angle = 15*np.pi/180 >>> x = np.arange(0,10) >>> y = np.round(np.sin(angle)*x).astype(int) >>> [(x,y) x, y in zip(x, y)] [(0, 0), (1, 0), (2, 1), (3, 1), (4, 1), (5, 1), (6, 2), (7, 2), (8, 2), (9, 2)]
Comments
Post a Comment