平均发光值与到图像中心的距离

时间:2018-07-19 08:12:20

标签: python image-processing average distance

我想计算平均发光值与到图像中心的距离。我正在考虑的方法是

  1. 计算图像像素与图像中心之间的距离
  2. 将相同距离的像素分组
  3. 计算每组像素的平均值
  4. 距离与平均强度的关系图

要计算第一步,请使用以下功能:

dist_img = np.zeros(gray.shape, dtype=np.uint8)
for y in range(0, h):
    for x in range(0, w):
        cy = gray.shape[0]/2
        cx = gray.shape[1]/2

        dist = math.sqrt(((x-cx)**2)+((y-cy)**2))
        dist_img[y,x] = dist

不幸的是,id给出的结果与我从此处计算出的结果不同

distance = math.sqrt(((1 - gray.shape[0]/2)**2 )+((1 - gray.shape[1]/2 )**2))

当我测试像素(1,1)时,我从第一个代码收到20,从第二个代码收到3605。 我希望您能提出有关如何纠正循环的建议,以及有关如何从其他观点入手的提示。或者也许还有其他方法可以实现我想要的目标。

1 个答案:

答案 0 :(得分:0)

您正在使用dist_img dtype设置np.uint8。该8位无符号整数可以适合0到255之间的值,因此3605无法正确表示。对距离图像dtype使用更高的位深度,例如np.uint32

distance = math.sqrt(((1 - gray.shape[0]/2)**2 )+((1 - gray.shape[1]/2 )**2))

小心:gray.shape将为您提供(高度,宽度)或(y,x)。其他代码正确地将gray.shape[0]/2分配给y中心,该代码将其混合并使用x坐标的高度。

您的算法似乎足够好,我建议您坚持使用它。您可以通过将图像转换为极坐标空间(例如使用OpenCV linearToPolar)来实现与前两个步骤相似的操作,但这可能很难调试。

相关问题