我正在尝试编写一种算法来系统地确定图像中有多少不同的“曲线”。示例图像。我对这里的白线特别感兴趣,所以我使用了颜色阈值来掩盖图像的其余部分,只得到白色像素。这些线代表玩家(NFL中的宽接收器)运行的路径,因此我对路径所代表的 x 和 y 坐标感兴趣 - 每个“曲线”代表玩家采取的不同路径(或“路线”)。所有曲线都应在蓝线之上或之后开始。
然而,虽然我只能得到白色像素,但我无法弄清楚如何系统地识别单独的曲线。在此示例图像中,存在 8 条白色曲线(或路线)。我已经识别出这张图片中的那些曲线。我尝试了边缘检测,然后使用 scipy ndimage 来获取连接组件的数量,但是由于曲线重叠,它将它们计为连接的,并且只给了我这个图像的 3 个标记组件,而不是 8 个。这是边缘检测输出的样子。有没有更好的方法来解决这个问题?这是我的示例代码。
import cv2
from skimage.morphology import skeletonize
import numpy as np
from scipy import ndimage
#Read in image
image = cv2.imread('example_image.jpeg')
#Color boundary to get white pixels
lower_white = np.array([230, 230, 230])
upper_white = np.array([255, 255, 255])
#mask image for white pixels
mask = cv2.inRange(image, lower_white, upper_white)
c_pixels = cv2.bitwise_and(image, image, mask=mask)
#make pixels from 0 to 1 form to use in skeletonize
c_pixels = c_pixels.clip(0,1)
ske_c = skeletonize(c_pixels[:,:,1]).astype(np.uint8)
#Edge Detection
inputImage =ske_c*255
edges = cv2.Canny(inputImage,100,200,apertureSize = 7)
#Show edges
cv2.imshow('edges', edges)
cv2.waitKey(0)
cv2.destroyAllWindows()
#Find number of components
# smooth the image (to remove small objects); set the threshold
edgesf = ndimage.gaussian_filter(edges, 1)
T = 50 # set threshold by hand to avoid installing `mahotas` or
# `scipy.stsci.image` dependencies that have threshold() functions
# find connected components
labeled, nr_objects = ndimage.label(edgesf > T) # `dna[:,:,0]>T` for red-dot case
print("Number of objects is %d " % nr_objects)