Method for extracting building contour and planar functional features based on computer vision openCV
By using the OpenCV library to automate the extraction of building outline and planar functional features, the problem of low efficiency in existing technologies is solved, enabling efficient and rapid automated analysis of building information and supporting automated processing of the entire process from external form to internal function.
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- XI'AN UNIVERSITY OF ARCHITECTURE AND TECHNOLOGY
- Filing Date
- 2026-03-12
- Publication Date
- 2026-06-09
AI Technical Summary
Existing technologies for the automated extraction of building outlines and internal planar functional features in architectural design and urban planning are inefficient and fail to meet the needs for rapid, batch, objective, and integrated analysis.
The OpenCV library is used to automatically extract the building's external contour and planar functional features, including image preprocessing, edge detection, contour filtering, color space conversion, threshold segmentation, and morphological processing. Combined with K-Means clustering analysis, it achieves comprehensive automated analysis from external form to internal function.
It has achieved full automation of the process from extracting the building's outer contour to dividing the internal functional areas, improving efficiency by more than 10 times. It can quickly process single buildings or batches of building samples, improving the efficiency of design research and urban big data analysis.
Smart Images

Figure CN122176341A_ABST
Abstract
Description
Technical Field
[0001] This invention belongs to the fields of computer vision and building informatics, and specifically relates to a method for extracting building outlines and planar functional features based on OpenCV computer vision. Background Technology
[0002] In the preliminary research phase of architectural design, urban planning, Building Information Modeling (BIM) applications, and urban studies, efficiently and accurately acquiring information on the spatial form and internal organization of buildings is a crucial foundational task. This information mainly includes two dimensions: first, the building's external outline defining its geometric boundaries; and second, the functional distribution of its internal spaces and uses. Automated identification, outline extraction, area calculation, and probability calculation of the occurrence of various functional areas (such as living rooms, bedrooms, kitchens, and offices) within a building's floor plan are of significant practical value for batch-scale functional analysis of building floor plans, research on interior space distribution patterns, spatial optimization design, and post-use evaluation.
[0003] Currently, in practical engineering, surveying, and preliminary research, the mainstream methods for obtaining the aforementioned information still heavily rely on traditional manual methods. Specifically, obtaining the building's outline requires professional surveyors to conduct field measurements using equipment such as total stations and real-time dynamic differential (RTK), or to perform manual visual interpretation and screen vectorization on high-resolution imagery. For building plan functional analysis, it usually requires professionals to manually interpret and delineate the boundaries of different functional areas and manually calculate the area based on on-site surveys or design drawings. While this method can ensure the accuracy of individual data through experience, it has inherent drawbacks such as low efficiency, high cost, strong subjectivity, and difficulty in scaling. Especially when dealing with large areas or massive building samples, manual methods are time-consuming and slow to update, failing to meet the urgent needs of modern design, research, and smart city management for rapid data acquisition and dynamic analysis.
[0004] To improve efficiency, the academic community has explored automated extraction technologies. In the field of building outline extraction, a typical approach is based on the analysis of shadows and map-ground relationships in remote sensing images. This method relies on shadows generated under specific lighting conditions to indirectly deduce the outline, but its application is severely limited by imaging angle, weather conditions, and surrounding environmental occlusion, resulting in insufficient universality and stability. More importantly, existing automated research mostly focuses on the extraction of building outlines, while mature and efficient solutions have not yet been developed for the automated identification, refined segmentation, and quantitative feature extraction (such as the area, spatial proportion, frequency, and probability of each function) of functional areas within buildings. Although computer vision and machine learning (such as image segmentation technology) have made significant progress, there is still a technological gap in their systematic application to the interpretation of building floor plans to achieve integrated automatic extraction and quantitative analysis from "outer outline" to "inner zoning."
[0005] In conclusion, neither traditional methods relying on manual labor nor existing automated methods focusing on external contours can meet the practical needs for rapid, batch, objective, and integrated analysis of building interior and exterior spatial information. Therefore, the industry urgently requires a comprehensive technical solution that integrates building exterior contour extraction with internal planar functional feature analysis. This solution aims to provide professionals in related fields with a powerful automated tool to fundamentally improve efficiency and insight throughout the entire process, from preliminary research to in-depth analysis. Summary of the Invention
[0006] The purpose of this invention is to overcome the shortcomings of the existing technology and provide a method for extracting building outlines and planar functional features based on OpenCV (Open Source Computer Vision Library). OpenCV is an open-source, cross-platform computer vision and machine learning software library. Initiated and developed by Intel, it is continuously maintained and updated by Willow Garage and other organizations and the open-source community. OpenCV provides a rich set of computer vision algorithms, covering functional modules such as image acquisition and preprocessing, feature extraction and matching, object detection and recognition, video analysis, camera calibration, 3D reconstruction, and machine learning model invocation. It supports multiple programming languages such as C++, Python, and Java, and can run on various operating system platforms including Windows, Linux, macOS, and Android. It is widely used in computer vision research, intelligent monitoring, autonomous driving, robot vision, medical image analysis, and human-computer interaction.
[0007] This invention is achieved using the following technical solution: A method for extracting building outlines and planar functional features based on OpenCV computer vision includes the following steps: Step 1: Input the original image containing the target building, and perform preprocessing and edge detection on the original image to obtain the initial edge image; Step 2: Binarize and perform contour search on the initial edge image to obtain multiple candidate contours; Step 3: Perform geometric approximation and screening on the obtained candidate contours, and determine the final building outline based on the screening results; Step 4: Input functional images containing the internal functional zones of the building, and perform color space conversion and threshold segmentation to obtain binary masks for multiple functional areas; Step 5: Perform morphological processing and contour finding on the binary mask to determine the contour of each functional area, and label the functional areas according to preset rules; Step 6: Perform cluster analysis on the extracted functional regions and output the analysis results, which include functional outlines, area statistics, and occurrence probabilities.
[0008] Compared with the prior art, the present invention has the following beneficial effects: This invention, by organically integrating the building outline extraction process with the building plan functional feature extraction process, achieves for the first time a comprehensive automated analysis from external form to internal function. Users only need to input the original image, and the system can automatically complete the entire process from edge detection and outline filtering to functional area segmentation and area statistics. This invention can reduce the time for extracting the outline of a single building and analyzing the plan function of a single unit from days or hours to seconds or minutes. Batch processing of 100 building samples can complete the entire process of outputting internal and external outlines, area, and functional probabilities in just a few minutes, improving efficiency by more than 10 times. This greatly improves the efficiency of preliminary architectural design research, urban big data analysis, and BIM model building, effectively overcoming the shortcomings of low efficiency and long cycles in existing technologies. Attached Figure Description
[0009] Figure 1 Flowchart for extracting the building's outer contour; Figure 2 Extract the building's floor plan functions and area flowchart. Detailed Implementation
[0010] This invention achieves automated analysis of building spatial features through two major technical processes: "building outline extraction process" and "adaptive extraction and analysis process of building plan functional outline." The following is a combination of these processes. Figure 1 , Figure 2 Detailed explanation of the implementation process and working principle: Part 1: Building Outer Contour Extraction Process (Example 1, corresponding to...) Figure 1 ) This embodiment uses aerial imagery of urban areas as the input source to automatically extract the outer contours of individual buildings / areas. The process is as follows: Step 1: Initial Image Input (corresponding to) Figure 1 "Step 1 Input: Initial Image") Implementation process: Select aerial imagery covering the target area (resolution 0.5m / pixel, containing complete building areas), and read the imagery as raw input data using the OpenCV library.
[0011] Working principle: Acquire visual data sources containing building targets to provide a basic basis for subsequent contour extraction.
[0012] Step 2: Image preprocessing and preliminary contour extraction (corresponding to...) Figure 1 "Step 2: Edge Detection → Binarization → Contour Finding → Contour Approximation → Contour Analysis → Non-maximum Suppression → Contour Hierarchy") Implementation process: Grayscale conversion: A weighted average method (R=0.299, G=0.587, B=0.114) is used to convert color images into grayscale images, simplifying data dimensions; Filtering and denoising: Apply a 3×3 Gaussian filter (σ=1.0) to smooth the image and eliminate the interference of detail texture and random noise on the edges; Edge detection: The Canny operator (high threshold 120, low threshold 60, high-low ratio 1:2) is used to extract edge pixels with abrupt grayscale changes in the image; Binarization: Convert the edge detection results into a binary image (set the edge pixels to 255 and the background to 0) to highlight building boundary information; Contour finding: Call the findContours function (search mode RETR_TREE, approximation method CHAIN_APPROX_SIMPLE) to obtain all closed contours in the binary image; Contour filtering: The contours are geometrically approximated using the Douglas-Peucker algorithm (with an accuracy of 1% of the perimeter), and interfering contours such as trees and roads are filtered out according to "area > total image area 0.1% and shape complexity ≥ 0.6". Non-maximum suppression: 3×3 neighborhood suppression is applied to the filtered contours to retain only the edge points with the largest gradient and eliminate redundant lines; Outline hierarchy determination: Based on the hierarchical relationship of RETR_TREE, the outermost closed outline is selected as the candidate for the building's outer outline.
[0013] Working principle: Data is simplified and noise is reduced through grayscale conversion and filtering; Canny operator + binarization accurately locates building boundaries; contour filtering and suppression distinguishes buildings from interference objects through geometric features; hierarchical relationship locks the outer boundary of the building.
[0014] Step 3: Output and batch extraction of outer contour (corresponding to...) Figure 1 "Step 3 Output: Outline" "Step 4 Outline Extraction" Implementation process: Call the drawContours function to draw the final building outline as a 2-pixel red solid line on the original image and output it as SVG vector data; Repeat the filtering logic in Step 2 to extract the outer contours of all buildings within the area in batches.
[0015] Working principle: Visualize the results and output standardized data to achieve full-coverage extraction of building outlines at the regional level.
[0016] Step 4: Abstraction and Refinement of Architectural Prototypes (corresponding to...) Figure 1 Step 5: Abstract Refinement of the Architectural Prototype Implementation process: Classify the outer contours of a batch of buildings according to their shape characteristics (such as "Siheyuan", "Sanheyuan", "L-shaped", "I-shaped") and extract typical building prototypes.
[0017] Working principle: Simplify the description of building form by classifying shapes to support subsequent spatial pattern analysis.
[0018] Part Two: Adaptive Extraction and Analysis Process of Architectural Floor Plan Functional Profile (Example 2, corresponding to...) Figure 2 ) This embodiment uses residential floor plans from the Qinghai-Tibet Plateau (Tibet, Qinghai, and Xinjiang) as samples (sample size ≥ 100 households) to achieve automatic identification and feature analysis of internal functions. The process is as follows: Step 1: Sample Input and Preprocessing Preparation (corresponding to...) Figure 2 Step 1) Implementation process: Sample acquisition: Architectural floor plans of the target area were obtained through field surveys and literature collection, and functional areas were marked with different colors (e.g., light yellow = living room, light blue = bedroom). Image Input: Batch import annotated planar maps (RGB format, 300 DPI resolution) into the processing system.
[0019] Working principle: Acquire functional plane data with color annotations to provide a foundation for subsequent color segmentation.
[0020] Step 2: Image preprocessing and color adaptive segmentation (corresponding to...) Figure 2 “Step 2” Implementation process: Denoising preprocessing: Apply 3×3 median filtering to each plan view to eliminate salt-and-pepper noise generated during scanning / digitization; Color space conversion: Convert the RGB image to HSV space to reduce the interference of lighting on color recognition; K-Means Color Adaptive Segmentation (Core Steps): Extract the HSV values of all pixels in the image as samples; Set the number of clusters to 16 (the number of common functional areas covered; in the color adaptive segmentation stage, the K value is usually set to 16 to cover all color types; in the subsequent functional pattern clustering stage, the K value is determined by the elbow rule to summarize the layout types, usually between 3 and 8), and automatically identify the main color categories in the drawing through K-Means clustering. Generate a binary mask for each cluster color (functional area pixels = 255, background = 0); Morphological operations: Perform "erosion first, then dilation" opening and closing operations on the mask (5×5 rectangular structuring element) to eliminate noise points <10 pixels and smooth region boundaries; Contour extraction and function annotation: Use findContours to extract the closed contours of the mask (only retain contours with an area greater than 50 pixels). On the first run, professionals will establish a mapping of "cluster number - actual function" (e.g., "number 0 → bedroom"). Subsequent batch drawings will automatically call this mapping to complete the function labeling.
[0021] Working principle: Median filtering is used to specifically eliminate salt-and-pepper noise; HSV space enhances color discrimination; K-Means clustering achieves color adaptive segmentation, replacing manual threshold adjustment; morphological operations optimize region boundaries; and mapping relationships complete the semantic annotation of functions.
[0022] Step 3: Feature Quantification and Distribution Pattern Analysis (corresponding to...) Figure 2 “Step 3” Implementation process: Feature quantization: Calculate the pixel area of the functional area and convert it to the actual area using the drawing scale (accuracy 0.01m). 2 ), count the functional area and number of individual images; Functional pattern clustering: Construct a feature vector of "functional area ratio" and identify different floor plan layout patterns through K-Means clustering; Probability, Statistics and Visualization: Overall probability: The number of times a statistical function appears in 100 samples (e.g., the kitchen appears 85 times, with a probability of 0.85). Conditional probability: The frequency of occurrence of a statistical function within a subset of a certain layout pattern (such as "pattern A"); Output "spatial function distribution bar chart" and "function frequency density heat map".
[0023] Working principle: Area conversion enables functional quantification; clustering discovers common layout patterns; probability statistics and visualization intuitively present the functional distribution rules.
[0024] Process summary and collaboration Standalone application: When the data source is only aerial / satellite imagery (i.e., raw image), run Example 1 to obtain the outer contour; when the data source is only a color-annotated plan view (i.e., functional image), run Example 2 to obtain the internal functional features.
[0025] Integrated application: When image data from the two different sources mentioned above are available, two processes can be executed sequentially to spatially register and overlay the extracted building outline and internal functional features, ultimately forming a complete digital archive of the building space that includes "external form + internal function".
[0026] This implementation method is "K-Means color adaptive segmentation", which eliminates the need to manually adjust the color threshold of each drawing, greatly improving the automation and versatility of the method.
[0027] 1. In the color adaptive segmentation stage (Step 2), in order to fully cover all possible color types in the planar image (including main functional colors, auxiliary colors, text label colors, etc.), the K value is set to 16. This value has been verified through extensive sample testing and can ensure both segmentation accuracy and computational efficiency.
[0028] 2. In the functional pattern clustering stage (Step 3), in order to summarize a limited number of typical planar layout patterns, the K value is not fixed, but is automatically determined by the time part rule. Its value range is usually between 3 and 8 (i.e., a subset of 3-16 as described in claim 10) to adapt to the natural distribution characteristics of different sample sets.
[0029] The K-value setting logic in the two stages mentioned above is different, serving two different technical purposes: "fine segmentation" and "pattern induction". The two are not contradictory and together constitute the complete technical solution of this method.
Claims
1. A method for extracting building outlines and planar functional features based on computer vision OpenCV, characterized in that, Includes the following steps: Step 1: Input the original image containing the target building, and perform preprocessing and edge detection on the original image to obtain the initial edge image; Step 2: Binarize and perform contour search on the initial edge image to obtain multiple candidate contours; Step 3: Perform geometric approximation and screening on the obtained candidate contours, and determine the final building outline based on the screening results; Step 4: Input functional images containing the internal functional zones of the building, and perform color space conversion and threshold segmentation to obtain binary masks for multiple functional areas; Step 5: Perform morphological processing and contour finding on the binary mask to determine the contour of each functional area, and label the functional areas according to preset rules; Step 6: Perform cluster analysis on the extracted functional regions and output the analysis results, which include functional outlines, area statistics, and occurrence probabilities.
2. The method for extracting building outlines and planar functional features based on OpenCV computer vision according to claim 1, characterized in that, In step one, the source of the original image includes at least one of satellite imagery, aerial photographs, and architectural design plans; the resolution of the original image is not less than 256×256 pixels.
3. The method for extracting building outlines and planar functional features based on OpenCV computer vision according to claim 1, characterized in that, In step one, the preprocessing of the original image includes grayscale conversion and filtering for noise reduction. Grayscale conversion uses a weighted average method for the R, G, and B channels, with weights of R=0.299, G=0.587, and B=0.114, respectively. Filtering for noise reduction uses Gaussian filtering with a kernel size of 3×3 or 5×5, and the σ value is set between 0.5 and 2.0 according to the noise intensity.
4. The method for extracting building outlines and planar functional features based on OpenCV computer vision according to claim 1, characterized in that, In step one, edge detection uses the Canny operator, with the ratio of its high threshold to low threshold set between 1:2 and 1:3, and only edge points whose gradient magnitudes conform to this threshold range are retained.
5. The method for extracting building outlines and planar functional features based on OpenCV computer vision according to claim 1, characterized in that, In step two, the binarization process uses a fixed threshold method, with the threshold value ranging from 127 to 200. Pixel values above the threshold are set to 255, and pixel values below the threshold are set to 0.
6. The method for extracting building outlines and planar functional features based on OpenCV computer vision according to claim 1, characterized in that, In step three, the obtained candidate contours are geometrically approximated using the Douglas-Peucker algorithm, with an approximation accuracy of 0.5% to 2% of the candidate contour perimeter. The criteria for selecting candidate contours include: the contour area is greater than 0.1% of the total image area, the ratio of the contour perimeter to the area is between 2 and 10, and the shape complexity, i.e., the ratio of the contour area to the area of its smallest bounding rectangle, is not less than 0.
6.
7. The method for extracting building outlines and planar functional features based on OpenCV computer vision according to claim 1, characterized in that, In step four, the functional image is in RGB format, and the tonal difference and saturation difference of its different functional areas are not less than 30° and 20%, respectively; color space conversion is to convert the image from RGB color space to HSV color space.
8. The method for extracting building outlines and planar functional features based on OpenCV computer vision according to claim 7, characterized in that, In step four, during HSV threshold segmentation, the HSV threshold range for each functional region is determined based on sample statistics. The width of the hue value range is no greater than 20°, and the widths of the saturation value range and the brightness value range are both no greater than 30%. A binary mask is then generated accordingly.
9. The method for extracting building outlines and planar functional features based on OpenCV computer vision according to claim 1, characterized in that, In step five, morphological processing includes performing opening and closing operations on the binary mask using rectangular structuring elements; functional labeling is achieved by reading a preset color-function mapping rule configuration file.
10. The method for extracting building outlines and planar functional features based on OpenCV computer vision according to claim 1, characterized in that, In step six, the cluster analysis uses the K-Means algorithm, and the K value is determined by the elbow rule, with a range of 3 to 16. Clustering features include the area proportion of functional zones and their relative positions; in the analysis results, the area statistics accuracy is at least two decimal places, and the occurrence probability statistics accuracy is at least three decimal places.