gpt4 book ai didi

python - 手指宽度(计算机视觉)

转载 作者:行者123 更新时间:2023-12-02 17:30:32 25 4
gpt4 key购买 nike

我正在尝试计算图像中手指的宽度。
我所做的是将图像二值化,这使我的手变成了白色,背景变成了黑色。

我将y轴固定为:y = 156
被二值化的图像称为脱粒

为什么我没有得到打印结果(宽度)

这个想法是在x轴上循环,在我的示例中,我选择了范围(x,xD)
然后我寻找从黑色到白色以及从白色到黑色过渡的索引。

for i in range(x,xD):
if thresh[i-1][156]==0 and thresh[i][156]==255:
b2w = i
if thresh[i-1][156]==255 and thresh[i][156]==0:
w2b = i
width = w2b - b2w
print(width)

最佳答案

您的问题是,除非图片中没有清楚的起点和终点,否则循环将失败。如果您的手指一直延伸到边缘,则永远不会满足thresh[i] == 255 and thresh[i-1] == 0评估为True的条件。在您通过Google文档共享的数据集中,这正是发生的情况。

有很多方法可以解决这个问题。一种是将列表索引方法与异常处理一起使用。您也可以循环检查是否在最后。诸如numpy和pandas之类的软件包将提供其他工具。这是一个使用列表索引方法显示的片段,并捕捉到您碰到行尾而实际上没有边界的情况:

# lst = [x[156] for x in thresh]  # to actually pull the slice from your data set
# your data pasted from Google Docs
lst = [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, 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, 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, 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, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255]

# so you can change it to what you want
start = 0
end = len(lst)
# end = 10 # will trigger the first except clause printing finger is not in the slice

try:
first_255 = lst.index(255, start, end)
try:
last_255 = lst.index(0, first_255, end)
except ValueError:
last_255 = end
print(f"width: {last_255 - first_255}")
print(lst[first_255:last_255])
except ValueError:
print('finger is not in the slice')
first_255 = None
last_255 = None

打印品:
>>> width: 408
>>> [255, 255, 255, ... 255, 255]

关于python - 手指宽度(计算机视觉),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56061373/

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