Python 如何知道一个边框(矩形)是否位于另一个边框(矩形)内?
我试图把盒子(坐标)放在外盒子里。我已经使用了联合交集方法,我希望其他方法也这样做 另外,您能告诉我如何比较这两个内框吗?您可以使用 在您发布的图像中,内部矩形是具有父对象和子对象的轮廓(使用Python 如何知道一个边框(矩形)是否位于另一个边框(矩形)内?,python,opencv,bounding-box,Python,Opencv,Bounding Box,我试图把盒子(坐标)放在外盒子里。我已经使用了联合交集方法,我希望其他方法也这样做 另外,您能告诉我如何比较这两个内框吗?您可以使用 在您发布的图像中,内部矩形是具有父对象和子对象的轮廓(使用findContours和RETR_TREE标志时) 下面是一个代码示例: import cv2 # Read input image img = cv2.imread("boxes.png") # Convert to gray gray = cv2.cvtColor(img, cv2.COLOR_B
findContours
和RETR_TREE
标志时)
下面是一个代码示例:
import cv2
# Read input image
img = cv2.imread("boxes.png")
# Convert to gray
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# Apply threshold
_, thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
# Find contours and hierarchy
cnts, hier = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)[-2:] # Use [-2:] for OpenCV 3, 4 compatibility.
# Draw all contours with green color for testing
cv2.drawContours(img, cnts, -1, (0, 255, 0))
# Hierarchy Representation in OpenCV
# So each contour has its own information regarding what hierarchy it is,
# who is its child, who is its parent etc.
# OpenCV represents it as an array of four values : [Next, Previous, First_Child, Parent]
# Iterate contours and hierarchy:
for c, h in zip(cnts, hier[0]):
# Check if contour has one partent and one at least on child:
if (h[3] > 0) and (h[2] > 0):
# Get bounding rectange
x, y, w, h = cv2.boundingRect(c)
# Draw red rectange for testing
cv2.rectangle(img, (x, y), (x+w, y+h), (0, 0, 255), thickness=1)
# Show result for testing
cv2.imshow('img', img)
cv2.waitKey(0)
cv2.destroyAllWindows()
结果:
更新: 我在上面的代码中有一个bug(使用
>0
而不是=0
)。我们还需要验证家长是否有家长。
看起来有点过分了
# Iterate contours and hierarchy:
for c, h in zip(cnts, hier[0]):
# Check if contour has one partent and one at least on child:
if (h[3] >= 0) and (h[2] >= 0):
# Get the partent from the hierarchy
hp = hier[0][h[3]]
# Check if the parent has a parent:
if hp[3] >= 0:
# Get bounding rectange
x, y, w, h = cv2.boundingRect(c)
# Draw red rectange for testing
cv2.rectangle(img, (x, y), (x+w, y+h), (0, 0, 255), thickness=1)
你可以用
在您发布的图像中,内部矩形是具有父对象和子对象的轮廓(使用findContours
和RETR_TREE
标志时)
下面是一个代码示例:
import cv2
# Read input image
img = cv2.imread("boxes.png")
# Convert to gray
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# Apply threshold
_, thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
# Find contours and hierarchy
cnts, hier = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)[-2:] # Use [-2:] for OpenCV 3, 4 compatibility.
# Draw all contours with green color for testing
cv2.drawContours(img, cnts, -1, (0, 255, 0))
# Hierarchy Representation in OpenCV
# So each contour has its own information regarding what hierarchy it is,
# who is its child, who is its parent etc.
# OpenCV represents it as an array of four values : [Next, Previous, First_Child, Parent]
# Iterate contours and hierarchy:
for c, h in zip(cnts, hier[0]):
# Check if contour has one partent and one at least on child:
if (h[3] > 0) and (h[2] > 0):
# Get bounding rectange
x, y, w, h = cv2.boundingRect(c)
# Draw red rectange for testing
cv2.rectangle(img, (x, y), (x+w, y+h), (0, 0, 255), thickness=1)
# Show result for testing
cv2.imshow('img', img)
cv2.waitKey(0)
cv2.destroyAllWindows()
结果:
更新: 我在上面的代码中有一个bug(使用
>0
而不是=0
)。我们还需要验证家长是否有家长。
看起来有点过分了
# Iterate contours and hierarchy:
for c, h in zip(cnts, hier[0]):
# Check if contour has one partent and one at least on child:
if (h[3] >= 0) and (h[2] >= 0):
# Get the partent from the hierarchy
hp = hier[0][h[3]]
# Check if the parent has a parent:
if hp[3] >= 0:
# Get bounding rectange
x, y, w, h = cv2.boundingRect(c)
# Draw red rectange for testing
cv2.rectangle(img, (x, y), (x+w, y+h), (0, 0, 255), thickness=1)
通过比较边界框和内部框的左上角和右下角的坐标,很容易知道后者是否在前者内部 以下代码是一个简单的示例,其中只有一个边界框和一个内部框:
#边界框
边界b={
“x”:150,
“y”:150,
身高:50,,
“宽度”:100
}
#内盒
innerb={
“x”:160,
y:160,
身高:25,,
“宽度”:25
}
#如果左上角的内框角位于边界框内
如果boundb['x']
通过比较边界框和内部框的左上角和右下角的坐标,很容易知道后者是否在前者内部
以下代码是一个简单的示例,其中只有一个边界框和一个内部框:
#边界框
边界b={
“x”:150,
“y”:150,
身高:50,,
“宽度”:100
}
#内盒
innerb={
“x”:160,
y:160,
身高:25,,
“宽度”:25
}
#如果左上角的内框角位于边界框内
如果boundb['x']
我不明白你的问题。。。如果您已经使用了交叉路口。。。现在怎么了?@Kevinguetchouang我想用其他方法来做我不明白你的问题。。。如果您已经使用了交叉路口。。。现在怎么了?@KevinNguetchouang我想用其他方法来做