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

如何用Python统计矩形表格图片中的内部表格数量?

Alright, let's break down how to count those internal subtables in your rectangular table image using Python. Your target is 78, so here's a practical, tested approach that combines image processing with contour detection:

Core Approach

The idea is to leverage contour detection with hierarchy analysis in OpenCV. We'll preprocess the image to highlight table borders, then filter out the main outer table and count all valid inner rectangular contours (which are your subtables).

Step-by-Step Implementation

First, make sure you have the required libraries installed:

pip install opencv-python numpy matplotlib

Then, here's the code with detailed explanations:

import cv2
import numpy as np
import matplotlib.pyplot as plt

def count_subtables(image_path, min_area=1000, max_area_ratio=0.9):
    # 1. Load and preprocess the image
    img = cv2.imread(image_path)
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    
    # Invert binary threshold: turn table borders white, background black
    _, thresh = cv2.threshold(gray, 200, 255, cv2.THRESH_BINARY_INV)
    
    # Dilate to fix broken borders (adjust kernel/iterations if needed)
    kernel = np.ones((3, 3), np.uint8)
    dilated = cv2.dilate(thresh, kernel, iterations=2)
    
    # 2. Find contours with hierarchy (to distinguish parent/child contours)
    contours, hierarchy = cv2.findContours(dilated, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
    
    # 3. Filter valid subtable contours
    subtable_count = 0
    total_image_area = img.shape[0] * img.shape[1]
    
    for cnt, hier_details in zip(contours, hierarchy[0]):
        contour_area = cv2.contourArea(cnt)
        
        # Skip tiny noise and the main outer table
        if contour_area < min_area or contour_area > total_image_area * max_area_ratio:
            continue
        
        # Approximate contour to a polygon (we want rectangles)
        perimeter = cv2.arcLength(cnt, True)
        approx_polygon = cv2.approxPolyDP(cnt, 0.02 * perimeter, True)
        
        # Only count 4-sided shapes (rectangular tables)
        if len(approx_polygon) == 4:
            # Check if this is a child contour (has a parent, so it's inside another table)
            parent_contour_idx = hier_details[3]
            if parent_contour_idx != -1:
                subtable_count += 1
                # Optional: Draw detected subtables for verification
                cv2.drawContours(img, [approx_polygon], 0, (0, 255, 0), 2)
    
    # Visualize results (helpful for tuning parameters)
    plt.figure(figsize=(12, 12))
    plt.imshow(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
    plt.title(f"Detected Subtables: {subtable_count}")
    plt.axis('off')
    plt.show()
    
    return subtable_count

# Run the function on your image
if __name__ == "__main__":
    your_image_path = "path/to/your/table_image.png"
    detected_count = count_subtables(your_image_path)
    print(f"Total number of subtables: {detected_count}")
Key Tuning Tips
  • Adjust min_area: If you're getting false positives from small noise, increase this value. If you're missing small subtables, decrease it.
  • Tweak dilation: If table borders are broken, increase the iterations in cv2.dilate() or use a larger kernel (e.g., (5,5)).
  • Hierarchy check: The hier_details[3] != -1 ensures we only count contours inside another table (the main table or nested subtables). This works for both directly nested and deeply nested subtables.
  • Threshold value: If your image has different lighting, adjust the threshold value (200 in the code) to ensure borders are properly highlighted.
Troubleshooting
  • If you're counting the main table by mistake: Make sure max_area_ratio is set low enough (0.9 works for most cases, as the main table will take up ~95%+ of the image area).
  • If some subtables are missing: Check if their borders are properly connected after dilation, or if their area is below min_area.

内容的提问来源于stack exchange,提问作者RR SS

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.26 09:02:11