【Python】【功能模块化】判断多个目标检测的框与分割的掩码轮廓是否相交,并输出相交面积最大的框

为综合考虑目标检测和目标分割的结果,会将检测框和掩码轮廓综合考虑进行逻辑判断,需要判断目标框与掩码是否相交。本文对目标框与轮廓是否相交、相交面积对比、输出相交面积最大的目标框索引等功能进行实现,并对实现过程模块化前后代码进行展示,以及添加相应效果帮助大家理解和使用。最后把功能模块化,方便复用和调用。

模块化前

功能实现步骤

步骤1:读取图像,对图像进行二值化,并找出图像中的所有轮廓。
步骤2:判断轮廓大小,找出最大轮廓。
步骤3:定义目标框,格式(x1, y2, x2, y2),其中(x1, y1)是左上角的坐标,(x2,y2)是右下角坐标。
步骤4:把每个目标框的掩码画出来,中间区域填充。
步骤5:判断轮廓掩码与目标框掩码是否相交。
步骤6:若相交,则计算相交部分的轮廓和面积。
步骤7:判断相交面积与上一个相交面积的大小,保存最大面积和最大面积的目标框索引。
步骤8:若有多个目标框,循环步骤4、步骤5和步骤6。
步骤9:输出最大面积和最大面积的目标框索引。

代码示例

代码

import cv2
import numpy as np

# 读取图像
image = cv2.imread('image.jpg')
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

# 二值化图像以获取轮廓
_, thresh = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY)

# 查找轮廓
contours, _ = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)

# 获取最大轮廓
if contours:
    max_contour = max(contours, key=cv2.contourArea)
else:
    print("No contours found in the image.")
    exit()

# 定义目标框列表(矩形框的左上角和右下角坐标)

    # 定义目标框列表
bounding_boxes = [
    (571, 576, 902, 1017),  # 示例目标框1
    (527, 317, 713, 395),
    (146, 211, 226, 359),
    (764, 77, 1122, 700),
    # ... 可以添加更多目标框

]

# 初始化最大相交面积和目标框索引
max_intersection_area = 0
max_intersection_box_index = None

# 遍历每个目标框
for i, (x1, y1, x2, y2) in enumerate(bounding_boxes):
    # 创建一个与原图大小相同的空白图像,用于绘制目标框
    box_mask = np.zeros_like(thresh)

    # 在空白图像上绘制目标框
    cv2.rectangle(box_mask, (x1, y1), (x2, y2), 255, thickness=-1)
    name=str(i)+".jpg"
    cv2.imwrite(name,box_mask)
    # 计算轮廓与目标框的交集
    intersection = cv2.bitwise_and(thresh, box_mask)

    # 如果交集不为空,则计算交集面积
    if np.any(intersection):
        # 计算交集轮廓
        intersection_contours, _ = cv2.findContours(intersection, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
        if intersection_contours:
            # 选择交集轮廓中的最大轮廓来计算面积
            intersection_contour = max(intersection_contours, key=cv2.contourArea)
            # 计算交集轮廓的边界框面积作为交集面积
            x, y, w, h = cv2.boundingRect(intersection_contour)
            intersection_area = w * h

            # 更新最大相交面积和目标框索引
            if intersection_area > max_intersection_area:
                max_intersection_area = intersection_area
                max_intersection_box_index = i

# 输出结果
if max_intersection_box_index is not None:
    print(
        f"The largest contour intersects with box {max_intersection_box_index} with the largest intersection area of {max_intersection_area}.")
else:
    print("No bounding box intersects with the largest contour.")

请确保替换path_to_your_image.jpg为你的图像路径,并根据实际情况调整bounding_boxes列表中的目标框坐标。

这段代码首先读取图像并转换为灰度图像,然后进行二值化以得到轮廓。之后,它查找所有轮廓并选择面积最大的轮廓。然后,对于每个目标框,它创建一个掩码图像,其中目标框的区域被填充为白色,其余为黑色。接着,它计算轮廓掩码与目标框掩码的交集,并从这个交集中提取轮廓。最后,它计算交集轮廓的边界框面积作为交集面积,并更新最大相交面积和目标框索引。

注意,这个方法通过计算交集轮廓的边界框面积来近似表示交集面积,这可能不是最精确的方法,但它通常能够提供一个合理的近似值。如果需要更精确的相交面积计算,可能需要采用更复杂的几何计算或像素级别的比较。

效果图像

轮廓区域
在这里插入图片描述

第一个目标框的掩码
在这里插入图片描述
第二个目标框的掩码
在这里插入图片描述

第三个目标框的掩码
在这里插入图片描述

第四个目标框的掩码
在这里插入图片描述

终端输出结果

The largest contour intersects with box 0 with the largest intersection area of 114036.

输出的索引的0,即第0个目标框与轮廓相交,且相交面积最大。

模块化后

功能实现

输入:目标框、轮廓、图像宽、图像高
输出:目标框索引或None

代码

name:B.py

##B.py
import cv2
import numpy as np

def calculate_max_intersection(contour, bounding_boxes, image_width, image_height):
    # 创建一个空白图像用于计算交集,与原始图像同样大小,但全部为黑色
    mask = np.zeros((image_height, image_width), dtype=np.uint8)

    # 在空白图像上绘制轮廓
    cv2.drawContours(mask, [contour], -1, 255, thickness=-1)

    # 初始化最大相交面积和目标框索引
    max_area = 0
    max_box_index = None

    # 计算每个目标框与轮廓的相交面积
    for i, (x1, y1, x2, y2) in enumerate(bounding_boxes):
        # 检查目标框是否在图像边界内


        # 创建一个与目标框同样大小的掩码
        box_mask = np.zeros_like(mask)
        cv2.rectangle(box_mask, (x1, y1), (x2, y2), 255, thickness=-1)

        # 计算轮廓掩码与目标框掩码的交集
        intersection = cv2.bitwise_and(mask, box_mask)

        # 计算交集区域的面积
        intersection_area = cv2.countNonZero(intersection)

        # 更新最大相交面积和目标框索引
        if intersection_area > max_area:
            max_area = intersection_area
            max_box_index = i
    if max_box_index is None:
        return None
    return max_box_index



name:A.py

##A.py
from B import calculate_max_intersection
import cv2


image = cv2.imread('image.jpg')
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

# 二值化图像以获取轮廓
_, thresh = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY)

# 查找轮廓
contours, _ = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)

# 获取最大轮廓
if contours:
    contour = max(contours, key=cv2.contourArea)
else:
    print("No contours found in the image.")
    exit()

bounding_boxes = [
    (571, 576, 902, 1017),  # 示例目标框1
    (527, 317, 713, 395),
    (146, 211, 226, 359),
    (764, 77, 1122, 700),
    # ... 可以添加更多目标框

]

image_width=image.shape[1]
image_height=image.shape[0]

max_intersection_box_index = calculate_max_intersection(contour, bounding_boxes, image_width, image_height)

# 输出结果
if max_intersection_box_index is not None:
    print(max_intersection_box_index )
else:
    print("No bounding box intersects with the contour.")

运行A.py,调用B.py中的计算函数,输出0,如下:
在这里插入图片描述

  • 4
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

木彳

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值