gpt4 book ai didi

python - 计算图像中两个像素之间的距离

转载 作者:行者123 更新时间:2023-12-04 08:03:10 28 4
gpt4 key购买 nike

我正在尝试计算两个像素之间的距离,但是以一种特定的方式。我需要知道图像中红线的粗细,所以我的想法是按列遍历图像,找到两个边缘点的坐标并计算它们之间的距离。对顶部和底部的两条线执行此操作。对每一列执行此操作,然后计算平均值。
我还应该进行从像素到实际比例的转换。
这是我现在的代码:

# Make numpy array from image
npimage = np.array(image)

# Describe what a single red pixel looks like
red = np.array([255, 0, 0], dtype=np.uint8)

firs_point = 0
first_find = False
for i in range(image.width):
column = npimage[:,i]
for row in column:
comparison = row == red
equal_arrays = comparison.all()
if equal_arrays == True and first_find == False:
first_x_coord = i
first_find = True
我无法获得坐标。有人能帮助我吗?当然,如果有更优的计算方式,我也很乐意接受建议。我很新!
非常感谢!

最佳答案

使用opencv:

img = cv2.imread(image_path)
average_line_width = np.average(np.count_nonzero((img[:,:,:]==np.array([0,0,255])).all(2),axis=0))/2
print(average_line_width)
使用 pil
img = np.asarray(Image.open(image_path))
average_line_width = np.average(np.count_nonzero((img[:,:,:]==np.array([255,0,0])).all(2),axis=0))/2
print(average_line_width)
两种情况下的输出:
18.430701754385964

关于python - 计算图像中两个像素之间的距离,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66350844/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com