如何获得与另一个像素成一定角度的像素的值? [英] How to get value of pixels which are at a certain angle from another pixel?

查看:48
本文介绍了如何获得与另一个像素成一定角度的像素的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试实现一种方法,其中必须获取所有以一定角度穿过像素(i,j)形成一条线的那些像素的值

I'm trying to implement a method where I have to obtain the values of all those pixels which forms a line at a certain angle through a pixel (i, j)

考虑以下代码段

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

现在在上面的代码中,我知道相对于i,j的哪些像素以15度角形成线.因此,我能够获得所有这些像素的值.

Now in the above code I know which pixels relative to i, j forms the line at angle 15 degrees. Hence I am able to get the values of all those pixels.

现在有一种简单的方法可以执行此操作,因为当前此代码找到15个像素的灰度级总和,这些像素以15度通过i,j形成了这条线.我希望此代码具有灵活性,以便可以轻松找到长度为15像素,13像素或11像素等的行的总和.

Now is there an easy way to do this because currently this code find the sum of the grey level of 15 pixels that forms this line at 15 degree through i, j. I want this code to be flexible so that I can easily find the sum of line of length 15 pixels or 13 pixels or 11 pixels and so on.

推荐答案

您可以使用一些三角函数.回想一下 sin(angle)= y/x ,所以 y = x * sin(angle).只需创建一个 x 值数组即可,然后将其插入 y 值的公式即可.然后,您当然需要将 y 值取整.然后,您可以将它们全部翻译到该行的起始位置.

You can use some trigonometry. Recall that sin(angle) = y/x, so y = x*sin(angle). Just create an array of x values however long you want, and then plug it into the formula for the y values. You'll of course need to round the y values afterwards. And then you can translate all of them to the starting location for the line.

>>> import numpy as np
>>> angle = 15*np.pi/180
>>> x = np.arange(0,10)
>>> y = np.round(np.sin(angle)*x).astype(int)
>>> [(x,y) for 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)]

这篇关于如何获得与另一个像素成一定角度的像素的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆