OpenCV检测轮廓交叉点

Mad*_*man 12 c++ opencv

我有2个轮廓A和B,我想检查它们是否相交.A和B都是cv :: Point类型的向量,并且具有不同的大小

为了检查交集,我试图做一个bitwise_and.这是一个例外,因为输入的大小不同.我该如何解决 ?

编辑:

附图应该能够更好地了解这个问题.汽车由蓝色轮廓跟踪,障碍物由粉红色轮廓跟踪.我需要检查交叉点. 在此输入图像描述

mat*_*fee 12

一种简单但可能不是最有效(??)的方法是用于drawContours创建两个图像:一个具有汽车轮廓,另一个具有障碍物轮廓.

然后and他们在一起,任何仍然是积极的点将是交叉点.

一些伪代码(我使用Python接口,所以不能正确使用C++语法,但它应该足够简单,你可以转换):

import numpy as np # just for matrix manipulation, C/C++ use cv::Mat
# find contours.
contours,h = findContours( img, mode=RETR_LIST, method=CHAIN_APPROX_SIMPLE )
# Suppose this has the contours of just the car and the obstacle.

# create an image filled with zeros, single-channel, same size as img.
blank = np.zeros( img.shape[0:2] )

# copy each of the contours (assuming there's just two) to its own image. 
# Just fill with a '1'.
img1 = drawContours( blank.copy(), contours, 0, 1 )
img2 = drawContours( blank.copy(), contours, 1, 1 )

# now AND the two together
intersection = np.logical_and( img1, img2 )

# OR we could just add img1 to img2 and pick all points that sum to 2 (1+1=2):
intersection2 = (img1+img2)==2
Run Code Online (Sandbox Code Playgroud)

如果我看一下,intersection我将得到一个图像,其中轮廓相交,其他地方为0.

或者,您可以填充整个轮廓(不仅是轮廓,也可以填充内部),drawContours( blank.copy(), contours, 0, 1, thickness=-1 )然后intersection图像将包含轮廓之间的交叉区域.