在openCV中,基于轮廓/边缘的物体识别的最佳算法是什么

2024-06-16 13:11:35 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在开发一个应用程序,它需要在任何情况下根据对象的轮廓来识别对象。到目前为止,如果物体有一个简单的背景,我可以识别出它们的轮廓,但是如果后面有一个复杂的东西,比如一个头部或一张脸,那么算法就失败了。你知道吗

我使用countures和matchShapes来实现这一点:

framecp = frame
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
frame = np.uint8(frame)

# Defining counturs of the frames
ret, thresh = cv2.threshold(frame, 80, 255, 1)
counturs, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)

# res - result, ind - saved index, index - index of countures in for loop
res = 0.36
ind = 0 
index = 0

# matching every shape with the given sample to find best result.
for shape in counturs:
    if cv2.matchShapes(shape, kcounturs[1], 1, 0.0) < res:
        res = cv2.matchShapes(shape, kcounturs[1], 1, 5)
        ind = index
        print(res)
    index = index + 1

# printing result to console, drawing contours and displaying result.
cv2.drawContours(framecp, counturs, ind, (0, 0, 200), 2)

我也试过用canny,但效果不好。我甚至做了好的拉普拉斯+模糊+平滑得到更明确和厚的边缘,但这也是一个死胡同。那时我决定放弃这个算法。我也尝试了更多的算法,但到目前为止这对我来说效果最好。问题是,为了优化性能,我不需要使用像ORB、SURF或SIFT这样的权重算法,我只需要对象的边缘,然后在帧中找到边缘或轮廓。你知道吗


Tags: 对象算法indexresresultcv2frame边缘