836. Rectangle Overlap

本文介绍了一种高效的矩形重叠判断算法,通过检查矩形边界来确定两个矩形是否存在交集。该方法避免了复杂的重叠情况判断,简化为四个基本条件,实现了快速准确的重叠检测。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目

在这里插入图片描述

我的代码(效率较高)

class Solution(object):
    def isRectangleOverlap(self, rec1, rec2):
        """
        :type rec1: List[int]
        :type rec2: List[int]
        :rtype: bool
        """
        if rec1[0]>=rec2[2]:
            return False
        if rec1[1]>=rec2[3]:
            return False
        
        if rec2[0]>=rec1[2]:
            return False
        if rec2[1]>=rec1[3]:
            return False
        else:
            return True

优秀代码

与上面代码类似。

class Solution(object):
    def isRectangleOverlap(self, rec1, rec2):
        """
        :type rec1: List[int]
        :type rec2: List[int]
        :rtype: bool
        """
        # method 1, 16ms, 100%; 10.7MB, %
        # 判斷是否重疊太麻煩, 太多種類;
        # 不如直接判斷是否沒重疊
        # 只要四個點都在外面就可以了
        # 總共只有四種情況...
        for idx, num in enumerate(rec2):
            if idx == 0:
                if rec1[2] <= num:
                    return False
            elif idx == 1:
                if rec1[3] <= num:
                    return False
            elif idx == 2:
                if rec1[0] >= num:
                    return False
            else:
                if rec1[1] >= num:
                    return False
        return True
这串代码输出后中文标签显示为问号import cv2 from sahi.predict import get_sliced_prediction from sahi import AutoDetectionModel from ultralytics import YOLO # 初始化模型 detection_model = AutoDetectionModel.from_pretrained( model_type='yolov8', model_path='yolov8n_chinese.pt', confidence_threshold=0.4, #device='cuda:0' # 或 'cpu' ) # 视频处理参数 VIDEO_PATH = "视频检测源/sample_video.mp4" OUTPUT_PATH = ("output/output_video.mp4") SLICE_SIZE = 256 # 切片尺寸 OVERLAP_RATIO = 0.2 # 切片重叠比例 # 打开视频文件 cap = cv2.VideoCapture(VIDEO_PATH) frame_width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)) frame_height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)) fps = cap.get(cv2.CAP_PROP_FPS) # 创建视频写入器 fourcc = cv2.VideoWriter_fourcc(*'mp4v') out = cv2.VideoWriter(OUTPUT_PATH, fourcc, fps, (frame_width, frame_height)) while cap.isOpened(): ret, frame = cap.read() if not ret: break # 使用SAHI进行切片推理 result = get_sliced_prediction( frame, detection_model, slice_height=SLICE_SIZE, slice_width=SLICE_SIZE, overlap_height_ratio=OVERLAP_RATIO, overlap_width_ratio=OVERLAP_RATIO ) # 绘制检测结果 for pred in result.object_prediction_list: # 解析预测结果 bbox = pred.bbox.to_voc_bbox() label = pred.category.name confidence = pred.score.value # 绘制矩形框和标签 cv2.rectangle(frame, (int(bbox[0]), int(bbox[1])), (int(bbox[2]), int(bbox[3])), (0, 255, 0), 2) cv2.putText(frame, f"{label} {confidence:.2f}", (int(bbox[0]), int(bbox[1] - 5)), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 0, 255), 2) # 写入输出视频 out.write(frame) # 释放资源 cap.release() out.release() cv2.destroyAllWindows()
最新发布
06-02
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值