Image processing method, terminal device, and computer storage medium
By combining run-length encoding and structuring element matrices, the image processing method is optimized, solving the problem of low efficiency in dilation and erosion operations in existing technologies, and realizing efficient binary morphological calculations.
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- ZHEJIANG HUARAY TECH CO LTD
- Filing Date
- 2023-04-06
- Publication Date
- 2026-04-24
AI Technical Summary
In existing technologies, dilation and erosion operations require multiple accesses to the neighborhood of each pixel in the image, resulting in low operational efficiency and making it difficult to meet the real-time processing requirements of large-format images.
Run-length encoding is used to process binary images. First, erosion and dilation operations are performed in the row direction, and then further processing is performed in the column direction. The efficiency is improved by using a structuring element matrix, and the calculation process is optimized by block partitioning and register processing.
It improves the efficiency of image processing, especially the binary morphological calculation of large-format images, reduces redundant calculations, and meets the real-time processing requirements of industrial applications.
Smart Images

Figure CN116579957B_ABST
Abstract
Description
Technical Field
[0001] This application relates to the field of image processing technology, and in particular to an image processing method, a terminal device, and a computer storage medium. Background Technology
[0002] Erosion is a process that eliminates boundary points, causing the boundary to shrink inward. It can be used to eliminate small and meaningless objects; dilation is a process that merges all background points in contact with an object into that object, causing the boundary to expand outward. It can be used to fill holes in an object.
[0003] Erosion and dilation are the most basic transformations in image morphology computation. The application of image morphology computation covers almost all fields of image processing. By using structuring elements with certain shapes to measure and extract corresponding shapes in images, the purpose of image analysis and recognition can be achieved.
[0004] In existing technologies, dilation and erosion operations require accessing the neighborhood of each pixel in the image and performing logical operations. Each pixel needs to be accessed multiple times, resulting in low operational efficiency. Summary of the Invention
[0005] This application provides an image processing method, a terminal device, and a computer storage medium.
[0006] One technical solution adopted in this application is to provide an image processing method, which includes:
[0007] Obtain the original binary image;
[0008] Run-length encoding is performed on the original binary image according to the row direction to obtain the run-length encoding corresponding to the original binary image;
[0009] Based on run-length encoding, the original binary image is subjected to row-direction erosion and / or dilation using a structuring element matrix to obtain the first erosion result and / or the first dilation result corresponding to the original binary image.
[0010] Based on the first corrosion result, column-direction corrosion processing is performed to obtain the second corrosion result, and / or based on the first expansion result, column-direction expansion processing is performed to obtain the second expansion result;
[0011] The second erosion result and / or the second dilation result are used as the binary morphological calculation result of the original binary image.
[0012] Specifically, run-length encoding is performed on the original binary image according to the row direction to obtain the run-length encoding corresponding to the original binary image, including:
[0013] The original binary image is divided into blocks based on its height and the number of processor cores, resulting in several binary image blocks.
[0014] Several binary image blocks are assigned to corresponding processor cores, so that the processor cores perform run-length encoding on the binary image blocks in the row direction to obtain the run-length encoding corresponding to the binary image blocks;
[0015] The run-length codes corresponding to several binary image blocks are integrated to obtain the run-length codes corresponding to the original binary image.
[0016] The process of performing run-length encoding on binary image blocks to obtain the run-length encoding corresponding to the binary image blocks includes:
[0017] Binary image block data is loaded into registers in batches, and the target row data of the currently stored binary image block is checked in the register to see if it is all 1s.
[0018] If not, obtain the start or end position in the target row data to get the run code; if yes, continue loading until 0 data appears.
[0019] Specifically, based on run-length encoding, the original binary image is subjected to row-direction erosion using a structuring element matrix to obtain the first erosion result corresponding to the original binary image, including:
[0020] Obtain the start and end positions in the target travel code;
[0021] The corrosion initiation position is determined by using the corrosion radius and the initiation position, and the corrosion termination position is determined by using the corrosion radius and the termination position, thus obtaining the corrosion path code; whereby the corrosion radius is obtained based on the structuring element matrix;
[0022] The first erosion result corresponding to the original binary image is obtained by using erosion run-length encoding to perform row-direction erosion processing.
[0023] Before performing row-direction erosion using erosion run-length encoding to obtain the first erosion result corresponding to the original binary image, the process includes:
[0024] If the corrosion start position is greater than the corrosion end position, remove the corrosion path code.
[0025] Specifically, based on run-length encoding, the original binary image is dilated in the row direction using a structuring element matrix to obtain the first dilation result corresponding to the original binary image, including:
[0026] Obtain the start and end positions in the trip code;
[0027] The expansion start position is determined by using the expansion radius and the start position, and the expansion end position is determined by using the expansion radius and the end position, thus obtaining the expansion stroke code; whereby the expansion radius is obtained based on the structuring element matrix;
[0028] Line-direction dilation is performed using dilated run-length encoding to obtain the first dilation result corresponding to the original binary image.
[0029] Before performing row-direction dilation using dilated run-length encoding to obtain the first dilation result corresponding to the original binary image, the process includes:
[0030] If the expanded stroke code overlaps with the previous stroke code of the target stroke code, the expanded stroke code and the previous stroke code will be merged.
[0031] Specifically, based on the first corrosion result, column-direction corrosion processing is performed to obtain the second corrosion result, including:
[0032] The second erosion result for the i-th row is obtained by the intersection of the first erosion results in consecutive m rows, where m is obtained based on the structuring element matrix, and i and m are positive integers.
[0033] Specifically, based on the first expansion result, column-direction expansion is performed to obtain the second expansion result, including:
[0034] The second expansion result of the i-th row is obtained by the union of the first expansion results in the m consecutive rows, where m is obtained based on the structuring element matrix, and i and m are positive integers.
[0035] Another technical solution adopted in this application is to provide a terminal device, which includes a memory and a processor connected to the memory;
[0036] The memory is used to store program data, and the processor is used to execute the program data to implement the image processing method described above.
[0037] Another technical solution adopted in this application is to provide a computer storage medium for storing program data, which, when executed by a computer, is used to implement the image processing method described above.
[0038] The beneficial effects of this application are as follows: Row-direction erosion and / or dilation processing is performed based on run-length encoding of binary images, which improves the efficiency of row-direction erosion and / or dilation processing. Column-direction erosion and / or dilation processing is performed based on the first erosion result and / or the first dilation result in the row direction, eliminating the need to perform column-direction erosion and / or dilation processing separately, thus improving the efficiency of column-direction erosion and / or dilation processing and thereby improving the efficiency of binary morphological calculations on binary images. Attached Figure Description
[0039] To more clearly illustrate the technical solutions in the embodiments of this application, the accompanying drawings used in the description of the embodiments will be briefly introduced below. Obviously, the accompanying drawings described below are only some embodiments of this application. For those skilled in the art, other drawings can be obtained based on these drawings without creative effort.
[0040] Figure 1 This is a schematic flowchart of an embodiment of the image processing method provided in this application;
[0041] Figure 2 This is a schematic diagram of an embodiment of a structure element matrix;
[0042] Figure 3 This is a schematic diagram of an embodiment of the original binary image;
[0043] Figures 4-6 Yes Figure 3 The first line is a diagram illustrating the process of running-length encoding.
[0044] Figure 7 Yes Figure 3 The diagram shown in line 4 after the expansion process has been performed;
[0045] Figure 8 This is a schematic diagram of an embodiment of the original binary image acquired by the image processing terminal;
[0046] Figures 9-12 Yes Figure 8 A schematic diagram illustrating the expansion process;
[0047] Figure 13 This is a schematic diagram of the structure of an embodiment of the terminal device provided in this application;
[0048] Figure 14 This is a schematic diagram of the structure of an embodiment of the computer storage medium provided in this application. Detailed Implementation
[0049] The technical solutions of the embodiments of this application will be clearly and completely described below with reference to the accompanying drawings. Obviously, the described embodiments are only a part of the embodiments of this application, and not all of the embodiments. Based on the embodiments of this application, all other embodiments obtained by those of ordinary skill in the art without creative effort are within the scope of protection of this application.
[0050] The reference to "embodiment" in this application means that a particular feature, structure, or characteristic described in connection with an embodiment may be included in at least one embodiment of this application. The appearance of this phrase in various places in the specification does not necessarily refer to the same embodiment, nor is it a separate or alternative embodiment mutually exclusive with other embodiments. It will be explicitly and implicitly understood by those skilled in the art that the embodiments described herein can be combined with other embodiments.
[0051] The steps in the embodiments of this application are not necessarily processed in the order described. The steps can be rearranged, deleted, or added as needed. The step descriptions in the embodiments of this application are only optional combinations of sequences and do not represent all possible combinations of steps in the embodiments of this application. The order of steps in the embodiments should not be considered as a limitation of this application.
[0052] The terms "first," "second," etc., used in this application are used to distinguish different objects, not to describe a specific order. Furthermore, the terms "comprising" and "having," and any variations thereof, are intended to cover non-exclusive inclusion. For example, a process, method, system, or apparatus that includes a series of steps or units is not limited to the listed steps or units, but may optionally include steps or units not listed, or may optionally include other steps or units inherent to these processes, methods, or apparatuses.
[0053] Furthermore, although the terms "first," "second," etc., are used repeatedly in this application to describe various data (or various applications, or various instructions, or various operations), these data (or applications, instructions, or operations) should not be limited by these terms. These terms are only used to distinguish one type of data (or application, instruction, or operation) from another type of data (or application, instruction, or operation).
[0054] In recent years, machine vision and image processing technologies have achieved certain results in the detection of surface defects. Machine vision-based defect detection methods have advantages such as fast detection speed, high recognition rate, good real-time performance, and low cost. Detecting surface defects requires morphological processing of the binary image after obtaining it to reduce noise. This allows for better extraction of surface image features through template matching or artificial neural networks, which are then matched with the obtained defect information features to identify whether the surface has defects and the type of defect. With the advent of high-resolution cameras, large-format images contain more pixel information. Traditional image morphological calculation methods are insufficient for processing large-format images in a short time. Real-time image processing is often required in practical industrial applications, placing strict demands on the algorithm's time consumption. Existing algorithms are insufficient to meet the needs of practical applications.
[0055] This application mainly designs a method to improve the morphological calculation of binary images. Unlike traditional methods, this application first performs run-length encoding on the binary image, performs morphological operations in the row direction on the run-length encoded image, and then performs morphological calculations in the column direction. This can avoid the problem of repeated or redundant calculations in traditional methods and improve the efficiency of morphological calculation of binary images.
[0056] Please refer to the details. Figure 1 , Figure 1 This is a flowchart illustrating an embodiment of the image processing method provided in this application.
[0057] like Figure 1 As shown, the image processing method in this application embodiment may specifically include the following steps:
[0058] S1, obtain the original binary image.
[0059] The image processing method in this application embodiment is executed by an image processing terminal. In some application scenarios, the image processing terminal may be a camera. In some application scenarios, the image processing terminal may be a device that establishes a communication connection with the camera, wherein the device may further include a graphics processor.
[0060] Specifically, the image processing terminal obtains the original binary image.
[0061] In some embodiments, the image processing terminal acquires a color image through a color camera connected in communication with it, and obtains an original binary image by binarizing the color image.
[0062] In some embodiments, the image processing terminal acquires a grayscale image through a grayscale camera connected in communication with it, and obtains an original binary image by binarizing the grayscale image.
[0063] S2, perform run-length encoding on the original binary image according to the row direction to obtain the run-length encoding corresponding to the original binary image.
[0064] Specifically, the image processing terminal performs run-length encoding on the original binary image according to the row direction to obtain the run-length encoding corresponding to the original binary image.
[0065] In some embodiments, in the original binary image of row y, if the starting column coordinate of a consecutive 1 pixel is xs and the ending column coordinate is xe, then the image processing terminal records the current run as run = (y, xs, xe). It is understood that multiple runs can exist in the same row of the original binary image.
[0066] The run-length encoding obtained by the image processing terminal performing run-length encoding on the original binary image in the row direction is a set of several sequences, namely (run1, run2, run3, ..., run...). n ).
[0067] S3, based on run-length encoding, uses a structuring element matrix to perform row-direction erosion and / or dilation on the original binary image to obtain the first erosion result and / or the first dilation result corresponding to the original binary image.
[0068] Specifically, the image processing terminal, based on run-length encoding, uses a structuring element matrix to perform row-direction erosion and / or dilation on the original binary image to obtain the first erosion result and / or the first dilation result corresponding to the original binary image.
[0069] The structuring element matrix is generated from the structuring element parameters. Let SE be the structuring element matrix, containing only 0 and 1 values. The number of elements with a value of 1 in each row of the structuring element matrix is counted, denoted as D = (d1, d2, ..., d...). m ), where m is the height of the structuring element matrix SE; simultaneously, the radius of the structuring element corresponding to each row is calculated, denoted as R = (r1, r2, ..., r m ),
[0070] Wherein, the following conditions are met:
[0071] r m =Floor(d m / 2)
[0072] Where Floor is the value of d m The result of / 2 is rounded down. For example, d m / 2 = 2.5, then r m =Floor(d m / 2)=2.
[0073] Please see Figure 2 , Figure 2 This is a schematic diagram of an embodiment of a structuring element matrix.
[0074] like Figure 2 As shown, the structuring element matrix SE has a circular shape with a radius of 2.5. Therefore:
[0075]
[0076]
[0077]
[0078] In some embodiments, the height of the structuring element matrix SE is m, and the image processing terminal selects m rows for row-direction erosion and / or dilation processing.
[0079] Let the original binary image be I, and SE be the structuring element matrix. The definition of erosion is... The erosion algorithm works as follows: when all pixel values of the original binary image covered by the structuring element are 1, the pixel value at the center point of the structuring element is assigned a value of 1; otherwise, it is assigned a value of 0. In other words, if any pixel value in the binary image covered by the structuring element is 0, the value at the center point of the structuring element is assigned a value of 0; otherwise, it is assigned a value of 1. If the height of SE is m, the erosion result of the i-th row of the original binary image is the result of the combined action of its m neighboring rows.
[0080] by Figure 2 Taking the structuring element matrix as an example, the structuring element matrix is circular with a radius of 2.5, and the height m of SE is 5. The erosion result of the i-th row of the original binary image is the result of the combined action of the (i-2), (i-1), (i), (i+1), and (i+2)-th rows. The erosion kernel size of each row is d. i-2 =3, d i-1 =5, d i =5, d i+1 =5, d i+2 =3, corrosion radii are respectively r i-2 =1, r i-1 =2, r i =2, r i+1 =2, r i+2 =1. If the pixel value is in the i-th row of a binary image, the run-length encoding of the i-th row of the binary image is {run(y, xs, xe) | y==i&&g i [xs, xe]==1&&i∈[i-2, i+2]}, then the row erosion result is LR i ={run(y, xs+r)} i xe-r i )|y==i&&xe-r i ≥xs+r i}
[0081] Wherein, expansion is defined as The dilation algorithm works as follows: if any pixel value in the binary image covered by the structuring element is 1, the pixel value at the center of the structuring element is assigned a value of 1; otherwise, it is assigned a value of 0. In other words, if all pixel values in the binary image covered by the structuring element are 0, the pixel value at the center of the structuring element is assigned a value of 0; otherwise, it is assigned a value of 1. If the height of SE is m, the dilation result of the i-th row of the original binary image is the result of the combined effect of its m neighboring rows.
[0082] by Figure 2Taking the structuring element matrix as an example, the structuring element matrix is circular with a radius of 2.5, and the height m of SE is 5. The dilation result of the i-th row of the binary image is the result of the combined effect of the (i-2), (i-1), (i), (i+1), and (i+2)-th rows. The dilation kernel size of each row is d. i-2 =3, d i-1 =5, d i =5, d i+1 =5, d i+2 =3, and the expansion radii are r respectively i-2 =1, r i-1 =2, r i =2, r i+1 =2, r i+2 =1. If g i Let be the pixel values of the i-th row of the original binary image. The run-length encoding of the i-th row of the binary image is {run(y, xs, xe) | y==i&&g i [xs, xe]==1&&i∈[i-2, i+2]}, then the row direction expansion results are LD respectively. i ={run(y, xs-r)} i ,xe+r i )|y==i}.
[0083] S4, perform column-direction corrosion processing based on the first corrosion result to obtain the second corrosion result, and / or perform column-direction expansion processing based on the first expansion result to obtain the second expansion result.
[0084] Specifically, the image processing terminal performs column-direction erosion and / or dilation processing based on the obtained first erosion result and / or first dilation result to obtain a second erosion result and / or a second dilation result.
[0085] S5, use the second erosion result and / or the second dilation result as the binary morphological calculation result of the original binary image.
[0086] Specifically, the image processing terminal uses the obtained second erosion result and / or second dilation result as the binary morphological calculation result of the original binary image.
[0087] In some applications, the image processing terminal uses the obtained second erosion result as the binary morphological calculation result of the original binary image.
[0088] In some application scenarios, the image processing terminal uses the obtained second dilation result as the binary morphological calculation result of the original binary image.
[0089] In some application scenarios, the image processing terminal uses the obtained second erosion result and second dilation result as the binary morphological calculation result of the original binary image.
[0090] The above scheme performs row-direction erosion and / or dilation processing based on run-length encoding of binary images, improving the efficiency of row-direction erosion and / or dilation processing. It also performs column-direction erosion and / or dilation processing based on the first erosion and / or first dilation results in the row direction, eliminating the need to perform column-direction erosion and / or dilation processing separately, thus improving the efficiency of column-direction erosion and / or dilation processing and consequently improving the efficiency of binary morphological calculations on binary images.
[0091] Another embodiment of the image processing method provided in this application may specifically include:
[0092] S11, obtain the original binary image.
[0093] S12, the original binary image is divided into blocks according to the height of the original binary image and the number of processor cores to obtain several binary image blocks.
[0094] Specifically, the image processing terminal divides the original binary image into blocks based on the height pixel values of the original binary image and the number of processor cores of the image processing terminal, resulting in several binary image blocks.
[0095] In some application scenarios, the image processing terminal may also include a graphics processing unit (GPU). The image processing terminal can divide the original binary image into blocks based on the height pixel value of the original binary image and the number of graphics processor cores of the image processing terminal, to obtain several binary image blocks.
[0096] For example, the height of the original binary image is ImgH. The image processing terminal divides the original binary image into nCores blocks according to the number of physical cores of the processor, resulting in nCores binary image blocks Block1, Block2, ..., Block. nCores The image height corresponding to each binary image block is ImgH / nCores.
[0097] like Figure 3 As shown, Figure 3 This is a schematic diagram of an embodiment of the original binary image.
[0098] Figure 3 The original binary image shown has a width of 34 pixels and a height of 4 pixels, where gray squares represent pixels with a data value of 1 and white squares represent pixels with a data value of 0.
[0099] For example, if the processor has 4 physical cores, the image processing terminal divides the original binary image into 4 blocks, that is, each row is 1 binary image block.
[0100] S13, several binary image blocks are allocated to the corresponding processor cores so that the processor cores perform run-length encoding on the binary image blocks in the row direction to obtain the run-length encoding corresponding to the binary image blocks.
[0101] Specifically, the image processing terminal allocates the obtained binary image blocks to different processor cores and performs run-length encoding operations in the row direction to obtain the run-length encoding corresponding to each binary image block. The encoding result corresponding to each binary image block is stored in different memory blocks.
[0102] For example, please refer to [the previous text]. Figure 3 Taking the first binary image block as an example, which is the first row, the image processing terminal performs run-length encoding on the first row. Specifically, the image processing terminal starts from the first column of the row and proceeds column by column until the first element with a data value of 1 appears, i.e., the 5th column. The image processing terminal records the starting position of the first run-length encoding, xs = 5, and then continues to proceed until the 29th column, recording the ending position of the first run-length encoding, xe = 29, thus obtaining the first run-length encoding (1, 5, 29). The image processing terminal continues to proceed, obtaining the starting position of the second run-length encoding, xs = 33, and the ending position, xe = 34, and records the second run-length encoding (1, 33, 34). Based on this, the run-length encoding result of the first row is obtained as run1 = {(1, 5, 29), (1, 33, 34)}. Following this pattern, we can obtain the run-code results for the second row: run2 = {(2, 9, 17), (2, 33, 33)}, the run-code results for the third row: run3 = {(3, 17, 33)}, and the run-code results for the fourth row: run4 = {(4, 11, 17), (4, 20, 24)}.
[0103] S14, integrate the run codes corresponding to several binary image blocks to obtain the run codes corresponding to the original binary image.
[0104] Specifically, the image processing terminal arranges the run-length codes corresponding to several binary image blocks stored in different memory blocks sequentially into a contiguous memory block, so that the image processing terminal can process them. Figure 3Taking the run-length encoding results run1, run2, run3, and run4 obtained after run-length encoding as an example, run1, run2, run3, and run4 are stored in different memory blocks. The image processing terminal strictly arranges run1, run2, run3, and run4 in a contiguous block of memory in the correct order, ensuring that the order of run1 to run4 is correct. Since the morphological operation results are affected by the results of the preceding and following rows, the image processing terminal needs to perform the operation on the run-length encoding of the original binary image obtained after integrating the run-length encodings of several binary image blocks. It is not possible to directly perform erosion and / or dilation operations on the run-length encodings of several binary image blocks.
[0105] S15, based on run-length encoding, the original binary image is subjected to row-direction erosion and / or dilation using a structuring element matrix to obtain the first erosion result and / or the first dilation result corresponding to the original binary image.
[0106] S16, perform column-direction corrosion processing based on the first corrosion result to obtain a second corrosion result, and / or perform column-direction expansion processing based on the first expansion result to obtain a second expansion result.
[0107] S17, the second erosion result and / or the second dilation result are used as the binary morphological calculation result of the original binary image.
[0108] The above scheme performs row-direction erosion and / or dilation based on run-length encoding of the binary image, improving the efficiency of row-direction erosion and / or dilation. Furthermore, it performs column-direction erosion and / or dilation based on the first erosion and / or dilation result in the row direction, eliminating the need for separate column-direction erosion and / or dilation, thus improving the efficiency of column-direction erosion and / or dilation, and consequently improving the efficiency of binary morphological calculations on the binary image. Further, before performing run-length encoding, the binary image is divided into several binary image blocks according to the number of processor cores, and each binary image block is assigned to a different processor core for run-length encoding processing, further improving the processing efficiency of run-length encoding.
[0109] Another embodiment of the image processing method provided in this application may specifically include:
[0110] S21, Obtain the original binary image.
[0111] S22, the original binary image is divided into blocks according to the height of the original binary image and the number of processor cores to obtain several binary image blocks.
[0112] S23, several binary image blocks are allocated to the corresponding processor cores so that the processor cores perform run-length encoding on the binary image blocks in the row direction to obtain the run-length encoding corresponding to the binary image blocks.
[0113] S24: Load the binary image block data into the register in batches, row by row, and determine in the register whether the target row data of the currently stored binary image block is all 1s.
[0114] Specifically, the image processing terminal loads binary image blocks line by line into the SIMD register according to the size of the SIMD (Single Instruction Multiple Data) register, and uses SIMD instructions in the SIMD register to determine whether the target row data of the currently stored binary image block is all 1s.
[0115] If the judgment result is yes, proceed to S25. If the judgment result is no, proceed to S26.
[0116] Optionally, SIMD instructions may include SSE (Streaming SIMD Extensions) instructions and AVX2 instructions (Advanced Vector Extensions), without limitation here.
[0117] In some embodiments, the image processing terminal loads binary image blocks into the XMM register in batches, line by line, using the SSE instruction. Within the XMM register, it is determined whether the target row data of the currently stored binary image block is all 1s. The XMM register is 128 bits in size, allowing the image processing terminal to load 16 bytes of binary image block data at a time.
[0118] In some embodiments, the image processing terminal loads binary image blocks into the YMM register in batches, line by line, using the AVX2 instruction. Within the YMM register, it is determined whether the target row data of the currently stored binary image block is all 1s. The YMM register is 256 bits in size, allowing the image processing terminal to load 32 bytes of binary image block data at a time.
[0119] S25, if so, continue loading until 0 data appears.
[0120] If the judgment result is yes, the image processing terminal loads the row data of the next set of binary image blocks and judges again. This continues until it is determined that there is a set of binary image blocks containing data with a value of 0.
[0121] S26, if not, obtain the start or end position in the target row data to get the run code.
[0122] Specifically, if the target row data of the currently loaded binary image block is not all 1s, the image processing terminal obtains the start or end position of the data with 0s in the target row data and obtains at least one run code for that row.
[0123] For detailed execution steps of S22-S26, please refer to [link / reference]. Figures 4-6 , Figures 4-6 Yes Figure 3 The first line is a diagram illustrating the process of running code processing.
[0124] Image processing terminals can use SSE or AVX2 instructions to perform run-length encoding on loaded binary image blocks. If the image processing terminal uses SSE instructions for run-length encoding, since each pixel in the binary image block occupies 8 bits of register space, and the xmm register corresponding to the SSE instruction is 128 bits long, one SSE instruction can process 16 columns of data. If the image processing terminal uses AVX2 instructions for run-length encoding, since the ymm register corresponding to the AVX2 instruction is 256 bits long, one SSE instruction can process 32 columns of data.
[0125] like Figure 4 As shown, the image processing terminal uses the SSE instruction to perform run-length encoding on the binary image block loaded in the first row.
[0126] Specifically, the image processing terminal loads the first 16 columns of data from the first row into the xmm register, that is, it loads the data from columns 1 to 16 into the xmm register, such as... Figure 4 As shown, the image processing terminal uses the SSE instruction to determine if all data in the register is 1. Understandably, the data currently loaded in the xmm register is not all 1s. Therefore, the image processing terminal further determines the location of the first 1, obtaining xs = 5.
[0127] Furthermore, the image processing terminal loads the data from column 6 to column 21 into the xmm register, starting from column 6. Figure 5 As shown. The image processing terminal then uses the SSE instruction to determine whether all the data in the xmm register is currently 1. Understandably, all the data currently loaded in the xmm register is 1, so the image processing terminal continues to load the next set of data.
[0128] Furthermore, the image processing terminal loads the data from column 22 to column 37 into the xmm register, starting from column 22. Figure 6As shown. Since the image width is 34 pixels, the data in columns 35 to 37 loaded into the xmm register are directly set to 0. At this time, there is data containing 0 in the xmm register. The image processing terminal determines that the 8th bit is the first termination position. Combining the loading process, we know that xe = 5 + 16 + 8 = 29. By analogy, the run code of the first row, run1 = {(1, 5, 29), (1, 33, 34)}, is obtained.
[0129] S27. Based on run-length encoding, the original binary image is subjected to row-direction erosion and / or dilation using a structuring element matrix to obtain the first erosion result and / or the first dilation result corresponding to the original binary image.
[0130] S28, perform column-direction corrosion processing based on the first corrosion result to obtain a second corrosion result, and / or perform column-direction expansion processing based on the first expansion result to obtain a second expansion result.
[0131] S29, the second erosion result and / or the second dilation result are used as the binary morphological calculation result of the original binary image.
[0132] The above scheme performs row-direction erosion and / or dilation based on run-length encoding of the binary image, improving the efficiency of row-direction erosion and / or dilation. Furthermore, it performs column-direction erosion and / or dilation based on the first erosion and / or dilation result in the row direction, eliminating the need for separate column-direction erosion and / or dilation, thus improving the efficiency of column-direction erosion and / or dilation, and consequently improving the efficiency of binary morphological calculations on the binary image. Further, the calculation method of run-length encoding is optimized, improving the processing efficiency of run-length encoding.
[0133] Another embodiment of the image processing method provided in this application may specifically include:
[0134] S31, Obtain the original binary image.
[0135] S32, perform run-length encoding on the original binary image according to the row direction to obtain the run-length encoding corresponding to the original binary image.
[0136] S33, obtain the start and end positions in the target travel code.
[0137] Specifically, the image processing terminal obtains the start and end positions of the run-code for each line, where each line of run-code may have multiple start and end positions.
[0138] S34 uses the corrosion radius and the starting position to determine the corrosion start position, and uses the corrosion radius and the ending position to determine the corrosion end position, thus obtaining the corrosion path code.
[0139] The corrosion radius is obtained based on the structural element matrix.
[0140] For example, using d1, d2, ..., d m Perform row-direction erosion on rows im / 2, ..., i, ..., i+m / 2 respectively, and denote the erosion result of each row as LR. i-m / 2 , ..., LR i , ..., LR i+m / 2 After each row of data in the original binary image is encoded by run-length encoding, the row-direction erosion only needs to consider the start and end positions of each run. If the current run is (y, xs, xe) and the corresponding erosion radius is r, then the erosion encoding of the current run is (y, xs+r, xe-r).
[0141] S35, if the corrosion start position is greater than the corrosion end position, remove the corrosion stroke code.
[0142] Specifically, if the erosion start position in a certain erosion stroke code is greater than the erosion end position, the image processing terminal removes that erosion stroke code.
[0143] For example, for a certain erosion path encoding (y, xs+r, xe-r), if the following conditions are met:
[0144] xs+r>xe-r,
[0145] The image processing terminal then removes the erosion run code.
[0146] S36. Using erosion run-length encoding, row-direction erosion is performed to obtain the first erosion result corresponding to the original binary image.
[0147] S37 performs column-direction corrosion processing based on the first corrosion result to obtain the second corrosion result.
[0148] Understandably, if the erosion operation of row i is the result of the combined action of rows im / 2, ..., i, ..., i+m / 2, then the erosion operation of row i+1 is the result of the combined action of rows im / 2+1, ..., i, ..., i+m / 2+1. Therefore, when iterating from row i to row i+1, some results calculated in row i can be reused. For example, when calculating the erosion result of row i, row-direction erosion operation needs to be performed on row im / 2+1, with a corresponding erosion kernel size of d2. Similarly, when calculating the erosion result of row i+1, row-direction erosion operation needs to be performed on row im / 2+1, with a corresponding erosion kernel size of d1. Therefore, if d1 and d2 are equal, the row-direction erosion operation for that row does not need to be recalculated, and so on. Finally, erosion operation is performed along the column direction to obtain the erosion result of row i+1. The erosion result of row i+1 is...
[0149] S38, the second erosion result is used as the binary morphological calculation result of the original binary image.
[0150] The above scheme performs row-direction erosion and / or dilation processing based on run-length encoding of binary images, improving the efficiency of row-direction erosion and / or dilation processing. It also performs column-direction erosion and / or dilation processing based on the first erosion and / or first dilation results in the row direction, eliminating the need to perform column-direction erosion and / or dilation processing separately, thus improving the efficiency of column-direction erosion and / or dilation processing and consequently improving the efficiency of binary morphological calculations on binary images.
[0151] Another embodiment of the image processing method provided in this application may specifically include:
[0152] S41, Obtain the original binary image.
[0153] S42, perform run-length encoding on the original binary image according to the row direction to obtain the run-length encoding corresponding to the original binary image.
[0154] S43, obtain the start and end positions in the target travel code.
[0155] S44, the expansion start position is determined using the expansion radius and the start position, and the expansion end position is determined using the expansion radius and the end position, thereby obtaining the expansion stroke code.
[0156] The expansion radius is obtained based on the structuring element matrix.
[0157] For example, using the structuring element matrix d1, d2, ..., d m Perform row-wise dilation on rows im / 2, ..., i, ..., i+m / 2 respectively, and denote the dilation result of each row as LD. i-m / 2 , ...,LD i , ...,LD i+m / 2 After each row of data in a binary image is encoded by run-length encoding, the row-direction dilation only needs to consider the start and end positions of each run. If the run is (y, xs, xe) and the dilation radius is r, then the dilation result of the run is (y, xs-r, xe+r).
[0158] S45, if the expanded stroke code overlaps with the previous stroke code of the target stroke code, the expanded stroke code and the previous stroke code are merged.
[0159] In some embodiments, if the current dilation stroke code overlaps with the previous dilation stroke code in the same row, such as the dilation termination position of the previous dilation stroke code being greater than the start position of the current dilation stroke code, the image processing terminal will merge the current dilation stroke code with the previous dilation stroke code.
[0160] by Figure 3 Taking line 4 as an example, assuming the morphological kernel size is 3, the dilation radius is 1. The run-length encoding of line 4 is run4 = {(4, 11, 17), (4, 20, 24)), and the dilation result is {(4, 11-1, 17+1), (4, 20-1, 24+1)), which is {(4, 10, 18), (4, 19, 25)}. The dilation result of line 4 is as follows. Figure 7 As shown, Figure 7 Yes Figure 3 The diagram shown in line 4 after the expansion process has been performed.
[0161] Since the expanded strokes are already connected, they can be merged into a single stroke, resulting in the stroke {(4, 10, 25)}. Therefore, strokes that are expanded along the row direction and are connected need to be merged. Understandably, if the morphological kernel is particularly large, strokes that are not adjacent in the same row may also become connected or overlap after expansion, in which case they also need to be merged.
[0162] S46, using the dilation run-length encoding, perform row-direction dilation processing to obtain the first dilation result corresponding to the original binary image.
[0163] S47 performs column-direction expansion processing based on the first expansion result to obtain the second expansion result.
[0164] Understandably, if the morphological operation of row i is the result of the combined action of rows im / 2, ..., i, ..., i+m / 2, then the morphological operation of row i+1 is the result of the combined action of rows im / 2+1, ..., i, ..., i+m / 2+1. Therefore, when iterating from row i to row i+1, some results calculated in row i can be reused. For example, when calculating the morphological operation result of row i, row-direction morphological operations need to be performed on row im / 2+1, with a corresponding morphological kernel size of d2. Similarly, when calculating the morphological operation result of row i+1, row-direction morphological operations need to be performed on row im / 2+1, with a corresponding morphological kernel size of d1. Therefore, if d1 and d2 are equal, the row-direction morphological operations for that row do not need to be recalculated, and so on. Finally, morphological operations are performed along the column direction to obtain the morphological operation result of row i+1. The dilation result of row i+1 is...
[0165] S48, the second dilation result is used as the binary morphological calculation result of the original binary image.
[0166] The above scheme performs row-direction erosion and / or dilation processing based on run-length encoding of binary images, improving the efficiency of row-direction erosion and / or dilation processing. It also performs column-direction erosion and / or dilation processing based on the first erosion and / or first dilation results in the row direction, eliminating the need to perform column-direction erosion and / or dilation processing separately, thus improving the efficiency of column-direction erosion and / or dilation processing and consequently improving the efficiency of binary morphological calculations on binary images.
[0167] An embodiment of the image processing method proposed in this application may specifically include:
[0168] Please refer to the previous document. Figure 2 , Figure 2 The structuring element matrix shown has a radius of 2.5. The morphological kernel sizes of each row are d1=3, d2=5, d3=5, d4=5, d5=3, and the morphological kernel radii are r1=1, r2=2, r3=2, r4=2, r5=1.
[0169] Please see Figure 8 , Figure 8 This is a schematic diagram of an embodiment of the original binary image acquired by the image processing terminal.
[0170] like Figure 8 As shown, the original binary image acquired by the image processing terminal contains a foreground target with an outer rectangle of size 10x10. The gray areas correspond to pixels with a value of 1, and the white areas correspond to pixels with a value of 0.
[0171] Specifically, the image processing terminal for Figure 8 The original binary image is run-length encoded along the row direction, resulting in the run-length encodings for each row as follows:
[0172]
[0173]
[0174]
[0175] run4={(4, 4, 7), (4, 13, 13)),
[0176] run5={(5, 4, 7), (5, 10, 12)),
[0177] run6={(6, 4, 5), (6, 10, 12)),
[0178] run7={(7, 4, 5), (7, 10, 12)),
[0179] run8={(8,4,5),(8,9,10)},
[0180] run9={(9, 4, 5), (9, 9, 10)),
[0181] run 10 ={(10, 4, 10))
[0182] run 11 ={(11, 4, 13)},
[0183] run 12 ={(12,4,13)},
[0184] run 13 ={(13,7,13)},
[0185]
[0186]
[0187]
[0188] Based on the structural element dimensions, the minimum outer rectangle size of the expanded target is 14×14. Therefore, the expanded strokes LD2,…,LD need to be calculated row by row. 15 The calculation process of LD2 is as follows: First, row-direction dilation is performed on five consecutive rows (run0, run1, run2, run3, and run4) with row-direction dilation kernel sizes of d1=3, d2=5, d3=5, d4=5, and d5=3, respectively. Then, the results of the row-direction dilation are subjected to union operation, specifically:
[0189]
[0190] Since run0, run1, run2, and run3 are empty sets, and:
[0191]
[0192] Finally, adjusting the y-coordinate in the run-length encoding yields LD2 = {(2,3,8),(2,12,14)}. Please refer to [link / reference needed]. Figure 9 , Figure 9 for Figure 8 This is a diagram showing the result of performing dilation up to row 2. The light gray area represents the region where the newly generated value is 1 after dilation.
[0193] By analogy, we can obtain:
[0194]
[0195] And:
[0196]
[0197]
[0198] That is, LD3 = {(3,2,15)}.
[0199] Please see Figure 10 , Figure 10 for Figure 8 This is a diagram showing the result of performing dilation up to row 3. The light gray area represents the region where the newly generated value is 1 after dilation.
[0200] Similarly, we can conclude that:
[0201]
[0202] Since run2 and run3 are empty sets, therefore:
[0203]
[0204] Since d3 = d4, therefore at this time No need to recalculate, directly use the values from the LD3 calculation. The calculation result is as follows:
[0205]
[0206]
[0207]
[0208] The final result is LD4 = {(4,2,15)}. Figure 11 As shown, Figure 11 for Figure 8 This is a diagram showing the result of performing dilation up to line 4. The light gray area represents the region where the newly generated value is 1 after dilation.
[0209] This process is repeated line by line to obtain the final expanded result, as shown below. Figure 12 As shown. Figure 12 yes Figure 8 A schematic diagram of the final result after performing dilation. The light gray area represents the region where the newly generated value is 1 after dilation.
[0210] The above scheme performs row-direction erosion and / or dilation processing based on run-length encoding of binary images, improving the efficiency of row-direction erosion and / or dilation processing. It also performs column-direction erosion and / or dilation processing based on the first erosion and / or first dilation results in the row direction, eliminating the need to perform column-direction erosion and / or dilation processing separately, thus improving the efficiency of column-direction erosion and / or dilation processing and consequently improving the efficiency of binary morphological calculations on binary images.
[0211] Another embodiment of the image processing method provided in this application may specifically include:
[0212] S51, obtain the original binary image.
[0213] S52, perform run-length encoding on the original binary image according to the row direction to obtain the run-length encoding corresponding to the original binary image.
[0214] S53, based on run-length encoding, uses a structuring element matrix to perform row-direction erosion and / or dilation on the original binary image to obtain the first erosion result and / or the first dilation result corresponding to the original binary image.
[0215] S54, for the second corrosion result of the i-th row, it is obtained based on the intersection of the first corrosion results in the continuous m-row directions.
[0216] Where m is obtained based on the structure element matrix, and i and m are positive integers.
[0217] Specifically, the image processing terminal performs column-direction erosion operation on the first erosion result by calculating the intersection of m consecutive rows of the first erosion result to obtain the first erosion result, where m is the height of the structuring element matrix.
[0218] For example, the corrosion result of the i-th row is:
[0219]
[0220] by Figure 2 Taking the corresponding structuring element matrix SE as an example, the erosion result of the i-th row is:
[0221]
[0222] S55, for the second expansion result of the i-th row, it is obtained by the union of the first expansion results in the continuous m-row directions.
[0223] Where m is obtained based on the structure element matrix, and i and m are positive integers.
[0224] Specifically, the image processing terminal performs column-direction dilation operation on the first dilation result by calculating the union of m consecutive rows of the first dilation result to obtain the second dilation result, where m is the height of the structuring element matrix.
[0225] For example, the expansion result of the i-th row is:
[0226]
[0227] by Figure 2 Taking the corresponding structuring element matrix SE as an example, the expansion result of the i-th row is:
[0228]
[0229] S56, the second erosion result and / or the second dilation result are used as the binary morphological calculation result of the original binary image.
[0230] The above scheme performs row-direction erosion and / or dilation based on run-length encoding of the binary image, improving the efficiency of row-direction erosion and / or dilation. It then performs column-direction erosion and / or dilation based on the first row-direction erosion and / or first dilation result, eliminating the need for separate column-direction erosion and / or dilation, thus improving the efficiency of column-direction erosion and / or dilation, and consequently improving the efficiency of binary morphological calculations on the binary image. Furthermore, after performing row-direction morphological operations on the binary image, intersection and / or union operations are performed based on the first erosion and / or first dilation result to obtain the final morphological result, further improving the efficiency of morphological calculations.
[0231] Please continue reading Figure 13 , Figure 13 This is a schematic diagram of the structure of an embodiment of the terminal device provided in this application. The terminal device 500 of this application embodiment includes a processor 51 and a memory 52.
[0232] The processor 51 and the memory 52 are connected to a bus. The memory 52 stores program data, and the processor 51 is used to execute the program data to implement the image processing method described in the above embodiments.
[0233] In this embodiment, processor 51 can also be referred to as a CPU (Central Processing Unit). Processor 51 may be an integrated circuit chip with signal processing capabilities. Processor 51 can also be a general-purpose processor, a digital signal processor (DSP), an application-specific integrated circuit (ASIC), a field-programmable gate array (FPGA), or other programmable logic devices, discrete gate or transistor logic devices, or discrete hardware components. The general-purpose processor can be a microprocessor, or processor 51 can be any conventional processor.
[0234] This application also provides a computer storage medium; please refer to the following: Figure 14 , Figure 14 This is a schematic diagram of a computer storage medium according to an embodiment of the present application. The computer storage medium 600 stores program data 61, which is used to implement the image processing method of the above embodiment when executed by the processor.
[0235] When the embodiments of this application are implemented as software functional units and sold or used as independent products, they can be stored in a computer-readable storage medium. Based on this understanding, the technical solution of this application, in essence, or the part that contributes to the prior art, or all or part of the technical solution, can be embodied in the form of a software product. This computer software product is stored in a storage medium and includes several instructions to cause a computer device (which may be a personal computer, server, or network device, etc.) or processor to execute all or part of the steps of the methods described in the various embodiments of this application. The aforementioned storage medium includes various media capable of storing program code, such as USB flash drives, portable hard drives, read-only memory (ROM), random access memory (RAM), magnetic disks, or optical disks.
[0236] The above description is merely an embodiment of this application and does not limit the patent scope of this application. Equivalent structural or procedural transformations made using the content of this application's specification and drawings, or direct or indirect applications in other related technical fields, are similarly included within the patent protection scope of this application.
Claims
1. An image processing method, characterized in that, The method includes: Obtain the original binary image; Run-length encoding is performed on the original binary image according to the row direction to obtain the run-length encoding corresponding to the original binary image; Calculate the radius of the structural element corresponding to each row in the structural element matrix; Based on the run-length encoding, the original binary image is subjected to row-direction erosion and / or dilation using the structuring element matrix to obtain the first erosion result and / or first dilation result corresponding to the original binary image; Based on the first corrosion result, column-direction corrosion processing is performed to obtain a second corrosion result, and / or based on the first expansion result, column-direction expansion processing is performed to obtain a second expansion result; The second erosion result and / or the second dilation result are used as the binary morphological calculation result of the original binary image; The step of performing row-direction erosion and / or dilation on the original binary image based on the run-length encoding and using the structuring element matrix includes: Using the first and second structuring element rows in the structuring element matrix, the same target row in the original binary image is processed for row direction; In response to the fact that the radius of the structural element corresponding to the first structural element row is equal to the radius of the structural element corresponding to the second structural element row, the row direction processing result of the second structural element row processing the target row is directly used as the row direction processing result of the first structural element row processing the target row.
2. The method according to claim 1, characterized in that, The step of performing run-length encoding on the original binary image according to the row direction to obtain the run-length encoding corresponding to the original binary image includes: The original binary image is divided into blocks based on its height and the number of processor cores to obtain several binary image blocks; The binary image blocks are assigned to corresponding processor cores, so that the processor cores perform run-length encoding on the binary image blocks in the row direction to obtain the run-length encoding corresponding to the binary image blocks; The run-length codes corresponding to the aforementioned binary image blocks are integrated to obtain the run-length codes corresponding to the original binary image.
3. The method according to claim 2, characterized in that, The step of performing run-length encoding on the binary image block to obtain the run-length encoding corresponding to the binary image block includes: Binary image block data is loaded into the register in batches, row by row, and the target row data of the currently stored binary image block is determined in the register to be all 1s. If not, obtain the start or end position in the target row data to get the run code; if yes, continue loading until 0 data appears.
4. The method according to claim 1, characterized in that, Based on the run-length encoding, the original binary image is subjected to row-direction erosion processing using a structuring element matrix to obtain a first erosion result corresponding to the original binary image, including: Obtain the start and end positions in the target travel code; The corrosion start position is determined using the corrosion radius and the start position, and the corrosion end position is determined using the corrosion radius and the end position, thereby obtaining the corrosion path code; wherein, the corrosion radius is obtained based on the structuring element matrix; The erosion run-length encoding is used to perform row-direction erosion processing to obtain the first erosion result corresponding to the original binary image.
5. The method according to claim 4, characterized in that, Before performing row-direction erosion processing using the erosion run-length encoding to obtain the first erosion result corresponding to the original binary image, the process includes: If the corrosion start position is greater than the corrosion end position, remove the corrosion path code.
6. The method according to claim 1, characterized in that, The process of performing row-direction dilation on the original binary image based on the run-length encoding, using a structuring element matrix to obtain a first dilation result corresponding to the original binary image, includes: Obtain the start and end positions from the travel code; The expansion start position is determined using the expansion radius and the start position, and the expansion end position is determined using the expansion radius and the end position, thereby obtaining the expansion stroke code; wherein, the expansion radius is obtained based on the structure element matrix; The line-direction dilation process is performed using the dilation run-length encoding to obtain the first dilation result corresponding to the original binary image.
7. The method according to claim 6, characterized in that, Before performing row-direction dilation processing using the dilated run-length encoding to obtain the first dilation result corresponding to the original binary image, the process includes: If the expanded stroke code overlaps with the previous stroke code of the target stroke code, the expanded stroke code and the previous stroke code are merged.
8. The method according to claim 1, characterized in that, The process of performing column-direction corrosion based on the first corrosion result to obtain a second corrosion result includes: The second erosion result for the i-th row is obtained by the intersection of the first erosion results in consecutive m rows, where m is obtained based on the structuring element matrix, and i and m are positive integers.
9. The method according to claim 1, characterized in that, The process of performing column-direction dilation based on the first dilation result to obtain a second dilation result includes: The second expansion result of the i-th row is obtained by the union of the first expansion results in consecutive m rows, where m is obtained based on the structuring element matrix, and i and m are positive integers.
10. A terminal device, characterized in that, The terminal device includes a processor and a memory connected to the processor, wherein... The memory stores program instructions; The processor is configured to execute program instructions stored in the memory to implement the method as described in any one of claims 1 to 9.
11. A computer-readable storage medium, characterized in that, The storage medium stores program instructions that, when executed, implement the method as described in any one of claims 1 to 9.
Citation Information
Patent Citations
Run-length encoding-based quick region expansion algorithm and apparatus
CN106846424A
Fast region corrosion algorithm based on run length encoding and device
CN106875453A
Methods and systems for improved image encoding
CN112385225A