A fast image threshold segmentation method using run length encoding as storage medium

By adopting a method of using stroke encoding as a storage medium in image threshold segmentation, and using parallel computing and batch processing technology, the problems of long time and low space utilization in the prior art are solved, and rapid processing and efficient resource utilization of image threshold segmentation are realized.

CN118279338BActive Publication Date: 2025-04-08NANJING HUASHI INTELLIGENT TECH CO LTD
View PDF 2 Cites 0 Cited by

Patent Information

Application Number
CN202410363805.6
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2024-03-28
Publication Date
2025-04-08
Estimated Expiration
2044-03-28

AI Technical Summary

Technical Problem

When processing larger images, the existing image threshold segmentation method takes a lot of time to calculate and has low space utilization, making it difficult to meet the needs of industrial vision detection scenarios with high requirements for detection speed.

Method used

Using a fast image threshold segmentation method with stroke encoding as storage medium, the data partitioning module, the inter-row threshold segmentation module and the data merging module are used to perform parallel calculations and batch processing, thereby improving the efficiency of computer resources.

Benefits of technology

It realizes rapid processing of image threshold segmentation, speeds up the extraction of areas of interest, improves the efficiency of computer resources, and has practical application value in industrial vision detection scenarios.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN118279338B_ABST
    Figure CN118279338B_ABST
Patent Text Reader

Abstract

The present invention discloses a method for rapid threshold segmentation of an image with run-length coding as a storage medium, specifically relates to the field of image processing technology, and includes the following steps: Step 1: Setting a data partitioning module, an inter-row threshold segmentation module, and a data merging module; Step 2: The data partitioning module utilizes the characteristics that the run-length coding retains the inter-row order in the image, the run-length coding of different rows is independent of each other, and the adjacent run-length coding in the row is related to each other, and the row is partitioned in a continuous row number manner according to the number of threads in the existing thread pool, with each thread being responsible for data processing in each area. The present invention realizes the rapid implementation of image threshold segmentation with run-length coding as a storage medium, speeds up the processing speed of extracting regions of interest, increases the efficiency of using computer resources, and has practical application value in industrial visual inspection scenarios with high requirements for detection speed.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention relates to the field of image processing technology, and more specifically, to a method for fast image threshold segmentation using run length coding as a storage medium. Background Art

[0002] In the field of image processing, image threshold segmentation is a very important and basic step. For example, in the detection of some industrial products, it is necessary to first use the threshold range of interest to perform threshold segmentation to obtain the target area, and then obtain multiple sub-areas from the target area by disconnection. It can be said that as long as the detection process requires bl ob analysis of the area, it is inevitable to use image threshold segmentation to obtain the target area, because threshold segmentation is an indispensable step in converting image data into regional data, and it is a bridge between the two.

[0003] Existing threshold segmentation implementations are mainly based on images, and use binary images to store the areas of grayscale value intervals of interest in the form of masks. This method usually has low space utilization. For example, in a larger image, no matter how small the area of ​​interest is, a mask of the same size is required to fully represent it. There are also some threshold segmentation implementations that use run-length coding as a medium, which utilizes the characteristics of the above-mentioned mask being a binary image and uses run-length coding in compression coding as a storage medium, which greatly improves space utilization. However, because run-length coding is a characteristic of compression coding, there is a relationship between the compression of the front and back pixels in the process of threshold segmentation, and serial implementation is required, resulting in the current algorithm implementations all traversing pixel by pixel to obtain the area represented by the run-length coding, which will take more time to calculate when encountering larger images. Summary of the invention

[0004] In order to overcome the above-mentioned defects of the prior art, an embodiment of the present invention provides a method for fast threshold segmentation of images with run-length coding as a storage medium. The present invention realizes the fast implementation of threshold segmentation of images with run-length coding as a storage medium, speeds up the processing speed of extracting areas of interest, increases the efficiency of using computer resources, and has practical application value in industrial visual inspection scenarios with high requirements on detection speed, so as to solve the problems raised in the above-mentioned background technology.

[0005] To achieve the above object, the present invention provides the following technical solution: a method for fast image threshold segmentation using run length coding as a storage medium, comprising the following steps:

[0006] Step 1: Set up a data partitioning module, an inter-row threshold segmentation module, and a data merging module;

[0007] Step 2: The data partitioning module utilizes the characteristics that the run length encoding retains the order between rows in the image, the run length encodings of different rows are independent of each other, and the adjacent run length encodings in a row are related to each other. The image is selected as a unit, and the rows are partitioned in a continuous row number manner according to the number of threads in the existing thread pool. Each thread is responsible for data processing in each area, and each area can be processed in parallel, so as to improve the use efficiency of the computer in the processing, and the result of the current area is temporarily retained in the current thread;

[0008] Step 3: The inter-row threshold segmentation module divides each row of data into 32 data as a vector and processes them separately, where every 8 data are called a group, and uses a flag bit 1 astu8 to record whether the last bit of data in the previous group meets the threshold segmentation condition. The data of a vector is batch processed based on the AVX instruction set and the sign bit is merged to obtain a 32-bit int value, where each bit 1 / 0 represents whether the corresponding data meets the threshold segmentation condition, and this 32-bit int value is regarded as 4 8-bit uchar values, and each uchar value selects the corresponding function in a lookup table with a fixed 256 functions to add the corresponding run length encoding to the temporary result and modify the flag bit 1 astu8, and traverse in sequence;

[0009] Step 4: The data merging module selects to insert the result of the current area into the end of the actual result data in order according to the area number after each thread traverses all the rows required to be calculated in the current area. The data of a vector is batch processed based on the AVX instruction set. The AVX instruction set only provides the processing of the less than operation of the char type. Therefore, according to the binary representation of the char type and the uchar type, the highest bit of the input uchar type is negated to normally apply the less than sign operation of the char type to the uchar type data. There are maximum and minimum thresholds for threshold segmentation, so it is necessary to compare the result of the input data less than the minimum threshold and the result of the maximum threshold less than the input data or the result of 0x00 or 0xff. The sign bit is merged and a 32-bit int value is obtained. For a 256-bit data, the AVX instruction set provides the creation of a mask whose 32 bits correspond to the most significant bit of each byte of the 32-bit int value obtained by the sign bit merging process. The instruction is used to merge the sign bit merging operation of 32 bytes. The lookup table with a fixed 256 functions is called the function number in the function lookup table. The uchar value merged into the current sign bit is called the function number. astu8 flag, as well as the current row number and the column number corresponding to the first data of the current group are used as input data to execute the function of the corresponding function number, where the logic of each function is: if the highest binary bit of the function number is 0, then modify the l astu8 sign bit to false, otherwise the l astu8 sign bit is assigned to true; if the lowest bit is 1, it is necessary to determine whether the previous l astu8 flag is true. If it is true, directly modify the ce value of the last run-length code in the temporary result to be the column number of the first data that is 0 starting from the lowest bit, otherwise add a new run-length code with the current row number, the column number of the first data in the group is the start of the run, and the column number of the first data that is 0 starting from the lowest bit is the end of the run, and then observe from the low-order data to the high-order data in turn, and add the run-length codes one by one into the temporary result with continuous or single 1. When the highest binary bit of the flag bit in a group of data is 1, the column end of the run-length code last added to the group should be the column where the data at the end of the current group is located.

[0010] In a preferred embodiment, the row numbers are partitioned in a continuous manner according to the number of threads in the existing thread pool. Considering that each time data is added to the thread for calculation, there is performance loss, and the threshold segmentation step is very basic, its absolute time consumption is relatively low, and each call to the thread operation will be a significant performance bottleneck. Therefore, before thread scheduling, the number of existing thread pools num, the total number of rows rows, and the number of continuous rows responsible for each partition and the number of rows remaining to be put into the main thread for calculation are obtained by every=rows / num and remainder=rows%num. In the method, block selection is selected instead of row by row in the thread for calculation, which effectively reduces the number of thread scheduling times and allows the threshold segmentation to be carried out efficiently.

[0011] In a preferred embodiment, after each thread has traversed all the rows that need to be calculated in the current area, the result of the current area is inserted into the end of the actual result data in order according to the area number. After each thread calculates the final temporary result of the current area, the temporary result is placed into the final result in order according to the area number. This operation first stores the temporary result in each thread rather than inside the main thread, which distributes the loss of destructing and releasing temporary variables to each thread; secondly, the results are placed at the end of the actual result in order according to the area number. This operation utilizes the fact that the run length encoding retains the order between rows in the image and the run length encodings of different rows are independent of each other, thereby avoiding the subsequent step of sorting the actual results.

[0012] Technical effects and advantages of the present invention:

[0013] The present invention realizes rapid implementation of image threshold segmentation with run length encoding as storage medium, accelerates the processing speed of extracting regions of interest, increases the efficiency of using computer resources, and has practical application value in industrial visual inspection scenarios with high requirements on inspection speed. BRIEF DESCRIPTION OF THE DRAWINGS

[0014] Figure 1 It is a schematic diagram of the modules of the present invention.

[0015] Figure 2 This is a partition diagram of the row number continuous mode of the present invention.

[0016] Figure 3 It is a schematic diagram of vectorized threshold segmentation of the present invention.

[0017] Figure 4 It is a schematic diagram of the function lookup table of the present invention. DETAILED DESCRIPTION

[0018] The following will be combined with the drawings in the embodiments of the present invention to clearly and completely describe the technical solutions in the embodiments of the present invention. Obviously, the described embodiments are only part of the embodiments of the present invention, not all of the embodiments. Based on the embodiments of the present invention, all other embodiments obtained by ordinary technicians in this field without creative work are within the scope of protection of the present invention.

[0019] Example 1

[0020] As attached Figure 1 , Attachment Figure 2 , Attachment Figure 3 and attached Figure 4 As shown, the present invention provides a method for fast image threshold segmentation using run length coding as a storage medium, comprising the following steps:

[0021] Step 1: Set up a data partitioning module, an inter-row threshold segmentation module, and a data merging module;

[0022] Step 2: The data partitioning module utilizes the characteristics that the run length encoding retains the order between rows in the image, the run length encodings of different rows are independent of each other, and the adjacent run length encodings in a row are related to each other. The image is selected as a unit, and the rows are partitioned in a continuous row number manner according to the number of threads in the existing thread pool. Each thread is responsible for data processing in each area, and each area can be processed in parallel, so as to improve the use efficiency of the computer in the processing, and the result of the current area is temporarily retained in the current thread;

[0023] Step 3: The inter-row threshold segmentation module divides each row of data into 32 data as a vector and processes them separately, where every 8 data are called a group, and uses a flag bit 1 astu8 to record whether the last bit of data in the previous group meets the threshold segmentation condition. The data of a vector is batch processed based on the AVX instruction set and the sign bit is merged to obtain a 32-bit int value, where each bit 1 / 0 represents whether the corresponding data meets the threshold segmentation condition, and this 32-bit int value is regarded as 4 8-bit uchar values, and each uchar value selects the corresponding function in a lookup table with a fixed 256 functions to add the corresponding run length encoding to the temporary result and modify the flag bit 1 astu8, and traverse in sequence;

[0024] Step 4: The data merging module selects to insert the result of the current area into the end of the actual result data in order according to the area number after each thread has traversed all the rows required to be calculated in the current area. The data of a vector is batch processed based on the AVX instruction set. The AVX instruction set only provides the processing of the less than operation of the char type. Therefore, according to the binary representation of the char type and the uchar type, the highest bit of the input uchar type is negated to normally apply the less than sign operation of the char type to the uchar type data. There are maximum and minimum thresholds for threshold segmentation, so it is necessary to compare the result of the input data less than the minimum threshold with the result of the maximum threshold less than the input data or the result of 0x00 or 0xff. The sign bit is merged and a 32-bit int value is obtained. For a 256-bit data, the AVX instruction set provides the creation of a mask whose 32 bits correspond to the most significant bit of each byte of the 32-bit int value obtained by the sign bit merging process. The instruction is used to merge the sign bit merging operation of 32 bytes. The lookup table with a fixed 256 functions is called the function number in the function lookup table. The uchar value merged into the current sign bit is called the function number. astu8 flag, as well as the current row number and the column number corresponding to the first data of the current group as input data, execute the function of the corresponding function number. The logic of each function is: if the highest binary bit of the function number is 0, then modify the l astu8 sign bit to false, otherwise the l astu8 sign bit is assigned to true; if the lowest bit is 1, it is necessary to determine whether the previous l astu8 flag is true. If it is true, directly modify the ce value of the last run code in the temporary result to the column number of the first data with a zero starting from the lowest bit, otherwise add a new run code with the current row number, the column number of the first data in the group is the start of the run, and the column number of the first data with a zero starting from the lowest bit is the end of the run, and then observe from the low-order data to the high-order, and add the run codes one by one with continuous or single 1 into the temporary result. When the highest binary bit of the flag bit in a group of data is 1, the column end of the run code last added to the group should be the column where the data at the end of the current group is located.

[0025] The described method of partitioning rows by row numbers in a continuous manner according to the number of threads in the existing thread pool takes into account that each time data is added to the thread for calculation, there is a performance loss, and the threshold segmentation step is very basic, and its absolute time consumption is relatively low. Each thread call operation will be a significant performance bottleneck. Therefore, before thread scheduling, the number of existing thread pools num, the total number of rows rows, and the number of continuous rows responsible for each partition and the number of remaining rows that need to be put into the main thread for calculation are obtained through every=rows / num and remainder=rows%num. The method selects block calculation instead of putting rows into the thread for calculation, which effectively reduces the number of thread scheduling times and allows the threshold segmentation to be implemented efficiently.

[0026] After each thread has traversed all the rows that need to be calculated in the current area, the result of the current area is inserted into the end of the actual result data in order according to the area number. After each thread calculates the final temporary result of the current area, the temporary result is placed in the final result in order according to the area number. This operation first stores the temporary result in each thread rather than inside the main thread, which distributes the loss of destructing and releasing temporary variables to each thread; secondly, the results are placed at the end of the actual result in order according to the area number. This operation utilizes the fact that the run length encoding retains the order between rows in the image and the run length encodings of different rows are independent of each other, thereby avoiding the subsequent step of sorting the actual results.

[0027] The present invention mainly includes the following three modules: a data partitioning module, an inter-row threshold segmentation module and a data merging module. The data partitioning module utilizes the characteristics that the run length coding retains the inter-row order in the image, the run length coding of different rows is independent of each other, and the adjacent run length coding in the row is related to each other. The image is selected as a unit, and the rows are partitioned in a row number continuous manner according to the number of threads in the existing thread pool. Each thread is responsible for data processing in each area, and each area can be calculated and processed in parallel, so as to improve the use efficiency of the computer in the processing. The row is partitioned in a row number continuous manner considering that each time data is added to the thread for calculation, there is performance loss, and the threshold segmentation step is very basic, and its absolute time consumption is relatively low. Each call to the thread operation will be a significant performance bottleneck. Therefore, before thread scheduling, the number of existing thread pools num, the total number of rows rows, and every=rows / num and remainder=rows%num are used to obtain the number of continuous rows responsible for each partition and the number of rows remaining to be put into the main thread for calculation. The actual partitioning method is as follows Figure 2The method selects block calculation instead of row by row into the thread, which effectively reduces the number of thread scheduling times and allows the threshold segmentation to be implemented efficiently. The inter-row threshold segmentation module divides each row of data into 32 data vectors for separate processing, where each 8 data is called a group, and uses a flag bit lastu8 to record whether the last bit of the previous group meets the threshold segmentation condition. The data of a vector is batch processed based on the AVX instruction set and the sign bit is merged to obtain a 32-bit int value, where each bit 1 / 0 represents whether the corresponding data meets the threshold segmentation condition, and this 32-bit int value is regarded as 4 8-bit uchar values. You can refer to Figure 3 Then, for each uchar value, select the corresponding function in a lookup table with a fixed 256 functions, add the corresponding run length encoding to the temporary result and modify the flag bit l astu8, and traverse in sequence. The function implementation content in the lookup table can be referred to Figure 4 In the inter-row threshold segmentation module, 32 data batches are implemented in obtaining the threshold segmentation Boolean value result, and 8 data batches are implemented in converting the threshold segmentation Boolean value result into run-length encoding, which increases the data throughput by at least 8 times per unit time and greatly improves the CPU utilization. In the function lookup table, the uchar value formed by merging the current sign bit is called the function number, and the previous l astu8 flag bit, the current row number and the column number corresponding to the first data of the current group are used as input data to execute the function corresponding to the function number, where the logic of each function is: if the highest bit of the function number binary is 0, then modify the l astu8 sign bit to false, otherwise the l astu8 sign bit is assigned to true; if the lowest bit is 1, it is necessary to judge the previous l Whether the astu8 flag is true, if it is true, directly modify the ce value of the last run-length code in the temporary result to the column number of the first data with a 0 starting from the lowest bit, otherwise add a new current row number, the column number of the first data in the group is the start of the run, and the column number of the first data with a 0 starting from the lowest bit is the run-length code at the end of the run, and then observe from the low-order data to the high-order data in turn, and add the run-length codes one by one into the temporary result with continuous or single 1. When the highest bit of the binary flag bit in a group of data is 1, the column end of the run-length code added last in the group should be the column where the data at the end of the current group is located. The logic implemented in the function can also be referred to Figure 4, the method of using a function lookup table instead of using switch or if-else is used for implementation, because considering the branch prediction of the CPU, when the CPU pipeline encounters a branch, the CPU will guess which branch it may take. If it guesses correctly, the pipeline continues as usual. If it guesses wrong, all subsequent instructions that have been executed on the pipeline will be invalidated. Therefore, if the CPU guesses wrong, there will be performance loss. In the present invention, if the branch is counted, 256 branches will be involved and the probability of each branch is completely random, which will greatly curb the accuracy of the CPU branch prediction and cause many instructions that have been executed to be invalidated; therefore, the method of using a function lookup table is finally used, there is only one main branch, and the performance loss of the base address offset is only required once when calling the corresponding function, which greatly fits the CPU pipeline operation mode and improves the efficiency of the overall method, wherein the data merging module selects to perform a merging operation in each thread. After all the rows that need to be calculated in the current area are completed, the results of the current area are inserted to the end of the actual result data in order according to the area code. Specifically, this operation first stores the temporary results in each thread rather than inside the main thread, which distributes the loss of destructing and releasing temporary variables to each thread; secondly, the results are placed at the end of the actual result in order according to the area code. This operation utilizes the fact that run-length encoding retains the order between rows in the image, and the run-length encodings of different rows are independent of each other, thus avoiding the subsequent step of sorting the actual results. The present invention realizes the rapid implementation of image threshold segmentation with run-length encoding as the storage medium, speeds up the processing speed of extracting the area of ​​interest, increases the efficiency of using computer resources, and has practical application value in industrial visual inspection scenarios with high requirements on detection speed.

[0028] Finally: The above description is only a preferred embodiment of the present invention and is not intended to limit the present invention. Any modifications, equivalent substitutions, improvements, etc. made within the spirit and principles of the present invention should be included in the protection scope of the present invention.

Claims

1. A fast threshold segmentation method for images using run length encoding as a storage medium, characterized in that: The following steps are involved: Step 1: Set up a data partitioning module, an inter-row threshold segmentation module, and a data merging module; Step 2: The data partitioning module utilizes the characteristics that the run length encoding retains the order between rows in the image, the run length encodings of different rows are independent of each other, and the run length encodings of adjacent rows are related to each other. The image is selected as a unit, and the rows are partitioned in a continuous row number manner according to the number of threads in the existing thread pool. Each thread is responsible for data processing in each area. The areas can be calculated and processed in parallel, and the result of the current area is temporarily retained in the current thread. Step 3: The inter-row threshold segmentation module divides each row of data into 32 data as a vector and processes them separately, where every 8 data are called a group, and uses a flag bit lastu8 to record whether the last bit of data in the previous group meets the threshold segmentation condition. The data of a vector is batch processed based on the AVX instruction set and the sign bit is merged to obtain a 32-bit int value, where each bit 1 / 0 represents whether the corresponding data meets the threshold segmentation condition, and this 32-bit int value is regarded as 4 8-bit uchar values, and each uchar value selects the corresponding function in a lookup table with a fixed 256 functions to add the corresponding run encoding to the temporary result and modify the flag bit lastu8, and traverse in sequence; Step 4: The data merging module selects to insert the result of the current area into the end of the actual result data in order according to the area number after each thread has traversed all the rows that need to be calculated in the current area. The data of a vector is batch processed based on the AVX instruction set. The AVX instruction set only provides the processing of the less than operation of the char type. Therefore, according to the binary representation of the char type and the uchar type, the highest bit of the input uchar type is negated to normally apply the less than sign operation of the char type to the uchar type data. There are maximum and minimum thresholds for threshold segmentation, so it is necessary to compare the result of the input data less than the minimum threshold and the result of the maximum threshold less than the input data or the result of 0x00 or 0xff. The sign bit is merged to obtain a 32-bit int value. For a 256-bit data, the AVX instruction set provides the creation of a mask whose 32 bits correspond to the most significant bits of each byte of the 32-bit int value obtained by the sign bit merging process. The instruction is used to merge the sign bit merging operation of 32 bytes. A lookup table with 256 fixed functions is provided. In the function lookup table, the uchar value formed by merging the current sign bit is called the function number. The last lastu8 flag bit, the current row number, and the column number corresponding to the first data of the current group are used as input data to execute the function of the corresponding function number. The logic of each function is: if the highest binary bit of the function number is 0, the lastu8 sign bit is modified to be false, otherwise the lastu8 sign bit is assigned to be true; if the lowest bit is 1, it is necessary to determine whether the last lastu8 flag bit is true. If it is true, the ce value of the last run-length code in the temporary result is directly modified to be the column number of the first data with a zero starting from the lowest bit, otherwise a new run-length code of the current row number is added, the column number of the first data in the group is the start of the run, and the column number of the first data with a zero starting from the lowest bit is the end of the run. Then, observe from the low-order data to the high-order data in turn, and add the run-length codes one by one into the temporary result with continuous or single 1. When the highest binary bit of the flag bit in a group of data is 1, the column end of the run-length code last added to the group should be the column where the data at the end of the current group is located.

2. The method for fast image threshold segmentation using run length encoding as a storage medium according to claim 1, characterized in that: The described method partitions the rows in a row number continuous manner according to the number of threads in the existing thread pool. Before thread scheduling, the number of existing thread pools num, the total number of rows rows, and every=rows / num and remainder=rows%num are used to obtain the number of continuous rows each partition is responsible for and the number of remaining rows that need to be put into the main thread for calculation.

3. The method for fast image threshold segmentation using run length encoding as a storage medium according to claim 1, characterized in that: After each thread has traversed all the rows that need to be calculated in the current area, the result of the current area is inserted into the end of the actual result data in order according to the area code. After each thread calculates the final temporary result of the current area, the temporary result is placed in the final result in order according to the area code. This operation first stores the temporary result in each thread, rather than in the main thread.

Citation Information

Patent Citations

  • Printed matter defect detection method and system based on GPU parallel operation

    CN105894514A

  • Image processing method, terminal equipment and computer storage medium

    CN116579957A