python - 有没有办法找到轮廓之间的距离?
问题描述
我通过python(opencv)获得了二进制图像,并尝试找到每个轮廓之间的距离,如下所示。对于这个例子,
我会尝试像这样测量每条线的每个轮廓距离:
有什么方法可以捕捉每个轮廓点并获得我标记为红色箭头的距离并延伸到每条线(1st,2nd .....nth)
我尝试使用 for 循环来捕捉每个点,但我不知道如何像这样自动测量距离 (1st<-->2nd,2nd<--->3rd,。。。。。。nth-1< -->第n个)
解决方案
如果预期的输入始终是条纹图案,您可以逐行遍历像素。当一个像素与前一个不同时,请注意相等的像素数。
我创建了一个示例,为了清楚起见,我使用了原始图像的一小部分。
输入:
结果:
[[6, 10, 8, 10, 8, 8],
[6, 10, 8, 10, 8, 8],
[6, 10, 8, 10, 8, 8],
[7, 9, 8, 10, 8, 8],
[7, 9, 8, 10, 8, 8]]
代码:
import cv2
# load image as grayscale
img = cv2.imread('YDsdI.png',0)
# create small subimage
subimg = img[100:105,95:150]
# create empty list
all_distances = []
# loop all lines, then each pixel
# if the current pixel differs from the previous,
# then append the number of pixel that where equal
for line in subimg:
start = 0
line_distances = []
for i in range(1,len(line)):
if line[i] != line[i-1]:
line_distances.append(i-start)
start = i
all_distances.append(line_distances)
# print result
print(all_distances)
# draw gray rectangle around the smaller subimage
cv2.rectangle(img,(95,100),(150,105),(127),2)
# show image
cv2.imshow('Img',img)
cv2.waitKey(0)
cv2.destroyAllWindows()
推荐阅读
- ios - 如何在 ios 中使用带有域的 pjsip 添加对 IPv6 的支持?
- python - Python 记录器转储自“
" 到新文件 - javascript - 如果错误,如何检查无线电组中的第二个选项?
- mongodb - Mongodb主副本正在生成大量日志
- mysql - TypeORM 与 MySQL - 用大长文本保存对象失败
- arrays - 当我尝试在菜单驱动的数组操作程序中调用插入或删除函数时,谁能告诉我为什么我的程序崩溃?
- html - 使用 flexbox 并排放置两个 p 标签
- android - Android MotionLayout + Recyclerview,recyclerview中的视图不可点击
- asynchronous - 您如何从反应式微服务系统中获取数据?
- javascript - 不同端口上的不同 Tailwind 行为