You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

建筑平面图门检测技术求助:弧检测效果不佳求优化方案

Door Detection in Floor Plans: Better Approaches & Code Improvements

Hey there, let's work through your door detection problem. It's common for arc-based methods to fall short here—floor plan doors come in so many variations (arcs, rectangles with gaps, simple swing markers) that a one-size-fits-all arc check just isn't reliable. Let's break down better strategies and fix up your current code too.

Top Approaches to Try

1. Rule-Based Shape Matching with Gap Detection

Most floor plan doors sit in a gap between two wall segments. Instead of hunting for arcs directly:

  • First, leverage your existing wall extraction results.
  • Identify wall gaps: Look for breaks in wall lines where the distance between two adjacent wall endpoints falls within typical door widths (e.g., 60-120 pixels, adjust based on your plan's scale).
  • For each gap, check the surrounding area for door-specific shapes:
    • Arc-shaped doors: Use cv2.fitEllipse() on nearby contours to see if the aspect ratio matches a door arc (usually a tall, narrow ellipse segment).
    • Swing-style doors: Look for short line segments (the swing indicator) connected to the gap, or a small rectangle adjacent to the gap.

2. Template Matching (For Uniform Floor Plan Styles)

If your floor plans are all from the same CAD tool or have consistent door designs:

  • Create a set of templates: Capture different door variations (open arc, closed rectangle, different angles) as small image snippets.
  • Use OpenCV's cv2.matchTemplate() with methods like TM_CCOEFF_NORMED to scan the floor plan for matches. Set a threshold (e.g., 0.7) to filter out weak matches.
  • This is fast and easy to implement if your dataset is consistent—no training required.

3. Deep Learning-Based Detection (For Diverse Plans)

If you have access to labeled floor plan data (even a few hundred examples), a lightweight object detection model will give you the best long-term results:

  • Use models like YOLOv8 Nano or Faster R-CNN (YOLO is easier to set up for quick iterations).
  • Label your images with bounding boxes around doors (tools like LabelImg make this straightforward).
  • Train the model on your dataset—this will handle all door variations automatically, even if they look very different from each other.

Fixes to Your Current Code

Your existing code has a few small issues that might be hurting results:

  • You're applying erosion/dilation to the color image (r_img) instead of the grayscale version—this wastes computation and can introduce color-based noise.
  • The contour filter (len(approx) >10 and area>100) is too broad—it will catch any large curved shape, not just doors.
  • You're not tying detections to wall positions, so you'll get false positives on non-door curves.

Here's a quick revised version of your code that focuses on grayscale processing and adds basic shape ratio checks:

import cv2
import numpy as np

def door_detection(r_img):
    gray = cv2.cvtColor(r_img, cv2.COLOR_BGR2GRAY)
    
    # Use grayscale for morphological operations
    kernel = np.ones((5,5), np.uint8)
    erosion = cv2.erode(gray, kernel, iterations=2)
    dilated = cv2.dilate(erosion, kernel, iterations=2)
    
    blurred = cv2.medianBlur(dilated, 5)
    canny = cv2.Canny(blurred, 80, 180)
    
    contours, hierarchy = cv2.findContours(canny, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
    contour_list = []
    
    for contour in contours:
        area = cv2.contourArea(contour)
        # Skip tiny or huge contours
        if area < 100 or area > 2000:
            continue
            
        # Check if contour is a curved shape (potential arc door)
        approx = cv2.approxPolyDP(contour, 0.02 * cv2.arcLength(contour, True), True)
        if len(approx) > 8:
            # Get bounding rectangle to check aspect ratio (doors are tall/narrow or wide/short)
            x, y, w, h = cv2.boundingRect(contour)
            aspect_ratio = float(w) / h
            # Typical door aspect ratios (adjust based on your plans)
            if 0.2 < aspect_ratio < 1.5:
                contour_list.append(contour)
    
    cv2.drawContours(r_img, contour_list, -1, (255,0,0), 2)
    cv2.imshow('Objects Detected', r_img)
    cv2.imwrite('door_detections.jpg', r_img)
    
    if cv2.waitKey(0) & 0xff == 27:
        cv2.destroyAllWindows()

Start with the rule-based gap detection approach first—it's the most straightforward to implement without needing extra data. If that still isn't enough, move to template matching or deep learning depending on your dataset consistency.

内容的提问来源于stack exchange,提问作者sandeep konar

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.07 12:23:12