A method for extracting a connected component and a terminal
By allocating threads to each row and column of the binarized image separately and using the GPU for parallel computation, the problem of low efficiency in extracting connected components in existing technologies is solved, and faster computation speed is achieved.
Patent Information
- Application Number
- CN202310993959.9
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2023-08-08
- Publication Date
- 2025-12-16
- Estimated Expiration
- 2043-08-08
AI Technical Summary
Existing technologies have low computational efficiency when extracting connected components, especially in scenarios requiring real-time processing, which makes it difficult to meet speed requirements. This is mainly because iterative methods rely on single-threaded processing.
A multi-threaded processing method is adopted, which allocates threads to each row and column of the binarized image, uses threads to perform labeling and equivalent mapping transformation, and uses GPU for parallel computing to extract connected components.
By using multi-threaded parallel computing, the extraction speed of connected components is significantly improved, thus enhancing computational efficiency.
Smart Images

Figure CN117197176B_ABST
Abstract
Description
TECHNICAL FIELD
[0001] The present application relates to the technical field of image processing, in particular to a connected component extraction method and a terminal. BACKGROUND
[0002] In image processing, especially in automatic image analysis, the method of extracting connected components in morphology is usually used. The common method is to first perform threshold segmentation on the image to obtain a binary image, then calculate the connected regions of the elements in the binary image, and give different labels respectively. There may be multiple connected regions in an image, and the regions are not connected. Labeling each region can be called extracting connected components. Subsequently, the corresponding region can be known according to the label of the region, so that each region can be analyzed and processed individually. Extracting connected components from a binary image is the core of many automatic image analysis applications. Many high-order algorithms are extended based on connected components.
[0003] The main method for extracting connected components at present is to use an iterative method, and the number of iterations determines the speed. Since the subsequent iterations depend on the previous results, a single thread is usually used for processing, which is slow and difficult to apply to real-time processing scenarios. SUMMARY
[0004] The technical problem to be solved by the present application is to provide a connected component extraction method and a terminal, which can effectively improve the calculation efficiency.
[0005] In order to solve the above technical problems, the technical scheme adopted by the present application is:
[0006] A connected component extraction method, comprising the steps of:
[0007] obtaining a binary image;
[0008] allocating a thread to each element row of the binary image respectively, and using the thread to label the connected elements in the element row to obtain a labeled binary image;
[0009] allocating a thread to each element column of the labeled binary image respectively, and using the thread to perform equivalent mapping transformation on each connected element in the element column according to the label to obtain a labeled connected component image.
[0010] In order to solve the above technical problems, another technical scheme adopted by the present application is:
[0011] A connected component extraction terminal, comprising a memory, a processor and a computer program stored in the memory and executable on the processor, wherein the processor implements the following steps when executing the computer program:
[0012] obtaining a binary image;
[0013] allocating a thread to each element row of the binary image respectively, and marking the connected elements in the element row by using the thread to obtain a marked binary image;
[0014] allocating a thread to each element column of the marked binary image respectively, and performing an equivalent mapping transformation on each connected element in the element column according to the mark by using the thread to obtain a marked connected component image.
[0015] The present application has the beneficial effect that a thread is allocated to each element row of the obtained binary image respectively, and the element row is marked by using the thread to obtain a marked binary image, a thread is allocated to each element column of the marked binary image respectively, and an equivalent mapping transformation is performed on each connected element in the element column according to the mark by using the thread to obtain a marked connected component image, so that the extraction of the connected component is performed simultaneously by using the GPU multi-thread, and compared with the existing single-thread calculation, the calculation speed is faster, thereby effectively improving the calculation efficiency. BRIEF DESCRIPTION OF DRAWINGS
[0016] Figure 1 a step flow chart of a connected component extraction method according to an embodiment of the present application;
[0017] Figure 2 a structural schematic diagram of a connected component extraction terminal according to an embodiment of the present application;
[0018] Figure 3 a binary image in a connected component extraction method according to an embodiment of the present application;
[0019] Figure 4 a thread allocation schematic diagram in a connected component extraction method according to an embodiment of the present application;
[0020] Figure 5 a remapped binary image in a connected component extraction method according to an embodiment of the present application;
[0021] Figure 6 a remapped binary image with thread allocation in a connected component extraction method according to an embodiment of the present application;
[0022] Figure 7 a marked connected component image in a connected component extraction method according to an embodiment of the present application. DETAILED DESCRIPTION
[0023] The technical content, purposes and effects of the present application will be described in detail below in combination with the embodiments and the accompanying drawings.
[0024] Please refer to Figure 1 A method for extracting connected components, comprising the steps of:
[0025] Obtaining a binary image;
[0026] Assigning a thread to each element row of the binary image respectively, and marking the connected elements in the element row by using the thread to obtain a marked binary image;
[0027] Assigning a thread to each element column of the marked binary image respectively, and performing an equivalent mapping transformation on each connected element in the element column according to the mark by using the thread to obtain a marked connected component image.
[0028] From the above description, the beneficial effects of the present application are that a thread is assigned to each element row of the obtained binary image respectively, and the element row is marked by using the thread to obtain a marked binary image, a thread is assigned to each element column of the marked binary image respectively, and an equivalent mapping transformation is performed on each connected element in the element column according to the mark by using the thread to obtain a marked connected component image, so that the extraction of connected components is performed simultaneously by using GPU multi-threading, and compared with the existing single-threaded calculation, the calculation speed is faster, thereby effectively improving the calculation efficiency.
[0029] Further, the marking of the connected elements in the element row by using the thread to obtain a marked binary image comprises:
[0030] Defining a mark value by using the thread, the mark value being initially 0;
[0031] Traversing each element in the element row until all elements in the element row have been traversed to obtain a marked binary image;
[0032] For the current element traversed, determining whether the element value of the current element is a first preset value, if yes, then adding one to the mark value to obtain an updated mark value, and marking the current element by using the updated mark value to obtain a marked element;
[0033] Determining a next element of the marked element, and determining whether the element value of the next element is the first preset value, if the first preset value, then marking the next element by using the updated mark value to obtain a marked element, if not the first preset value, then not marking the next element, and continuing to traverse the next element;
[0034] If not, then continuing to traverse the next element.
[0035] It can be known from the above description that the single element row is marked by the assigned thread to obtain the marked binary image, and the row marking time can be effectively reduced.
[0036] Further, after the connected elements in the element row are marked by the thread to obtain the marked binary image, the method further comprises:
[0037] determining the total number of the marking values of each element row in the marked binary image;
[0038] re-mapping the marking values in the marked binary image according to the total number in sequence according to the marking order of the element row to obtain a re-mapped binary image;
[0039] allocating a thread to each element column of the marked binary image respectively comprises:
[0040] allocating a thread to each element column of the re-mapped binary image respectively.
[0041] It can be known from the above description that the marking values in the marked binary image are re-mapped according to the total number in sequence according to the marking order of the element row, and the marking in the re-mapped binary image can be ensured to be unique, so as to mark the connected components subsequently.
[0042] Further, the equivalent mapping transformation of each connected element in the element column according to the marking by the thread to obtain the image of the marked connected components comprises:
[0043] determining the minimum marking value and the maximum marking value in the re-mapped binary image;
[0044] constructing an initial mapping table according to the maximum marking value, the initial mapping table comprising each marking value, a first mapping value corresponding to the each marking value and a second mapping value;
[0045] iterating each element with a marking value in each element column in sequence until all elements with a marking value in all element columns are iterated;
[0046] for the current element iterated, judging whether the marking value of the current element is the minimum marking value, if yes, determining a target element connected with the current element, and modifying the first mapping value corresponding to the marking value of the target element to the minimum marking value, if not, judging whether the current element and the next element of the current element are connected;
[0047] If not, no operation is performed, and if yes, it is determined whether the next element has multiple uplink connected elements, and if yes, the updated first mapping table is obtained by taking the label value of the next element as the second mapping value corresponding to the label value of the current element in the initial mapping table, and if not, the updated first mapping table is obtained by modifying the first mapping value corresponding to the label value of the next element to the label value of the current element in the initial mapping table;
[0048] The first mapping value in the updated first mapping table is sequentially conducted mapping based on the remapped binary image in the order of label values from small to large, and the updated second mapping table is obtained.
[0049] The second mapping value in the updated second mapping table is sequentially conducted mapping based on the remapped binary image in the order of label values from small to large, and the updated third mapping table is obtained.
[0050] The label of the remapped binary image is equivalently transformed based on the updated third mapping table by using threads, and the image of the label connected component is obtained.
[0051] As can be seen from the above description, the element column is equivalently mapped and transformed by using the allocated threads, and the mapping table is used to implement, which can reduce the comparison label time and improve the extraction efficiency.
[0052] Further, the first mapping value in the updated first mapping table is sequentially conducted mapping based on the remapped binary image in the order of label values from small to large, and the updated second mapping table is obtained.
[0053] Each element with a label value of the remapped binary image is traversed in the order of label values from small to large until all elements with a label value in the remapped binary image are traversed.
[0054] For the current element traversed, if the current element has no uplink connected element, the first mapping value corresponding to the label value of the current element in the updated first mapping table is modified to the label value of the current element itself, and the updated second mapping table is obtained, otherwise, it is determined whether the first mapping value corresponding to the label value of the current element exists in the updated first mapping table.
[0055] If yes, the first mapping value corresponding to the label value of the current element is traced back to obtain a first tracing value, and the first mapping value corresponding to the label value of the current element in the updated first mapping table is modified by using the first tracing value, and the updated second mapping table is obtained.
[0056] If not, the first mapping value in the first mapping table corresponding to the label value of the current element is modified as the label value of the current element itself, obtaining an updated second mapping table.
[0057] From the above description, the first mapping value in the first mapping table is sequentially conductive mapped based on the remapped binary image in the order of label value from small to large, obtaining the second mapping table as the basis for subsequent equivalent transformation, ensuring the accuracy of connected component extraction.
[0058] Further, the sequentially conductive mapping of the second mapping value in the updated second mapping table based on the remapped binary image in the order of label value from small to large to obtain the updated third mapping table includes:
[0059] Traverse each element with a label value of the remapped binary image in the order of label value from small to large until all elements with a label value in the remapped binary image have been traversed;
[0060] For the current element traversed, if the label value of the current element is the smallest label value, the second mapping value in the updated second mapping table corresponding to the label value of the current element is modified as the label value of the current element itself, obtaining the updated third mapping table, otherwise, it is determined whether the second mapping value corresponding to the label value of the current element exists in the updated second mapping table;
[0061] If it exists, the second mapping value corresponding to the label value of the current element is traced back to obtain a second trace value, and the second mapping value in the updated second mapping table corresponding to the label value of the current element is modified using the second trace value, obtaining the updated third mapping table;
[0062] If not, the first mapping value corresponding to the label value of the current element is traced back to obtain a third trace value, and the second mapping value in the updated second mapping table corresponding to the label value of the current element is obtained using the third trace value as the second mapping value.
[0063] From the above description, the second mapping value in the second mapping table is sequentially conductive mapped based on the remapped binary image in the order of label value from small to large, thereby realizing the equivalence of multiple columns of equivalent labels.
[0064] Further, before the thread based on the updated third mapping table performs equivalent transformation on the label of the remapped binary image to obtain the image of the labeled connected component, it further includes:
[0065] updating the second mapping value in the updated third mapping table as a continuous value to obtain a final mapping table;
[0066] the step of performing equivalent transformation on the labels of the remapped binary image based on the final mapping table by using the thread to obtain the image of the label connected components.
[0067] the step of performing equivalent transformation on the labels of the remapped binary image based on the final mapping table by using the thread to obtain the image of the label connected components.
[0068] From the above description, it can be seen that the second mapping value in the third mapping table is updated as a continuous value to obtain a final mapping table, so as to smoothly extract the connected components subsequently.
[0069] the step of performing equivalent transformation on the labels of the remapped binary image based on the final mapping table by using the thread to obtain the image of the label connected components.
[0070] finding a target second mapping value corresponding to the label of the remapped binary image from the final mapping table;
[0071] the step of modifying the label of the remapped binary image by using the target second mapping value by using the thread to obtain the image of the label connected components.
[0072] From the above description, it can be seen that the target second mapping value corresponding to the label of the remapped binary image is found from the final mapping table, and the label of the remapped binary image is modified by using the target second mapping value to obtain the image of the label connected components, so that the connected components in the image are simply and effectively extracted.
[0073] the step of modifying the label of the remapped binary image by using the target second mapping value by using the thread to obtain the image of the label connected components.
[0074] the step of modifying the label of the remapped binary image by using the target second mapping value by using the thread to obtain the image of the label connected components.
[0075] From the above description, it can be seen that the label in the image is modified by using the multi-thread, and the processing efficiency is improved.
[0076] Please refer to Figure 2 Another embodiment of the present application provides a connected component extraction terminal, which comprises a memory, a processor, and a computer program stored in the memory and executable on the processor, and the processor implements each step in the connected component extraction method when executing the computer program.
[0077] The extraction method and the terminal of the connected component can be applied to an automatic image analysis scene, and the specific embodiments are described below.
[0078] Please refer to Figure 1 、 Figures 3-7 , the embodiment one of the present application is:
[0079] An extraction method of a connected component, comprising the steps of:
[0080] S1, acquiring a binary image.
[0081] The binary image is an image whose element value of an element is 0 or 1, as shown in Figure 3 .
[0082] S2, a thread is assigned to each element row of the binary image respectively, and the connected elements in the element row are marked by using the thread to obtain a marked binary image, specifically comprising:
[0083] S21, a thread is assigned to each element row of the binary image respectively, as shown in Figure 4 .
[0084] S22, a marking value x is defined by using the thread, and the marking value is initially 0, that is, x=0.
[0085] S23, each element in the element row is traversed until all elements in the element row are traversed, and a marked binary image is obtained, as shown in Figure 4 .
[0086] S231, for the current element traversed, it is judged whether the element value of the current element is a first preset value, if yes, S2311-S2312 are executed, if not, the next element is continuously traversed.
[0087] The first preset value is 1.
[0088] S2311, the marking value is added by one to obtain an updated marking value, that is, x+1, and the current element is marked by using the updated marking value to obtain a marked element.
[0089] S2312, the next element of the marked element is determined, and it is judged whether the element value of the next element is the first preset value, if the first preset value, S23121 is executed, if not the first preset value, S23122 is executed.
[0090] S23121, the next element is marked by using the updated marking value to obtain a marked element.
[0091] S23122, do not mark the next element, and continue to traverse the next element.
[0092] S3, determine the total sum of the number of marked values of each element row in the marked binary image.
[0093] S4, remap the marked values in the marked binary image according to the total sum of the number of element rows in the marked binary image according to the marking order of the element rows, to obtain a remapped binary image.
[0094] For example, as shown in Figure 4 , the total sum of the number of element rows of the second row is 2, and the marked value of the element row is mapped to 1, 2 in turn, the total sum of the number of element rows of the third row is 2, and the marked value of the element row is mapped to 3, 4 in turn, the total sum of the number of element rows of the fourth row is 3, and the marked value of the element row is mapped to 5, 6, 7 in turn, and so on. Finally, as shown in Figure 5 , the remapped binary image is obtained, and the marked value of the remapped binary image is unique.
[0095] S5, a thread is allocated to each element column of the marked binary image, and the thread is used to perform equivalent mapping transformation on each connected element in the element column according to the marking, to obtain a marked connected component image, which specifically includes:
[0096] S51, a thread is allocated to each element column of the remapped binary image, as shown in the figure.
[0097] S52, determine the minimum marked value and the maximum marked value in the remapped binary image.
[0098] As shown in Figure 5 , the minimum marked value in the remapped binary image is 1, and the maximum marked value is 11.
[0099] S53, construct an initial mapping table according to the maximum marked value, the initial mapping table includes each marked value, a first mapping value corresponding to the each marked value and a second mapping value.
[0100] For example, the maximum marked value is 11, and an initial mapping table as shown in Table 1 can be constructed, the marked value is from 1-11, and the first mapping value and the second mapping value in it can be empty.
[0101] Table 1 Initial mapping table
[0102] Label value 1 2 3 4 5 6 7 8 9 10 11 [[ L o ]]> [[ L r ]]>
[0103] L0 represents the first mapping value corresponding to the each marked value, and L rL0 represents when one uplink mark corresponds to one downlink mark or one uplink mark corresponds to multiple downlink marks, and L represents when multiple uplink marks correspond to one downlink mark. r represents.
[0104] In another alternative implementation, further comprising: initializing the first mapping value and the second mapping value in the initial mapping table as the mark value itself corresponding thereto, as shown in Table 2.
[0105] Table 2 Another initial mapping table
[0106] Label value 1 2 3 4 5 6 7 8 9 10 11 [[ L o ]]> 1 2 3 4 5 6 7 8 9 10 11 [[ L r ]]> 1 2 3 4 5 6 7 8 9 10 11
[0107] S54, traversing each marked value element in each element column in sequence until all marked value elements in all element columns are traversed.
[0108] For example, as shown in FIG. 5B, the element with mark value 1 in the first element column is traversed first, then the element with mark value 5 in the first element column, then the element with mark value 1 in the second element column, then the element with mark value 3 in the second element column, then the element with mark value 5 in the second element column, and so on, until all marked value elements in all element columns are traversed. Figure 5
[0109] S541, for the current element being traversed, judging whether the mark value of the current element is the minimum mark value, if yes, performing S5411, if not, performing S5412.
[0110] For example, as shown in FIG. 5B, assuming that the element with mark value 1 in the first element column is traversed, and the mark value is the minimum mark value, then S5411 is performed, and assuming that the element with mark value 3 in the second element column is traversed, and the mark value is not the minimum mark value, then S5412 is performed. Figure 5
[0111] S5411, determining a target element connected with the current element, and modifying the first mapping value corresponding to the mark value of the target element as the minimum mark value.
[0112] In an alternative implementation, if the current element has no target element connected therewith, no operation is performed.
[0113] For example, as shown in FIG. 5B, assuming that the element with mark value 1 in the first element column is traversed, and the mark value is the minimum mark value, then S5411 is performed, and assuming that the element with mark value 3 in the second element column is traversed, and the mark value is not the minimum mark value, then S5412 is performed. Figure 5 As shown, assuming that the element with the first element column marker value of 1 is traversed, the element has no target element connected therewith, no operation is performed, assuming that the element with the second element column marker value of 3 is traversed, the target element connected with the element is the element with the second element column marker value of 3, the first mapping value corresponding to the marker value of the element is modified to 1, and the like.
[0114] S5412, it is judged whether the current element and the next element of the current element are connected or not, if not connected, no operation is performed, if connected, S54121 is performed.
[0115] For example, as shown in Table 2, the element with the second element column marker value of 3 is connected with the element with the next marker value of 5, S54121 is performed, and the element with the fourth element column marker value of 2 is connected with the element with the next marker value of 3, S54121 is performed. Figure 5
[0116] S54121, it is judged whether the next element has multiple uplink connected elements or not, if yes, S541211 is performed, if not, S541212 is performed.
[0117] For example, as shown in Table 2, the element with the second element column marker value of 5 has no multiple uplink connected elements, it has only one uplink connected element, i.e. the element with the marker value of 3, so S541212 is performed, and the element with the fourth element column marker value of 3 has multiple uplink connected elements, i.e. the element with the marker value of 1 and the element with the marker value of 2, so S541211 is performed. Figure 5
[0118] S541211, in the initial mapping table, the marker value of the next element is taken as the second mapping value corresponding to the marker value of the current element, to obtain an updated first mapping table.
[0119] For example, the second mapping value of the element with the fourth element column marker value of 2 is changed to 3.
[0120] S541212, in the initial mapping table, the first mapping value corresponding to the marker value of the next element is modified to the marker value of the current element, to obtain an updated first mapping table.
[0121] For example, the first mapping value of the element with the second element column marker value of 5 is changed to 3.
[0122] After all the traversing is completed, the updated first mapping table obtained is shown in Table 3.
[0123] Table 3 Updated first mapping table
[0124] Label value 1 2 3 4 5 6 7 8 9 10 11 [[ L o ]]> 1 3 3 4 7 8 8 [[ L r ]]> 3
[0125] S55. Following the ascending order of the label values, sequentially perform propagation mapping on the first mapping values in the updated first mapping table based on the remapped binary image to obtain the updated second mapping table, specifically including:
[0126] S551. Traverse each element with a labeled value in the remapped binarized image in ascending order of the labeled value, until all elements with labeled values in the remapped binarized image have been traversed.
[0127] For example, such as Figure 5 As shown, the elements with a label value of 1 are traversed first, then the elements with a label value of 2 are traversed, then the elements with a label value of 3 are traversed, and so on, until all elements with label values in the remapped binarized image have been traversed.
[0128] S5511. For the current element that has been traversed, if the current element does not have an upstream connected element, then modify the first mapping value corresponding to the tag value of the current element in the updated first mapping table to the tag value of the current element itself, and obtain the updated second mapping table; otherwise, determine whether there is a first mapping value corresponding to the tag value of the current element in the updated first mapping table; if it exists, then execute S55111; if it does not exist, then execute S55112.
[0129] For example, such as Figure 5 As shown, the element with a label value of 3 has two upstream connected elements, namely the element with a label value of 1 and the element with a label value of 2. Then, it should be determined whether there is a first mapping value in the updated first mapping table that corresponds to the label value of the current element. If it exists, then execute S55111; if it does not exist, then execute S55112. Since the element with a label value of 1 does not have an upstream connected element, the first mapping value of the element with a label value of 1 in Table 3 is modified to the label value of the element itself, that is, changed to 1, and the updated second mapping table is obtained.
[0130] S55111. Trace back the first mapping value corresponding to the tag value of the current element to obtain a first trace value, and use the first trace value to modify the first mapping value corresponding to the tag value of the current element in the updated first mapping table to obtain an updated second mapping table.
[0131] For example, such as Figure 5 As shown, the element with a label value of 5 has an upstream connected element, namely the element with a label value of 3. And the element with a label value of 5 in Table 3 has a first mapping value of 3. Since the label 3 corresponding to the first mapping value 3 is mapped to 1, it can be determined that the first trace value is 1, and the first mapping value of the element with a label value of 5 is changed to 1.
[0132] S55112. Modify the first mapping value in the first mapping table corresponding to the tag value of the current element to the tag value of the current element itself, and obtain the updated second mapping table.
[0133] After all iterations are completed, the updated second mapping table will be obtained, as shown in Table 4.
[0134] Table 4 Updated Second Mapping Table
[0135] Label value 1 2 3 4 5 6 7 8 9 10 11 [[ L o ]]> 1 2 1 4 1 1 4 4 9 4 4 [[ L r ]]> 3
[0136] S56. Following the ascending order of the label values, sequentially perform propagation mapping on the second mapping values in the updated second mapping table based on the remapped binary image to obtain the updated third mapping table, specifically including:
[0137] S561. Traverse each element with a labeled value in the remapped binarized image in ascending order of the labeled value, until all elements with labeled values in the remapped binarized image have been traversed.
[0138] S5611. For the current element that has been traversed, if the tag value of the current element is the minimum tag value, then modify the second mapping value corresponding to the tag value of the current element in the updated second mapping table to the tag value of the current element itself, and obtain the updated third mapping table; otherwise, determine whether there is a second mapping value corresponding to the tag value of the current element in the updated second mapping table; if it exists, then execute S56111; if it does not exist, then execute S56112.
[0139] For example, such as Figure 5 As shown, the element with a flag value of 1 has the minimum flag value, so the second mapping value of the element is changed to the flag value of the element itself, which is 1. The element with a flag value of 2 does not have the minimum flag value, so it should be determined whether there is a second mapping value in Table 4 corresponding to the element with a flag value of 2. If it exists, then execute S56111; if it does not exist, then execute S56112.
[0140] S56111. Trace back the second mapping value corresponding to the tag value of the current element to obtain the second trace value, and use the second trace value to modify the second mapping value corresponding to the tag value of the current element in the updated second mapping table to obtain the updated third mapping table.
[0141] Since the element with a mark value of 2 in Table 4 has a corresponding second mapping value of 3, and since the mark value 3 corresponding to the second mapping value 3 is mapped to 1, it can be determined that the second trace value is 1, and the second mapping value corresponding to the element with a mark value of 2 is changed to 1.
[0142] S56112. Trace back the first mapping value corresponding to the tag value of the current element to obtain the third trace value, and use the third trace value as the second mapping value corresponding to the tag value of the current element in the updated second mapping table to obtain the updated third mapping table.
[0143] As shown in Table 4, if an element with a label value of 3 does not have a corresponding second mapping value, then the first mapping value of the element with a label value of 3 is taken as the third trace value, which is 1. Therefore, the second mapping value corresponding to the element with a label value of 3 is 1.
[0144] After all iterations are completed, the updated third mapping table will be obtained, as shown in Table 5.
[0145] Table 5: Updated Third Mapping Table
[0146] Label value 1 2 3 4 5 6 7 8 9 10 11 [[ L o ]]> 1 2 1 4 1 1 4 4 9 4 4 [[ L r ]]> 1 1 1 4 1 1 4 4 9 4 4
[0147] S57. Update the second mapping value in the updated third mapping table to a continuous value to obtain the final mapping table.
[0148] For example, if the second mapping value in the updated third mapping table includes 1, 4, and 9, then it is updated to the consecutive values 1, 2, and 3 to obtain the final mapping table, as shown in Table 6.
[0149] Table 6 Final Mapping Table
[0150]
[0151]
[0152] S58. Using a thread, perform an equivalent transformation on the labels of the remapped binarized image based on the updated third mapping table to obtain an image of labeled connected components.
[0153] Specifically, such as Figure 6 As shown, the thread performs an equivalent transformation on the labels of the remapped binarized image based on the final mapping table to obtain an image of labeled connected components, specifically including:
[0154] S581. Find the target second mapping value corresponding to the label of the remapped binarized image from the final mapping table.
[0155] S582, the thread uses the target second mapping value to modify the label of the remapped binary image, and an image of a labeled connected component is obtained.
[0156] Specifically, the thread uses the target second mapping value to modify the label in the element column of the remapped binary image, and an image of a labeled connected component is obtained, as shown in Figure 7
[0157] In the prior art, single-threaded serial operation is required for each pixel point, and if an image has a size of M*N and the average running time of a single pixel point is T, the total operation time is M*N*T. After the present application adds GPU multi-threaded parallel operation, the parallel calculation times are M+N, the average running time of a single pixel point is T, and the operation time is (M+N)*T, which greatly reduces the operation time and improves the efficiency.
[0158] Please refer to Figure 2 Embodiment two of the present application is:
[0159] A connected component extraction terminal includes a memory, a processor, and a computer program stored in the memory and executable on the processor, and the processor implements each step in the connected component extraction method of embodiment one when executing the computer program.
[0160] In summary, the present application provides a connected component extraction method and terminal, each element row of the obtained binary image is respectively allocated a thread, and the element row is labeled by the thread to obtain a labeled binary image, each element column of the labeled binary image is respectively allocated a thread, and each connected element in the element column is equivalently mapped and transformed according to the label by the thread to obtain an image of a labeled connected component, so that the GPU multi-thread is used to simultaneously extract the connected component, and compared with the existing single-threaded calculation, the calculation speed is faster, thereby effectively improving the calculation efficiency; in addition, the target second mapping value corresponding to the label of the remapped binary image is searched from the final mapping table, the target second mapping value is used to modify the label of the remapped binary image, and an image of a labeled connected component is obtained, so that the connected component in the image is simply and effectively extracted.
[0161] The above only describes the embodiments of the present application, and does not limit the patent range of the present application, and any equivalent transformation made by using the content of the present application specification and drawings, or direct or indirect application in related technical fields, are also included in the patent protection range of the present application.
Claims
1. A method of extracting connected components, characterized by, The method comprises the steps of: obtaining a binary image; allocating a thread to each element row of the binary image respectively, and marking the connected elements in the element row by using the thread to obtain a marked binary image; allocating a thread to each element column of the marked binary image respectively, and performing an equivalent mapping transformation on each connected element in the element column according to the mark by using the thread to obtain a marked connected component image; after the step of marking the connected elements in the element row by using the thread to obtain the marked binary image, the method further comprises the steps of: determining the total sum of the mark values of each element row in the marked binary image; re-mapping the mark values in the marked binary image according to the total sum in the order of the mark sequence of the element row to obtain a re-mapped binary image; allocating a thread to each element column of the marked binary image respectively comprises the steps of: allocating a thread to each element column of the re-mapped binary image respectively; the step of performing the equivalent mapping transformation on each connected element in the element column according to the mark by using the thread to obtain the marked connected component image comprises the steps of: determining the minimum mark value and the maximum mark value in the re-mapped binary image; constructing an initial mapping table according to the maximum mark value, the initial mapping table comprising each mark value, a first mapping value corresponding to the each mark value, and a second mapping value corresponding to the each mark value; sequentially traversing each element with a mark value in each element column until all elements with a mark value in all element columns have been traversed; for a current element that has been traversed, determining whether the mark value of the current element is the minimum mark value, if yes, determining a target element connected with the current element, and modifying the first mapping value corresponding to the mark value of the target element to the minimum mark value, if no, determining whether the current element is connected with a next element of the current element; if not connected, no operation is performed, if connected, determining whether the next element has multiple uplink connected elements, if yes, in the initial mapping table, the mark value of the next element is taken as the second mapping value corresponding to the mark value of the current element to obtain an updated first mapping table, if not, in the initial mapping table, the first mapping value corresponding to the mark value of the next element is modified to the mark value of the current element to obtain an updated first mapping table; performing a conductive mapping on the first mapping values in the updated first mapping table according to the re-mapped binary image in the order of the mark value from small to large to obtain an updated second mapping table; performing a conductive mapping on the second mapping values in the updated second mapping table according to the re-mapped binary image in the order of the mark value from small to large to obtain an updated third mapping table; performing an equivalent transformation on the mark of the re-mapped binary image based on the updated third mapping table by using the thread to obtain the marked connected component image.
2. The method of claim 1, wherein, The marking the connected elements in the element row by using the thread to obtain a marked binary image comprises: Defining a mark value by using the thread, the mark value being initially 0; Traversing each element in the element row until all elements in the element row are traversed to obtain a marked binary image; For a current element traversed, judging whether the element value of the current element is the first preset value, if yes, adding 1 to the mark value to obtain an updated mark value, and marking the current element by using the updated mark value to obtain a marked element; Determining a next element of the marked element, and judging whether the element value of the next element is the first preset value, if being the first preset value, marking the next element by using the updated mark value to obtain a marked element, if not being the first preset value, not marking the next element, and continuing to traverse the next element; If no, continuing to traverse the next element.
3. The method of claim 1, wherein, The conducting mapping of the first mapping values in the updated first mapping table based on the remapped binary image in the order of mark values from small to large to obtain an updated second mapping table comprises: Traversing each element with a mark value of the remapped binary image in the order of mark values from small to large until all elements with mark values in the remapped binary image are traversed; For a current element traversed, if the current element does not have an uplink connected element, modifying the first mapping value corresponding to the mark value of the current element in the updated first mapping table to the mark value of the current element itself to obtain an updated second mapping table, otherwise, judging whether the updated first mapping table has the first mapping value corresponding to the mark value of the current element; If yes, tracing the first mapping value corresponding to the mark value of the current element to obtain a first tracing value, and modifying the first mapping value corresponding to the mark value of the current element in the updated first mapping table by using the first tracing value to obtain an updated second mapping table; If no, modifying the first mapping value corresponding to the mark value of the current element in the first mapping table to the mark value of the current element itself to obtain an updated second mapping table.
4. The method of claim 1, wherein, The conducting mapping of the second mapping values in the updated second mapping table based on the remapped binary image in the order of mark values from small to large to obtain an updated third mapping table comprises: Traversing each element with a mark value of the remapped binary image in the order of mark values from small to large until all elements with mark values in the remapped binary image are traversed; If the tag value of the current element is the minimum tag value, the second mapping value corresponding to the tag value of the current element in the updated second mapping table is modified to the tag value of the current element itself to obtain an updated third mapping table; otherwise, it is determined whether the second mapping table has a second mapping value corresponding to the tag value of the current element; If the second mapping table has the second mapping value corresponding to the tag value of the current element, the second mapping value is traced to obtain a second trace value, and the second mapping value corresponding to the tag value of the current element in the updated second mapping table is modified using the second trace value to obtain an updated third mapping table; If the second mapping table does not have the second mapping value corresponding to the tag value of the current element, the first mapping value corresponding to the tag value of the current element is traced to obtain a third trace value, and the third trace value is used as the second mapping value corresponding to the tag value of the current element in the updated second mapping table to obtain an updated third mapping table.
5. The method of claim 1, wherein, Before the thread performs equivalent transformation on the tags of the remapped binary image based on the updated third mapping table to obtain a label connected component image, the method further includes: updating the second mapping value in the updated third mapping table to a continuous value to obtain a final mapping table; the thread performs equivalent transformation on the tags of the remapped binary image based on the updated third mapping table to obtain a label connected component image includes: the thread performs equivalent transformation on the tags of the remapped binary image based on the final mapping table to obtain a label connected component image.
6. The method of claim 5, wherein, the thread performs equivalent transformation on the tags of the remapped binary image based on the final mapping table to obtain a label connected component image includes: finding a target second mapping value corresponding to the tag of the remapped binary image from the final mapping table; the thread modifies the tag of the remapped binary image using the target second mapping value to obtain a label connected component image.
7. The method of claim 6, wherein, the thread modifies the tag of the remapped binary image using the target second mapping value to obtain a label connected component image includes: the thread modifies the tag in the element column of the remapped binary image using the target second mapping value to obtain a label connected component image.
8. An extraction terminal of connected components comprising a memory, a processor and a computer program stored on the memory and executable on the processor, characterized in that, The processor implements each step in the method for extracting a connected component according to any one of claims 1 to 7 when executing the computer program.
Citation Information
Patent Citations
Image parallel processing method and device, and computer storage medium
CN109493318A