当前位置: 首页 > 面试题库 >

检查两个轮廓是否相交?

柯浩壤
2023-03-14
问题内容

我收到了2个轮廓(cont1cont2cv2.findContours()。我怎么知道它们是否相交?我不需要坐标,我只需要一个布尔值TrueFalse

我尝试了不同的方式,并且已经尝试与

if ((cont1 & cont2).area() > 0):

…但是得到的错误是数组没有方法“ Area()”

...
cont1array = cv2.findContours(binary1, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)[0]
cont2array = cv2.findContours(binary2, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)[0]
...

for cont1 in cont1array:
  for cont2 in cont2array:
    print("cont1")
    print(cont1)
    print(type(cont1))
    print("cont2")
    print(cont2)
    print(type(cont2))
>   if cont1 and cont2 intersect: #i dont know how check intersect
      print("yes they intersect")
    else:
      print("no they do not intersect")

# cont1
# [[172 302]
#  [261 301]
#  [262 390]
#  [173 391]]
# <class 'numpy.ndarray'>
# cont2
# [[  0   0]
#  [  0 699]
#  [499 699]
#  [499   0]]
# <class 'numpy.ndarray'>

问题答案:

一旦有了的两个轮廓cv2.findContours(),就可以使用按位AND运算来检测相交。具体来说,我们可以使用np.logical_and()。想法是为每个轮廓创建两个单独的图像,然后AND对它们使用逻辑运算。具有正值(1True)的任何点都将是交点。因此,由于您只想获取是否存在相交的布尔值,因此我们可以检查相交的图像以查看是否存在单个正值。本质上,如果整个数组都False存在,则轮廓之间就没有交集。但是,如果只有一个True,则轮廓会接触并因此相交。

def contourIntersect(original_image, contour1, contour2):
    # Two separate contours trying to check intersection on
    contours = [contour1, contour2]

    # Create image filled with zeros the same size of original image
    blank = np.zeros(original_image.shape[0:2])

    # Copy each contour into its own image and fill it with '1'
    image1 = cv2.drawContours(blank.copy(), contours, 0, 1)
    image2 = cv2.drawContours(blank.copy(), contours, 1, 1)

    # Use the logical AND operation on the two images
    # Since the two images had bitwise and applied to it,
    # there should be a '1' or 'True' where there was intersection
    # and a '0' or 'False' where it didnt intersect
    intersection = np.logical_and(image1, image2)

    # Check if there was a '1' in the intersection
    return intersection.any()

原始图片

在此处输入图片说明

检测到轮廓

在此处输入图片说明

现在,我们将两个检测到的轮廓传递给函数,并获得此交集数组:

[[False False False ... False False False]
 [False False False ... False False False]
 [False False False ... False False False]
 ...
 [False False False ... False False False]
 [False False False ... False False False]
 [False False False ... False False False]]

我们检查intersection数组以查看是否True存在。我们将获得一个True1轮廓相交的地方,False0它们不相交的地方。

return intersection.any()

这样我们得到

完整代码

import cv2
import numpy as np

def contourIntersect(original_image, contour1, contour2):
    # Two separate contours trying to check intersection on
    contours = [contour1, contour2]

    # Create image filled with zeros the same size of original image
    blank = np.zeros(original_image.shape[0:2])

    # Copy each contour into its own image and fill it with '1'
    image1 = cv2.drawContours(blank.copy(), contours, 0, 1)
    image2 = cv2.drawContours(blank.copy(), contours, 1, 1)

    # Use the logical AND operation on the two images
    # Since the two images had bitwise AND applied to it,
    # there should be a '1' or 'True' where there was intersection
    # and a '0' or 'False' where it didnt intersect
    intersection = np.logical_and(image1, image2)

    # Check if there was a '1' in the intersection array
    return intersection.any()

original_image = cv2.imread("base.png")
image = original_image.copy()

cv2.imshow("original", image)
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
cv2.imshow("gray", gray)
blurred = cv2.GaussianBlur(gray, (5,5), 0)
cv2.imshow("blur", blurred)
threshold = cv2.threshold(blurred, 60, 255, cv2.THRESH_BINARY)[1]
cv2.imshow("thresh", threshold)

contours = cv2.findContours(threshold.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
# Depending on OpenCV version, number of arguments return by cv.findContours 
# is either 2 or 3
contours = contours[1] if len(contours) == 3 else contours[0]

contour_list = []
for c in contours:
    contour_list.append(c)
    cv2.drawContours(image, [c], 0, (0,255,0), 2)

print(contourIntersect(original_image, contour_list[0], contour_list[1]))
cv2.imshow("contour", image)
cv2.waitKey(0)


 类似资料:
  • 问题内容: 我想检查两个数组是否相等。我的意思是:相同的大小,相同的索引,相同的值。我怎样才能做到这一点? 根据用户的建议,如果数组中的至少一个元素不同,我希望以下内容可以打印 enter ,但实际上没有。 问题答案: $arraysAreEqual = ($a == $b); // TRUE if $a and $b have the same key/value pairs. $arraysA

  • 问题内容: 我想知道如何检查两个功能是否相同。一个示例将评估为true。据我所知,Python将检查函数是否在内存中占据相同的位置,而不是它们是否具有相同的操作。我知道拥有该功能似乎不切实际。 另一个解决方案是我可以在函数上运行以查看其包含的内容或工作方式的某些方法。因此,其中一种将返回该方法的工作方式,可能是在字典中还是在某种形式中。 我希望得到一个答案,但我怀疑这是可能的。 问题答案: 如果您

  • 假设我有一组数组,包括和,我想检查它们是否相等。一般来说,我可以只使用(除了一些我现在忽略的愚蠢的情况)。 但是,这会计算的整个数组,这通常是不需要的。我的数组非常大,而且我有很多数组,两个数组相等的概率很小,所以很可能,在函数返回False之前,我只需要计算的一小部分,所以这对我来说不是一个最佳解决方案。 我尝试使用内置的函数,并结合: 然而,在两个数组相等的情况下,这似乎要慢得多,总的来说,它

  • 问题内容: 我知道我可以这样做: 然后只需编写语句中所需的代码。 还有其他方法可以检查它们是否相等? 问题答案: 怎么了 if(!Arrays.equals(array1,array2)) 与相同,即是同一数组。这不是大多数人期望的。 比较数组的内容。

  • 我需要一个可以在junit 方法中调用的方法,该方法比较两个布尔值以检查它们是否相等,并返回一个布尔值。例如,类似这样的事情: 如果不相等,则返回false,如果相等,则返回true。我已经检查了布尔类,但是唯一接近的是< code>Boolean.compare(),它返回一个int值,我不能使用这个值。

  • 嗨,我正在尝试解决Udemy练习:编写一个名为hasSharedDigit的方法,其中包含int类型的两个参数。 每个数字应在10(含)-99(含)之间。如果其中一个数字不在范围内,则该方法应返回false。 如果两个数字中都有数字,例如12和23中的2,则该方法应返回true;否则,该方法应返回false。 我一直在得到真实,而有共享数字(9,99)我无法发现为什么.. }