gpt4 book ai didi

python - OpenCV,将对象分成几部分

转载 作者:太空宇宙 更新时间:2023-11-03 21:37:15 27 4
gpt4 key购买 nike

我有以下图片:https://drive.google.com/file/d/0B6NhNcM1nZznQXVUZ01qS0Q3YTA/edit?usp=sharing .

OpenCV(最好是Python)中有没有一个函数可以说把这张图片中的物体分成几部分。例如,第一个对象由两段(或两行)组成,第三个对象由三段(或四段)组成。

如果 OpenCV 没有这样的东西,那么在任何地方都知道这样的算法/函数会很棒。

最佳答案

这个问题可以通过骨架化图像然后使用HoughlinesP来解决。 . Scikit-image有很好的骨架化方法。可以直接找到 14 条线段,如下所示。最后,您将需要遍历并找到哪些线组相交以查看哪些线属于一起。

result

#!/usr/bin/python

from skimage import morphology
import cv2
import math
import numpy as np

im = cv2.imread("objects.png")
dst = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY)

dst = 1 - dst / 255
dst = morphology.skeletonize(dst).astype(np.uint8)

objs = 255 * dst

#cv2.HoughLinesP(image, rho, theta, threshold[, lines[, minLineLength[, maxLineGap]]])
rho = 1
theta = math.pi / 180
threshold = 1
minLineLength = 3
maxLineGap = 5

lines = np.ndarray([1, 1, 4, 4])
lines = cv2.HoughLinesP(dst, rho, theta, threshold, lines, minLineLength, maxLineGap)

lineColor = (0, 255, 0) # red

for line in lines[0]:
#print line
cv2.line(im, (line[0], line[1]), (line[2], line[3]), lineColor, 1, 8)

#
# Now you need to go through lines and find those that intersect
# You will notice that some lines have small gaps where they should
# join to a perpendicular line. Before find intersections you would
# need to make each line longer (just by adjusting the numbers in lines)
# to get around this problem.
#

cv2.imshow('Objects', objs)
cv2.imshow('Lines', im)
cv2.imwrite('lines.png', im)

cv2.waitKey()
cv2.destroyAllWindows()

关于python - OpenCV,将对象分成几部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20114888/

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