A multi-box annotation file generation algorithm based on shp files
Through a multi-box labeling file generation algorithm based on shp file, the problem that the existing technology cannot convert the shp file into the labeling information supported by deep learning is solved, and a multi-box labeling file supporting deep learning is generated and stored in a general json format for further conversion.
Patent Information
- Application Number
- CN202211504610.6
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2022-11-29
- Publication Date
- 2025-06-24
- Estimated Expiration
- 2042-11-29
AI Technical Summary
The prior art cannot convert geometric data information in the shp file into labeled information that supports deep learning, resulting in the inability to effectively process and utilize these data.
A multi-box labeling file generation algorithm based on shp file is proposed. By reading shp file information, obtaining the shape and attribute information of geometric objects, traversing the objects for boundary and intersection judgment, and generating a labeling file in json format.
It realizes the conversion of shp files into multi-box annotation files that support deep learning, which can generate annotation files of different dimensions and store annotation information in a common json format, which is convenient for conversion to other segmented image annotation formats.
Smart Images

Figure CN115729898B_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the technical field of image processing, and in particular to an algorithm for generating a multi-box annotation file based on shp files. Background Art
[0002] Shp is a file in the "Spatial Data Open" format, full name "ESRI Shapefile", which is a vector graphics format. This format file is mainly used to describe geometric objects (points, polylines, and polygons), and can save the positions and related attributes of geometric figures. Currently, most of the processing methods for shp files are to open and view them in specific software. This method only performs the most superficial data reading operation on the shp file and cannot convert geometric data information into annotation information that supports deep learning. The present invention proposes a method for constructing an annotation file containing multiple geometric boxes using shp files. Summary of the Invention
[0003] In view of the deficiencies in the prior art, the present invention provides an algorithm for generating a multi-box annotation file based on shp files, which solves the problem in the prior art that most of the current processing methods for shp files are to open and view them in specific software. This method only performs the most superficial data reading operation on the shp file and cannot convert geometric data information into annotation information that supports deep learning.
[0004] The above technical object of the present invention is achieved through the following technical solutions:
[0005] An algorithm for generating a multi-box annotation file based on shp files, comprising the following steps:
[0006] Step 1, read shp file information, load the shp annotation file, obtain the shape information shape and attribute information record of each geometric object, and obtain a list of geometric objects;
[0007] Step 2, traverse the geometric objects, and denote the current object as main;
[0008] Step 3, obtain the bounding box bbox, geometric box polygon, and category class of the main object, add the sequence number of the main in the list to the list sequence, and create lists near_shape and distance;
[0009] Step 4, traverse the geometric objects, denote the current object as inner, and perform the following judgments and processing:
[0010] a. Whether inner is main. If so, skip it and process the next geometric object;
[0011] Whether the polygon of b.inner intersects with the bbox. If so, add the serial number of inner to near_shape. Otherwise, calculate the distance between the polygon of inner and main, and add a tuple consisting of the serial number of inner and the distance to main to distance;
[0012] Step 5, according to the pre-set number number of geometric frames included in each picture, if len(near_shape) ≥ (number - 1), randomly select number serial numbers from near_shape and add them to sequence; if len(near_shape) < (number - 1), then take out all the serial numbers in near_shape and add them to sequence, and then take out the serial numbers of number - len(near_shape) - 1 tuples with the smallest distances from distance and add them to sequence; where len(near_shape) represents the number of list elements of near_shape;
[0013] Step 6, add the polygons and classes of the geometric objects included in the serial numbers in sequence to lines and classes in turn, fuse the boundaries of these geometric objects, find the largest boundary, and fill around the boundary to form the final fused boundary boundary;
[0014] Step 7, traverse the geometric objects, denote the current object as sub, and make the following judgments and processing:
[0015] a. Whether sub is in sequence. If so, skip it and process the next geometric object;
[0016] b. Whether the polygon shape of sub intersects with boundary. If not, skip it; if so, calculate the intersection of the polygon of sub and boundary, add the intersecting part intersection to lines, and add the category class of sub to classes;
[0017] Step 8, write boundary, lines and classes into json and save them as files;
[0018] Step 9, repeat steps 2 - 8 until all geometric objects are processed;
[0019] Step 10, obtain the annotation files in json format for each geometric object and its neighboring geometric objects;
[0020] Step 11: Generate annotation information in JSON format with different sizes and scales according to the specified number of geometric frames included.
[0021] The present invention is further configured as follows: In step 1, shape includes the boundary bbox of the circumscribed rectangle of each geometric object and the sequence of point coordinates polygon of the geometric frame; record includes the identification id and class information class of each geometric object.
[0022] The present invention is further configured as follows: In step 6, fill 10% of the boundary width on the left and right and 10% of the boundary height on the top and bottom as the final fusion boundary boundary.
[0023] The present invention is further configured as follows: In step 10, each annotation file contains at least number geometric objects.
[0024] The present invention is further configured as follows: In step 11, the annotation information is used to be converted into the segmentation image annotation in the ADE or Cityscapes format.
[0025] The beneficial effects of the present invention are as follows: The process of finding adjacent geometric objects in steps 4 and 5 of the present invention is one of the innovative points of the present invention. The present invention splits a shp file into multiple annotation files containing multiple geometric frames, and can specify the number of geometric frames that each annotation file contains at least, so as to generate annotation files of different dimensions. At the same time, the annotation information is stored in a common JSON format, which can be conveniently converted into the required segmentation annotation format. Description of the Drawings
[0026] Figure 1 is the overall algorithm flow chart;
[0027] Figure 2 is the sub-flow chart for finding adjacent geometric objects;
[0028] Figure 3 is the flow chart for finding edge-intersecting objects. Detailed Embodiments
[0029] The technical solutions in the present invention will be further described below with reference to the drawings and embodiments.
[0030] A multi-frame annotation file generation algorithm based on a shp file includes the following steps:
[0031] Step 1: Read the shp file information. Load the shp annotation file, obtain the shape information (shape) and attribute information (record) of each geometric object, and get a list of geometric objects. Among them, shape contains the boundary box (bbox) of the circumscribed rectangle of each geometric object and the sequence of point coordinates (polygon) of the geometric box; record contains the identifier (id) and category information (class) of each geometric object.
[0032] Step 2: Traverse the geometric objects, and denote the current object as main.
[0033] Step 3: Obtain the boundary box (bbox), geometric box (polygon), and category (class) of the main object, add the sequence number of main in the list to the list sequence, and create the lists near_shape and distance.
[0034] Step 4: Traverse the geometric objects, and denote the current object as inner. Make the following judgments and processes:
[0035] a. Whether inner is main. If so, skip it and process the next geometric object;
[0036] b. Whether the polygon of inner has an intersection with the bbox. If so, add the sequence number of inner to near_shape. Otherwise, calculate the distance between inner and the polygon of main, and add a tuple composed of the sequence number of inner and the distance to main to distance.
[0037] Step 5: According to the pre-set number (number) of geometric boxes contained in each picture, if len(near_shape) ≥ (number - 1) (note that len(near_shape) represents the number of elements in the near_shape list), randomly select number sequence numbers from near_shape and add them to sequence; if len(near_shape) < (number - 1), then take out all the sequence numbers in near_shape and add them to sequence, and then take out the sequence numbers of number - len(near_shape) - 1 tuples with the smallest distances from distance and add them to sequence.
[0038] Step 6: Add the polygons and classes of the geometric objects corresponding to the sequence numbers in sequence to lines and classes in turn, fuse the boundaries of these geometric objects, find the largest boundary, and fill 10% of the boundary width on the left and right and 10% of the boundary height on the top and bottom of the boundary as the final fused boundary boundary.
[0039] Step 7, traverse the geometric objects, denote the current object as sub, and make the following judgments and processing:
[0040] a. Whether sub is in sequence. If so, skip it and process the next geometric object;
[0041] b. Whether the polygon shape of sub has an intersection with the boundary. If not, skip it; if so, calculate the intersection of the polygon of sub and the boundary, add the intersecting part intersection to lines, and add the class of sub to classes.
[0042] Step 8, write boundary, lines, and classes into json and save them as a file.
[0043] Step 9, repeat steps 2 - 8 until all geometric objects are processed.
[0044] Step 10, obtain the annotation files in json format for each geometric object and its neighboring geometric objects. Each annotation file contains at least number geometric objects.
[0045] Step 11, according to different specified numbers of geometric bounding boxes, this method can generate annotation information in json format of different sizes and scales. Based on this annotation information, it can be converted into segmentation image annotations in other formats such as ADE or Cityscapes as needed.
[0046] As Figure 1 shown, Figure 1 is the overall flowchart of the algorithm, which describes the whole process from shp to obtaining the json annotation file. There are two sub - processes shown in Figure 2 、 Figure 3 . Figure 2 is the flowchart for finding neighboring geometric objects, which is the core part of this algorithm. The basic idea is to first search from the adjacent objects of the current object. If the number of adjacent objects is insufficient, then supplement from the objects with the closest distance. Figure 3 is the flowchart for finding the objects that have intersections with the fusion boundary after the neighboring object fusion is completed, and supplement the shape and class of the intersecting part to the annotation information.
[0047] The present invention proposes a multi - bounding - box annotation file generation algorithm based on shp files, which splits a shp file into multiple annotation files containing multiple geometric bounding boxes. The number of geometric bounding boxes that each annotation file contains at least can be specified to generate annotation files of different dimensions. At the same time, the annotation information is stored in a common json format, which can be conveniently converted into the required segmentation annotation format.
[0048] Finally, it should be noted that the above embodiments are only used to illustrate the technical solutions of the present invention rather than to limit them. Although the present invention has been described in detail with reference to the preferred embodiments, those of ordinary skill in the art should understand that the technical solutions of the present invention can be modified or equivalently replaced without departing from the purpose and scope of the technical solutions of the present invention, and they should all be covered by the scope of the claims of the present invention.
Claims
1. A multi-box annotation file generation algorithm based on shp files, characterized in that: It includes the following steps: Step 1: Read the shp file information, load the shp annotation file, obtain the shape information "shape" and attribute information "record" of each geometric object, and get a list of geometric objects; Step 2: Traverse the geometric objects, and denote the current object as "main"; Step 3: Obtain the boundary "bbox", geometric frame "polygon" and category "class" of the "main" object, add the serial number of "main" in the list to the list "sequence", and create lists "near_shape" and "distance"; Step 4: Traverse the geometric objects, and denote the current object as "inner", and make the following judgments and processing: a. Whether "inner" is "main", if so, skip it and process the next geometric object; b. Whether the "polygon" of "inner" has an intersection with the "bbox", if so, add the serial number of "inner" to "near_shape", otherwise, calculate the distance between "inner" and the "polygon" of "main", and form a tuple of the serial number of "inner" and the distance to "main" and add it to "distance"; Step 5: According to the preset number "number" of geometric frames included in each picture, if len(near_shape) ≥ (number - 1), randomly select "number" serial numbers from "near_shape" and add them to "sequence"; if len(near_shape) < (number - 1), then take out all the serial numbers in "near_shape" and add them to "sequence", and then take out the serial numbers of number - len(near_shape) - 1 tuples with the smallest distances from "distance" and add them to "sequence"; where, len(near_shape) represents the number of list elements in "near_shape"; Step 6: Add the "polygon" and "class" of the geometric objects corresponding to the serial numbers in "sequence" to "lines" and "classes" in sequence, fuse the boundaries of these geometric objects, find the largest boundary, and perform filling around the boundary to form the final fused boundary "boundary"; Step 7: Traverse the geometric objects, and denote the current object as "sub", and make the following judgments and processing: a. Whether "sub" is in "sequence", if so, skip it and process the next geometric object; b. Whether the shape of the "polygon" of "sub" has an intersection with "boundary", if not, skip it; if so, calculate the intersection of the "polygon" of "sub" and "boundary", add the intersecting part "intersection" to "lines", and add the category "class" of "sub" to "classes"; Step 8: Write "boundary", "lines" and "classes" into json and save them as a file; Step 9: Repeat steps 2 - 8 until all geometric objects are processed; Step 10, obtain an annotation file in JSON format for each geometric object and its neighboring geometric objects; Step 11, generate annotation information in JSON format with different sizes and scales according to different specified numbers of geometric boxes.
2. The multi-box annotation file generation algorithm based on shp files according to claim 1, wherein: In Step 1, shape includes the boundary bbox of the circumscribed rectangle of each geometric object and the sequence of point coordinates polygon of the geometric box; record includes the identifier id and class information class of each geometric object.
3. The multi-box annotation file generation algorithm based on shp files as claimed in claim 1, wherein: In Step 6, fill 10% of the boundary width on the left and right of the boundary, and fill 10% of the boundary height above and below as the final fusion boundary boundary.
4. The multi-box annotation file generation algorithm based on shp files according to claim 1, wherein: In Step 10, each annotation file contains at least number geometric objects.
5. The multi-box annotation file generation algorithm based on shp files according to claim 1, wherein: In Step 11, the annotation information is used to convert into segmentation image annotations in ADE or Cityscapes format.
Citation Information
Patent Citations
Power grid graph drawing method based on mobile GIS platform
CN105975619A
Vector data SHP file parallel writing method
CN112463905A