栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

检查两个轮廓是否相交?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

检查两个轮廓是否相交?

一旦有了的两个轮廓

cv2.findContours()
,就可以使用按位
AND
运算来检测相交。具体来说,我们可以使用
np.logical_and()
。想法是为每个轮廓创建两个单独的图像,然后
AND
对它们使用逻辑运算。具有正值(
1
True
)的任何点都将是交点。因此,由于您只想获取是否存在相交的布尔值,因此我们可以检查相交的图像以查看是否存在单个正值。本质上,如果整个数组都
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
存在。我们将获得一个
True
1
轮廓相交的地方,
False
0
它们不相交的地方。

return intersection.any()

这样我们得到

完整代码

import cv2import numpy as npdef 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 3contours = 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)


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/419983.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号