Peak wavelength extraction acceleration method based on opencv parallel computing
By utilizing the OpenCV parallel computing framework and multi-threaded processing, combined with pixel grayscale value ratio thresholds and weighted operations, the shortcomings of peak wavelength extraction algorithms for line spectrum confocal sensors in terms of anti-interference and efficiency are addressed, achieving efficient and accurate peak wavelength extraction, and making it suitable for high-speed industrial measurements.
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- HUBEI CUGUANG 3D SENSING TECH CO LTD
- Filing Date
- 2025-12-31
- Publication Date
- 2026-05-08
AI Technical Summary
Existing peak wavelength extraction algorithms for line spectrum confocal sensors are insufficient to meet the demands of high-speed industrial measurements in terms of anti-interference capability and computational efficiency. The maximum value method is susceptible to noise, while the fitting method involves large computational loads and is time-consuming, making it difficult to adapt to real-time measurement scenarios.
A multi-threaded processing architecture is built using the OpenCV parallel computing framework. By identifying the maximum quantization value and column index row by row, setting the pixel grayscale value ratio threshold, performing weighted calculations, and outputting the peak wavelength position parameter, the amount of invalid pixels processed is reduced, thereby improving computational efficiency and accuracy.
It enables efficient and accurate peak wavelength extraction in industrial production, resolves the contradiction between computing speed and accuracy in traditional algorithms, meets the needs of real-time online measurement, and improves anti-interference capability and computing efficiency.
Smart Images

Figure CN121994715A_ABST
Abstract
Description
Technical Field
[0001] This invention relates to the field of spectral measurement technology, and in particular to a method for accelerating peak wavelength extraction based on OpenCV parallel computing. Background Technology
[0002] In high-speed, real-time online measurement scenarios in industrial production, the measurement accuracy of line-spectral confocal sensors is directly related to the performance of peak wavelength extraction algorithms. Stable, efficient, and fast peak wavelength extraction algorithms are crucial for improving the overall performance of measurement systems. Currently, various algorithms exist in the field of peak wavelength extraction, including the maximum value method, centroid method, square-weighted centroid method, parabolic fitting method, Gaussian fitting method, and sinc² fitting method. These algorithms are applied in different scenarios. Among them, the maximum value method has a simple computational process and low computational load; the centroid method has an intuitive principle and moderate computational load; the square-weighted centroid method, as an improvement on the centroid method, offers improved accuracy; while various fitting methods can achieve higher accuracy extraction, their computational complexity increases significantly. As industrial production demands increasing measurement efficiency, existing algorithms are increasingly unable to meet the actual needs of high-speed measurement systems in balancing computational speed and accuracy. There is an urgent need for a technical solution that can balance efficient computation and accurate extraction to meet the core requirements of real-time measurement in industrial scenarios.
[0003] Existing peak wavelength extraction algorithms for line-spectrum confocal sensors suffer from two significant drawbacks. Firstly, some algorithms lack robustness against interference. For instance, the maximum value method directly uses the wavelength corresponding to the point of maximum light intensity as the peak value, making it susceptible to noise and unable to achieve sub-pixel accuracy, thus failing to meet the requirements of high-precision measurement scenarios. Secondly, the computational efficiency of these algorithms falls short of actual industrial needs. While the square-weighted centroid method offers advantages in accuracy and efficiency, its computational speed is still insufficient to keep pace with high-speed online measurements. High-precision algorithms such as parabolic fitting and Gaussian fitting, due to their large computational load and long processing time, are difficult to apply to real-time measurement scenarios. Both of these limitations restrict the application expansion of line-spectrum confocal sensors in high-speed industrial measurement. Summary of the Invention
[0004] To overcome the shortcomings and deficiencies of existing technologies, this invention provides a peak wavelength extraction acceleration method based on OpenCV parallel computing.
[0005] The technical solution adopted in this invention is a peak wavelength extraction acceleration method based on OpenCV parallel computing, comprising the following steps: S1, using a line spectral confocal sensor to acquire images of the target object, obtaining two-dimensional grayscale image data including spectral information; S2, calling the OpenCV parallel computing framework to build a multi-threaded processing architecture, configuring thread scheduling parameters to adapt to the row pixel scale of the two-dimensional grayscale image data; S3, synchronously processing each row of pixel data of the two-dimensional grayscale image through the OpenCV parallel traversal mechanism, identifying the maximum quantization value of the pixel grayscale value in each row and the corresponding image. S4. Determine the target column interval based on the column index. This interval is centered on the column index and extends to both sides by K pixels to form a continuous column set, where K is a preset positive integer. S5. Set a pixel grayscale value ratio threshold, filter the grayscale values of each pixel in the continuous column set, and retain the pixels whose grayscale value to the maximum quantization value ratio reaches the threshold as valid pixels. S6. Perform a weighted operation on the column position information and corresponding grayscale values of all valid pixels through the OpenCV parallel computing module, and output the position parameters corresponding to the peak wavelength.
[0006] Furthermore, the pixel grayscale value ratio threshold in S5 is determined by the following model: ,in, Let be the grayscale value of the i-th pixel. Let i be the column position parameter of the i-th pixel. Use extremely small positive numbers to avoid making logarithmic operations meaningless. To adjust the parameters, These are the weighting coefficients. The pixel distribution dispersion parameter, This represents the total number of pixels in a single row and column.
[0007] Furthermore, the weighted operation in S6 adopts the following model: ,in, For the peak wavelength position parameter, Let j be the column position index of the j-th valid pixel. Let j be the grayscale value of the j-th valid pixel. Let n be the phase adjustment parameter corresponding to the j-th valid pixel, and n be the total number of valid pixels.
[0008] Furthermore, the maximum quantization value identification in S3 adopts the following model: ,in, The maximum quantization value of the pixel in the k-th row. Let be the grayscale value of the i-th pixel in the k-th row. is the row weight adjustment coefficient, k is the row index number, and m is the total number of pixels in a single row and column.
[0009] Furthermore, the boundary parameters of the target column interval in S4 are determined by the following model: floor ,in, Index the left boundary column of the target column range. The index is the right boundary column index of the target column range, and Index is the column index obtained in step S3. For the preset expansion coefficient, is the grayscale value of the corresponding row of pixels, m is the total number of pixels in a single row and column, floor is the floor function, and ceil is the floor function.
[0010] Furthermore, the peak wavelength position parameter correction model output in S6 is as follows: ,in, The corrected peak wavelength position parameters, This is the initial weighted result. Let j be the column position index of the j-th valid pixel. Let j be the grayscale value of the j-th valid pixel. The exponential decay coefficient is... The attenuation coefficient is the second-order attenuation coefficient. The value is the correction factor, and n is the total number of valid pixels.
[0011] Further, step S3 includes the following steps: S31, using the thread pool allocation mechanism of the OpenCV parallel computing framework, an independent processing thread is allocated to each row of pixel data in the 2D grayscale image, and each thread is bound to the corresponding row index and the grayscale values and column position data of all pixels in that row; S32, each independent thread compares the pixel grayscale values in the bound row one by one according to the preset pixel traversal order, and records the maximum grayscale value and the corresponding column position identifier that appears during the current traversal; S33, the processing results of all threads are collected through the thread synchronization mechanism, and a mapping relationship is established between the row index and the corresponding row maximum quantization value and column subscript index to form an intermediate data set; S34, the intermediate data set is checked for consistency, the maximum quantization value and column subscript index corresponding to abnormal row data are removed, and the results that meet the preset data validity conditions are retained.
[0012] Further, step S4 includes the following steps: S41, extracting the core column position parameter corresponding to each row from the valid column index obtained in S3, and determining the distribution range of the parameter in the column dimension of the two-dimensional grayscale image; S42, calculating the number of columns K1 to the left and the number of columns K2 to the right for each core column position parameter according to the preset expansion coefficient K, where K1 and K2 are both equal to K and are positive integers; S43, using the core column position parameter as a reference, subtracting K1 from the core column position index on the left to obtain the left boundary column index, and adding K2 to the core column position index on the right to obtain the right boundary column index; S44, defining all columns between the left boundary column index and the right boundary column index as the target column interval, and recording the target column interval range parameter corresponding to each row.
[0013] Further, step S5 includes the following steps: S51, based on the overall grayscale value distribution characteristics of the two-dimensional grayscale image, setting a fixed pixel grayscale value ratio threshold, which is a preset value greater than 0 and less than 1; S52, traversing all pixels in the target column interval corresponding to each row, obtaining the grayscale value of each pixel and the maximum quantization value of the corresponding row; S53, calculating the ratio of the grayscale value of each pixel to the maximum quantization value of the corresponding row, and comparing the ratio with the preset pixel grayscale value ratio threshold; S54, filtering out pixels with a ratio greater than or equal to the threshold, recording the column position index and grayscale value of the pixels, and forming a valid pixel dataset.
[0014] A peak wavelength extraction acceleration method based on OpenCV parallel computing is proposed. This method is implemented through different units, including: an image data acquisition unit, used to capture spectral images of the target object using a line-spectral confocal sensor, generating two-dimensional grayscale image data including pixel grayscale values and position information, and transmitting this data to a parallel computing scheduling unit; a parallel computing scheduling unit, used to receive the two-dimensional grayscale image data transmitted by the image data acquisition unit, build an OpenCV multi-threaded processing architecture, configure thread allocation rules and synchronization mechanisms, and distribute the image data to row pixel processing units; and a row pixel processing unit, used to receive the image data distributed by the parallel computing scheduling unit, and identify the maximum peak wavelength line by line using an OpenCV parallel traversal mechanism. The grayscale value and corresponding column index are processed and transmitted to the target column interval construction unit. The target column interval construction unit receives the column index transmitted by the row pixel processing unit, expands a set of K pixels around the index to form a continuous column set, and transmits the column set information to the effective pixel filtering unit. The effective pixel filtering unit receives the continuous column set information transmitted by the target column interval construction unit, filters effective pixels that meet the conditions based on a preset ratio threshold, and transmits the effective pixel data to the peak position calculation unit. The peak position calculation unit receives the effective pixel data transmitted by the effective pixel filtering unit, processes the column position information and grayscale value through parallel weighted calculation using OpenCV, and outputs the position parameter corresponding to the peak wavelength.
[0015] Beneficial Effects: This invention proposes an accelerated peak wavelength extraction method based on OpenCV parallel computing. It introduces OpenCV parallel computing technology into the peak wavelength extraction process of line-spectral confocal sensors, significantly improving computational efficiency through a multi-threaded synchronous processing architecture. This solves the problems of insufficient computational speed of traditional square-weighted algorithms and the time-consuming nature of various fitting methods, making them unsuitable for high-speed industrial measurements. This meets the core requirements of real-time online measurement in industrial production. The method employs a two-step extraction strategy. First, it quickly locates the region near the peak wavelength using the maximum value method. Then, it performs weighted calculations on pixels within a specific range before and after this region. This eliminates the need to process all pixel data, avoiding the drawbacks of the maximum value method, such as susceptibility to noise interference and inability to achieve sub-pixel accuracy measurement, while further improving computational efficiency. Simultaneously, by setting a reasonable pixel ratio threshold to filter effective pixels, the influence of outliers is eliminated, ensuring extraction accuracy. The weighting method can be flexibly adjusted to higher powers of computation, further optimizing accuracy. The overall technical solution achieves a precise balance between computing speed and extraction accuracy, which not only makes up for the contradiction between efficiency and accuracy in traditional algorithms, but also provides reliable support for the application of line spectrum confocal sensors in high-speed measurement scenarios through the adaptability design of parallel computing architecture. Attached Figure Description
[0016] Figure 1 This is a flowchart illustrating the overall process of the method of the present invention. Figure 2 This is a flowchart of method step S3 of the present invention; Figure 3 This is a flowchart of method step S4 of the present invention; Figure 4 This is a flowchart of step S5 of the method of the present invention; Figure 5 This is a diagram showing the unit composition for implementing the method of the present invention. Detailed Implementation
[0017] It should be noted that, unless otherwise specified, the embodiments and features described in this application can be combined with each other. The application will be further described in detail below with reference to the accompanying drawings and specific embodiments.
[0018] like Figure 1 As shown, a peak wavelength extraction acceleration method based on OpenCV parallel computing includes the following steps: S1, a line spectral confocal sensor is used to acquire images of the target object and obtain two-dimensional grayscale image data including spectral information; Specifically, step S1 involves acquiring an image of the target object using a line-spectral confocal sensor, obtaining two-dimensional grayscale image data including spectral information, providing a basic data source for subsequent peak wavelength extraction. In practice, the sensor parameters must first be calibrated, setting the acquisition resolution to 1024×768 pixels, the spectral response range to cover 400-1000 nanometers, and the sampling frequency to 100 frames / second, ensuring that the acquired data accurately reflects the spectral distribution characteristics of the target object. The sensor focuses on the target area through an optical lens, converting the light signal into an electrical signal. After analog-to-digital conversion, a two-dimensional grayscale image is generated. The grayscale value of each pixel in the image corresponds to the light intensity signal at that location, and the row and column coordinates of the pixels have a fixed mapping relationship with the spectral wavelength. During the acquisition process, the distance between the sensor and the target object must be kept stable at 50-100 mm, and the ambient light intensity must be controlled at 500-1000 lux to avoid data distortion caused by external light interference. Meanwhile, a continuous acquisition mode is used to acquire 10-20 frames of images. Abnormal frames with blurriness or excessive noise are removed through inter-frame consistency verification. Finally, valid frames with a clarity that meets the dynamic range of grayscale values reaching 256 levels are selected as the raw data for subsequent processing, ensuring the reliability and validity of the data and laying a high-quality data foundation for subsequent filtering and peak extraction steps.
[0019] S2 calls the OpenCV parallel computing framework to build a multi-threaded processing architecture and configures thread scheduling parameters to adapt to the row pixel scale of two-dimensional grayscale image data. Specifically, step S2 involves building an OpenCV parallel computing framework adapted for processing 2D grayscale image data, improving data processing efficiency through a multi-threaded architecture. In practice, the OpenCV parallel computing module is first invoked. The number of threads is determined based on the computer hardware configuration. If the device has 8 CPU cores, 8 processing threads are configured, each allocated independent memory space to avoid data contention. Thread scheduling parameters are set to assign tasks by row, meaning each thread is responsible for processing a fixed number of rows of image data. The thread task allocation ratio is dynamically adjusted according to the pixel size of the image rows. For a 1024×768 pixel image, each thread processes 96 rows of data, ensuring a balanced workload across threads. Simultaneously, a thread synchronization mechanism is configured, using mutex locks to prevent multiple threads from accessing shared data simultaneously. The thread communication buffer size is set to 4MB to ensure smooth data transmission between threads. During framework construction, OpenCV's hardware acceleration features need to be enabled, supporting CPU instruction set optimizations such as SSE and AVX to further improve processing speed. In addition, the framework was subjected to stability testing, with 100 consecutive data processing runs to ensure that there were no deadlocks or data loss in thread scheduling and that the framework response latency was controlled within 5 milliseconds. This provided efficient and stable computational support for the subsequent row-by-row traversal and peak extraction steps, ensuring that the overall algorithm could meet the time requirements of industrial real-time processing.
[0020] S3 synchronously processes the pixel data of each row of the two-dimensional grayscale image through the OpenCV parallel traversal mechanism, and identifies the maximum quantization value of the pixel grayscale value in the row and the column index of the corresponding pixel point. Specifically, step S3 synchronously processes the pixel data of each row of the 2D grayscale image using OpenCV's parallel traversal mechanism. The core objective is to identify the maximum quantization value of the pixel grayscale value within each row and the corresponding column index of the pixel. In practice, based on the multi-threaded framework built in step S2, each thread starts parallel traversal according to its allocated row data range. The traversal order is from the first column pixel of each row to the last column pixel, with a traversal step size of 1 to ensure no pixel is missed. During the traversal, each thread initializes the maximum quantization value to 0 and the column index to 0. It compares the current pixel's grayscale value with the recorded maximum quantization value. If the current pixel's grayscale value is greater than the recorded maximum quantization value, the maximum quantization value is updated to the current pixel's grayscale value, and the column index is simultaneously updated to the current pixel's column position. To avoid index misjudgment due to identical grayscale values, when multiple pixel grayscale values are equal to the maximum quantization value, the column index corresponding to the pixel with the smallest column coordinate is selected as the valid index for that row and column. During the traversal, each thread records the maximum quantization value and column index of each row in real time. After the traversal is completed, the processing results of all threads are aggregated to the main process through a thread synchronization mechanism, forming a mapping data table that includes the maximum quantization value, column index, and row index of each row. At the same time, the aggregated results are initially validated. If the maximum quantization value of a row is lower than the preset minimum effective grayscale threshold of 50, the data in that row is marked as abnormal and will not be processed in subsequent steps. This ensures that the extracted initial peak position is valid and provides an accurate core reference for determining the target column range in the future.
[0021] S4, determine the target column interval based on the column index, the interval is centered on the column index and extended to both sides by K pixels to form a continuous column set, where K is a preset positive integer; Specifically, step S4 determines the target column interval based on the column index obtained in step S3. This interval expands outwards from the column index to form a continuous set of columns. The core is to focus on the key area near the peak, reducing the amount of subsequent calculations. In practice, firstly, the value of the preset positive integer K is determined. Based on the image resolution and spectral wavelength accuracy requirements, the value of K is set to 5-10, with K=8 being preferred to ensure sufficient coverage of effective pixels near the peak while avoiding excessive expansion that would reduce computational efficiency. For the column index of each row, the left side is expanded by K pixels (i.e., subtracting K from the column index to obtain the left boundary column index), and the right side is expanded by K pixels (i.e., adding K to the column index to obtain the right boundary column index), forming a continuous column interval from Index-K to Index+K, which includes 2K+1 column pixels. If the left boundary column index is less than 0, the left boundary is corrected to 0; if the right boundary column index is greater than the total number of columns in the image (1023), the right boundary is corrected to 1023, ensuring that the column interval is within the effective range of the image. After determining the column intervals, the left boundary column index, right boundary column index, and center column index of each row are recorded to generate a column interval configuration table, where each row of data is associated with its corresponding column interval information. Simultaneously, the rationality of the column intervals is verified by calculating the variance of the grayscale values of pixels within the interval. If the variance is less than 10, the K value is appropriately increased to 10; if the variance is greater than 50, the K value is appropriately decreased to 5. This ensures that the column intervals cover effective pixels near the peak while excluding too many irrelevant pixels, defining a precise calculation range for subsequent effective pixel selection and weighted calculations, thus improving the overall algorithm efficiency while maintaining accuracy.
[0022] S5, set a pixel grayscale value ratio threshold, filter the grayscale values of each pixel in the continuous column set, and retain the pixels whose grayscale value ratio to the maximum quantization value reaches the threshold as valid pixels; Specifically, step S5 sets a pixel grayscale value ratio threshold to filter pixels within the target column range, retaining valid pixels to improve the accuracy of subsequent weighted calculations. In practice, the pixel grayscale value ratio threshold is first determined. Considering the image's grayscale dynamic range and noise level, the threshold is set to 0.3-0.6, with a default value of 0.45. This value has been verified through extensive experiments to effectively eliminate noise points and low-intensity invalid pixels. During the filtering process, for each pixel within each row of the target column range, the grayscale value of that pixel is first obtained, and then the maximum quantization value corresponding to that row and column in step S3 is retrieved. The ratio between the two is calculated, i.e., the current pixel grayscale value divided by the maximum quantization value. The calculated ratio is compared with the preset threshold. If the ratio is greater than or equal to the threshold, the pixel is determined to be a valid pixel, and its column position information and corresponding grayscale value are recorded. If the ratio is less than the threshold, it is determined to be an invalid pixel and is not recorded. The filtering process is executed synchronously using the OpenCV parallel computing framework. Each thread is responsible for filtering pixels in the column range of the assigned row. After filtering, the effective pixel data is stored row by row to form an effective pixel dataset. The dataset includes three key parameters: row index, column position, and grayscale value. Simultaneously, the filtering results are statistically analyzed. If a row has fewer than 3 effective pixels, the threshold for that row and column is lowered by 0.05 and the filtering is repeated. If the number of effective pixels is more than 15, the threshold is raised by 0.05 and the filtering is repeated, ensuring that the number of effective pixels in each row remains between 5 and 15. This guarantees sufficient data for subsequent weighted calculations while avoiding invalid data from interfering with the calculation results, thus improving the accuracy of peak wavelength extraction.
[0023] S6 uses the OpenCV parallel computing module to perform weighted calculations on the column position information and corresponding grayscale values of all valid pixels, and outputs the position parameters corresponding to the peak wavelength.
[0024] Specifically, step S6, using the OpenCV parallel computing module, performs a weighted calculation on the column position information and corresponding grayscale values of all effective pixels, outputting the position parameter corresponding to the peak wavelength. This is the core step for achieving accurate extraction. In practice, the specific method of weighted calculation is first clarified. Based on square weighting, it can be adjusted to triple or higher weighting according to accuracy requirements, with square weighting being preferred to ensure a balance between computational efficiency and accuracy. During the calculation, based on the OpenCV parallel computing architecture, each thread is responsible for processing the effective pixel data of the assigned row. Calculations are performed according to the weighting rules of column position information and grayscale values. That is, the column position information of each effective pixel is multiplied by the square of its own grayscale value to obtain the weighted contribution value of that pixel. Simultaneously, the squares of the grayscale values of all effective pixels are accumulated as the denominator of the weighted calculation. The weighted contribution values of all effective pixels are summed to obtain the numerator, and then the numerator is divided by the denominator to calculate the peak wavelength position parameter of that row. During the calculation, 64-bit floating-point numbers are used to ensure numerical accuracy and avoid overflow or precision loss during the calculation process. After all threads complete their respective rows of calculations, the peak wavelength position parameters of all rows are aggregated through a thread synchronization mechanism to form complete peak wavelength distribution data. Finally, the aggregated position parameters are post-processed to remove outliers exceeding the reasonable wavelength range. A moving average method is used to smooth the position parameters of five consecutive rows to reduce the impact of random noise. The final output is the peak wavelength position parameter corresponding to each row. The overall calculation latency is controlled within 20 milliseconds, meeting the needs of high-speed real-time online measurement in industrial production and ensuring the accuracy and stability of the extracted results.
[0025] Preferably, the pixel grayscale value ratio threshold in S5 is determined by the following model: ,in, Let be the grayscale value of the i-th pixel. Let i be the column position parameter of the i-th pixel. Use extremely small positive numbers to avoid making logarithmic operations meaningless. To adjust the parameters, These are the weighting coefficients. The pixel distribution dispersion parameter, This represents the total number of pixels in a single row and column.
[0026] Specifically, the pixel grayscale value ratio threshold in step S5 provides a specific calculation model. This model achieves precise threshold setting by integrating pixel grayscale values, column position parameters, and multiple adjustment parameters. This ensures that the selected effective pixels can cover key information near the peak while eliminating noise interference. During implementation, the specific value range of each parameter is first determined. The smallest positive number is set to 0.001 to avoid meaningless logarithmic calculations. The adjustment parameter is set to 0.1-0.3 to balance the influence weight of grayscale values and column position parameters. The weighting coefficient is set to 0.5-0.8 and dynamically adjusted according to the accuracy requirements of different measurement scenarios. The pixel distribution dispersion parameter is obtained by statistically analyzing the standard deviation of the grayscale values of each row of pixels in the image, with a value range of 10-30. The total number of pixels in a single row and column remains consistent with the image resolution; for example, if there are 1024 columns of pixels, this parameter is set to 1024. In the calculation process, the grayscale value of each pixel is first cubed, and then a small positive number is added to the column position parameter followed by a natural logarithm operation. The two are multiplied and summed to obtain the numerator. Next, the column position parameter is squared, and the grayscale value is squared after adding an adjustment parameter. The two are multiplied and summed to obtain the denominator. The numerator is divided by the denominator to obtain the base value, which is then added to the product of the weighting coefficient and the total number of pixels in a single row and column divided by the arctangent value of the pixel distribution dispersion parameter to finally obtain the pixel grayscale value ratio threshold. This model, through multi-parameter coordinated adjustment, allows the threshold to adapt to images with different grayscale distribution characteristics, effectively improving the targeting and accuracy of effective pixel selection, and providing a high-quality data foundation for subsequent peak wavelength extraction.
[0027] Preferably, the weighted operation in S6 adopts the following model: ,in, For the peak wavelength position parameter, Let j be the column position index of the j-th valid pixel. Let j be the grayscale value of the j-th valid pixel. Let n be the phase adjustment parameter corresponding to the j-th valid pixel, and n be the total number of valid pixels.
[0028] Specifically, the weighted calculation in step S6 provides a concrete implementation model. By introducing a phase adjustment parameter and adopting a high-power weighting method, the calculation accuracy of the peak wavelength position parameter is further improved. During implementation, all parameters must be taken according to specifications. The phase adjustment parameter is determined based on the optical characteristics of the line spectrum confocal sensor, with a value range of 0-π / 2. It is preset using the sensor's factory calibration data and can be fine-tuned according to the actual measurement environment. The total number of effective pixels is determined by the screening results in step S5, and is usually maintained at 5-15 to ensure a balance between computational efficiency and accuracy. During computation, the column position index, grayscale value, and corresponding phase adjustment parameter of each effective pixel are first obtained. The column position index is then raised to the fourth power, the grayscale value is squared, and the sine of the phase adjustment parameter is calculated. Multiplying these three values yields the numerator contribution value for each effective pixel, and summing all numerator contributions gives the total numerator. Simultaneously, the grayscale value of each effective pixel is squared, and the cosine of the phase adjustment parameter is calculated. Multiplying these two values yields the denominator contribution value for each effective pixel, and summing all denominator contributions gives the total denominator. Dividing the total numerator by the total denominator gives the peak wavelength position parameter. This model strengthens the influence of pixels near the peak by using high-power weighting of the column position index, and compensates for the phase shift caused by the optical system using the phase adjustment parameter. This makes the computation result more closely match the actual peak position, effectively improving sub-pixel-level measurement accuracy and meeting the high-precision extraction requirements of industrial scenarios.
[0029] Preferably, the maximum quantization value identification in step S3 adopts the following model: ,in, The maximum quantization value of the pixel in the k-th row. Let be the grayscale value of the i-th pixel in the k-th row. is the row weight adjustment coefficient, k is the row index number, and m is the total number of pixels in a single row and column.
[0030] Specifically, step S3 provides an optimized model for identifying the maximum quantization value. By introducing a row weight adjustment coefficient, the distribution differences of pixel grayscale values in different rows are balanced, improving the accuracy of maximum quantization value identification. In implementation, the row weight adjustment coefficient is set according to the overall grayscale distribution characteristics of the image, with a value range of 0.001-0.005. For images with generally low grayscale values, the value can be appropriately increased; for images with uniform grayscale distribution, the intermediate value of 0.003 is used. The row index number starts from 1 and increases sequentially, consistent with the actual row order of the image. The total number of pixels in a single row corresponds to the image column resolution; for example, if there are 768 columns of pixels, the value is 768. During the calculation process, each thread, while traversing the corresponding row of pixels, multiplies the grayscale value of each pixel in that row by 1 and adds the product of the row weight adjustment coefficient and the row index number to obtain the weighted grayscale value of that pixel. Then, the weighted grayscale values of all pixels in that row are compared, and the maximum value is selected as the maximum quantization value for that row. This model adaptively corrects the pixel grayscale values of different rows by adjusting the row weight coefficient, avoiding misjudgment of the maximum quantization value due to differences in illumination between the upper and lower regions of the image or inconsistent sensor sensitivity. This ensures that the maximum quantization value identified in each row can truly reflect the peak light intensity position of that row, providing an accurate core index for the subsequent determination of the target column interval.
[0031] Preferably, the boundary parameters of the target column interval in S4 are determined by the following model: floor ,in, Index the left boundary column of the target column range. The index is the right boundary column index of the target column range, and Index is the column index obtained in step S3. For the preset expansion coefficient, is the grayscale value of the corresponding row of pixels, m is the total number of pixels in a single row and column, floor is the floor function, and ceil is the floor function.
[0032] Specifically, in step S4, the boundary parameters of the target column interval provide a dynamic calculation model. By combining the grayscale value distribution characteristics of all pixels in that row, the expansion range is adjusted to make the target column interval more closely match the actual peak distribution. During implementation, the preset expansion coefficient is set to 5-10, with a default value of 8, which can be flexibly adjusted according to the measurement accuracy requirements. The total number of pixels in a single row and column is consistent with the image column resolution. The floor function and floor function are used to ensure that the boundary column index is an integer, which conforms to the discrete characteristics of pixel positions. When calculating the left boundary column index, first calculate the sum of the gray values of all pixels in the row, then calculate the sum of the squares of the gray values of all pixels in the row. Divide the two to obtain the gray distribution coefficient. Multiply the preset expansion coefficient by this coefficient to obtain the actual left expansion amount. Subtract this actual left expansion amount from the column index obtained in step S3, and then process it with the floor function to obtain the left boundary column index. When calculating the right boundary column index, first calculate the sum of the cubes of the gray values of all pixels in the row, then calculate the sum of the gray values of all pixels in the row. Divide the two to obtain the gray high-order distribution coefficient. Multiply the preset expansion coefficient by this coefficient to obtain the actual right expansion amount. Add the actual right expansion amount to the column index obtained in step S3, and then process it with the floor function to obtain the right boundary column index. The model dynamically adjusts the expansion range by using the gray-level distribution coefficient and the gray-level higher-order distribution coefficient. It appropriately reduces the expansion amount in areas where the gray-level value distribution is concentrated and appropriately increases the expansion amount in areas where the gray-level value distribution is dispersed. This ensures that the target column interval can accurately cover the effective pixels near the peak while avoiding including too many irrelevant pixels, thus achieving a balance between computational accuracy and efficiency.
[0033] Preferably, the peak wavelength position parameter correction model output in S6 is as follows: ,in, The corrected peak wavelength position parameters, This is the initial weighted result. Let j be the column position index of the j-th valid pixel. Let j be the grayscale value of the j-th valid pixel. The exponential decay coefficient is... The attenuation coefficient is the second-order attenuation coefficient. The value is the correction factor, and n is the total number of valid pixels.
[0034] Specifically, the peak wavelength position parameters output in step S6 provide a correction model. By introducing an exponential attenuation coefficient, a quadratic attenuation coefficient, and a correction coefficient, it compensates for potential systematic errors during the calculation process, further improving the extraction accuracy. In practice, the exponential attenuation coefficient is set to 0.01-0.05 to gradually weaken the influence of pixels far from the core peak; the quadratic attenuation coefficient is set to 0.001-0.003 to enhance the attenuation effect through a quadratic function, making the weight more concentrated in the core peak region; the correction coefficient is set to 0.0001-0.0005, determined based on sensor calibration data, to compensate for fixed deviations introduced by the hardware system; the total number of effective pixels is consistent with the screening result in step S5. During the correction process, the peak wavelength position parameters obtained from the initial weighted calculation in step S6 are first acquired. Then, the product of the square of the column position index of each effective pixel and its gray value is calculated, multiplied by the exponential decay value of the column index, and summed to form the numerator correction term. Simultaneously, the exponential decay value of the gray value of each effective pixel multiplied by the square of the column index is summed to form the denominator correction term. The numerator correction term is divided by the denominator correction term to obtain the correction coefficient. The initial parameters are multiplied by this correction coefficient, and then the product of the correction coefficient and the sum of the cube of the column position index of each effective pixel is added to obtain the corrected peak wavelength position parameters. This model optimizes the weight distribution through a dual exponential decay mechanism and compensates for system deviations with a fixed correction coefficient, effectively reducing the impact of computational errors and hardware errors on the results, making the output peak wavelength position parameters more accurate and meeting the stringent requirements of high-precision industrial measurements.
[0035] Preferred, such as Figure 2 As shown, step S3 includes the following steps: S31, using the thread pool allocation mechanism of the OpenCV parallel computing framework, an independent processing thread is allocated to each row of pixel data in the 2D grayscale image. Each thread is bound to the corresponding row index and the grayscale values and column position data of all pixels in that row; S32, each independent thread compares the pixel grayscale values in the bound row one by one according to the preset pixel traversal order, and records the maximum grayscale value and the corresponding column position identifier that appears during the current traversal; S33, the processing results of all threads are collected through the thread synchronization mechanism, and a mapping relationship is established between the row index and the corresponding row maximum quantization value and column subscript index to form an intermediate data set; S34, the intermediate data set is checked for consistency, the maximum quantization value and column subscript index corresponding to abnormal row data are removed, and the results that meet the preset data validity conditions are retained.
[0036] Specifically, step S3 achieves accurate extraction of the maximum quantization value of row pixels and column index based on the OpenCV parallel computing framework through a four-part process. The core is to ensure the reliability of the results through thread optimization and data verification. In implementation, step S31 first activates the thread pool allocation mechanism of the OpenCV parallel computing framework, matching the number of threads according to the total number of rows in the image and the number of CPU cores to ensure that each thread is allocated an equal amount of row data processing tasks. Simultaneously, each thread is bound to the corresponding row's pixel grayscale value, column position data, and row index to avoid data confusion between threads. In step S32, each independent thread performs a comparison operation on all pixel grayscale values within the bound row according to a fixed pixel traversal order. Initially, the recorded maximum grayscale value and column position identifier are set to initial default values. During the traversal, larger grayscale values and corresponding column position identifiers are updated in real time to ensure that peak pixel information is not missed. Step S33, through a preset thread synchronization mechanism, collects the processing results after all threads have completed the traversal, constructs a mapping table between the row index and the corresponding row's maximum quantization value and column index, and achieves orderly data integration. Step S34 performs a consistency check on the integrated intermediate data set, sets a grayscale value validity threshold, and removes the maximum quantization value and column index corresponding to abnormal row data below the threshold to ensure that the retained results meet the basic requirements for subsequent calculations and provide a reliable basis for the accurate delineation of the target column range.
[0037] Preferred, such as Figure 3 As shown, step S4 includes the following steps: S41, extracting the core column position parameter corresponding to each row from the valid column index obtained in S3, and determining the distribution range of the parameter in the column dimension of the two-dimensional grayscale image; S42, calculating the number of columns K1 to the left and the number of columns K2 to the right for each core column position parameter according to the preset expansion coefficient K, where K1 and K2 are both equal to K and are positive integers; S43, using the core column position parameter as a reference, subtracting K1 from the core column position index on the left to obtain the left boundary column index, and adding K2 to the core column position index on the right to obtain the right boundary column index; S44, defining all columns between the left boundary column index and the right boundary column index as the target column interval, and recording the target column interval range parameter corresponding to each row.
[0038] Specifically, step S4 achieves precise construction of the target column interval through a systematic process. The core is to dynamically expand a reasonable range based on the core column position parameters, balancing computational accuracy and efficiency. In implementation, step S41 first extracts the core column position parameters corresponding to each row from the valid column index output in step S3. Statistical analysis determines the distribution range of these parameters along the image column dimension, clarifying the overall distribution characteristics of the core column positions. Step S32 sets a fixed expansion coefficient based on measurement accuracy requirements and image resolution. This coefficient is a positive integer, and its value must balance the effective pixel coverage and computational load. Based on this coefficient, the number of columns to expand to the left and right of the core column position parameters is determined, ensuring symmetry in the expansion range on both sides. Step S43 uses the core column position parameters as a reference point, subtracting the set expansion column number from the left side to obtain the left boundary column index, and adding the set expansion column number to the right side to obtain the right boundary column index, thus clarifying the specific range boundaries of the target column interval. Step S44 defines all columns between the left boundary column index and the right boundary column index as the complete target column range, and records the range parameters corresponding to each row to form a range configuration list. This ensures that subsequent filtering and operations are only performed on pixels within this precise range, reducing the amount of invalid data processing.
[0039] Preferred, such as Figure 4 As shown, step S5 includes the following steps: S51, based on the overall grayscale value distribution characteristics of the two-dimensional grayscale image, a fixed pixel grayscale value ratio threshold is set, which is a preset value greater than 0 and less than 1; S52, all pixels in the target column interval corresponding to each row are traversed to obtain the grayscale value of each pixel and the maximum quantization value of the corresponding row; S53, the ratio of the grayscale value of each pixel to the maximum quantization value of the corresponding row is calculated, and the ratio is compared with the preset pixel grayscale value ratio threshold; S54, pixels with a ratio greater than or equal to the threshold are selected, and the column position index and grayscale value of the pixels are recorded to form a valid pixel dataset.
[0040] Specifically, step S5 achieves accurate screening of valid pixels through a standardized screening process. The core of this process is to eliminate invalid data based on threshold settings and ratio comparisons. In implementation, step S51 first sets a fixed pixel grayscale value ratio threshold based on the overall grayscale distribution characteristics of the image, noise level, and measurement accuracy requirements. This threshold is a specific value between 0 and 1, and has been verified through preliminary experiments to effectively distinguish between valid and noisy pixels. Step S52 starts the traversal program, extracting the grayscale value of each pixel within the target column interval corresponding to each row, and retrieving the maximum quantization value corresponding to that row and column from step S3 to ensure the accuracy of data pairing. Step S53 calculates the ratio between the grayscale value of each pixel and the maximum quantization value of the corresponding row, strictly following preset rules to perform numerical comparison operations and determine whether the ratio of each pixel meets the threshold requirements. In step S54, pixels with ratios reaching or exceeding a preset threshold are selected based on the comparison results. The column position index and grayscale value of these effective pixels are recorded in detail and organized by row to form a structured dataset of effective pixels. This provides high-quality and highly relevant data support for subsequent weighted calculations and ensures the accuracy of peak wavelength extraction.
[0041] like Figure 5 As shown, a peak wavelength extraction acceleration method based on OpenCV parallel computing is proposed. This method is implemented through different units, including: an image data acquisition unit, used to capture spectral images of the target object using a line spectral confocal sensor, generating two-dimensional grayscale image data including pixel grayscale values and position information, and transmitting this data to a parallel computing scheduling unit; a parallel computing scheduling unit, used to receive the two-dimensional grayscale image data transmitted by the image data acquisition unit, build an OpenCV multi-threaded processing architecture, configure thread allocation rules and synchronization mechanisms, and distribute the image data to row pixel processing units; and a row pixel processing unit, used to receive the image data distributed by the parallel computing scheduling unit, and identify the maximum peak wavelength line by line using an OpenCV parallel traversal mechanism. The quantized value and corresponding column index are transmitted to the target column interval construction unit. The target column interval construction unit receives the column index transmitted by the row pixel processing unit, expands a preset K pixel set around the index to form a continuous column set, and transmits the column set information to the effective pixel filtering unit. The effective pixel filtering unit receives the continuous column set information transmitted by the target column interval construction unit, filters effective pixels that meet the conditions based on a preset ratio threshold, and transmits the effective pixel data to the peak position calculation unit. The peak position calculation unit receives the effective pixel data transmitted by the effective pixel filtering unit, processes the column position information and grayscale value through OpenCV parallel weighted operation, and outputs the position parameter corresponding to the peak wavelength.
[0042] A peak wavelength extraction acceleration method based on OpenCV parallel computing is proposed. This method integrates OpenCV parallel computing technology into the peak wavelength extraction process of line spectrum confocal sensors. By leveraging a multi-threaded synchronous processing architecture, it significantly improves data processing efficiency, completely solving the pain points of insufficient computation speed of traditional square-weighted algorithms and the time-consuming nature of various fitting methods, which are unsuitable for high-speed industrial measurements. This fully meets the stringent requirements of real-time online measurement in industrial production. Furthermore, the method employs a step-by-step extraction strategy. First, it quickly locates the region near the peak wavelength using the maximum value method, and then performs weighted calculations on local pixels within this region. This eliminates the need to traverse all pixel data, further reducing computation time while ensuring accuracy. The weighting method can be flexibly adjusted to higher powers to further optimize accuracy.
[0043] This method addresses the issues of the maximum value method being susceptible to noise interference and unable to achieve sub-pixel accuracy measurement. After locking onto the region near the peak, it filters effective pixels by setting a reasonable pixel ratio threshold, eliminating the influence of outliers, and achieving high-precision extraction by combining local weighted operations. Furthermore, to address the disconnect between the computational efficiency of traditional algorithms and industrial requirements, the introduction of OpenCV's parallel computing architecture allows for multi-threaded synchronous processing of image row pixel data, significantly shortening the overall computation time. The method of focusing on local regions rather than processing the entire dataset further reduces the computational load, achieving an organic unity between high-speed computation and high-precision extraction. This provides reliable technical support for the widespread application of line-spectral confocal sensors in high-speed industrial measurement scenarios.
[0044] In the description of this invention, it should be noted that, unless otherwise explicitly specified and limited, the terms "set," "install," "connect," "link," and "fix" should be interpreted broadly. For example, they can refer to a fixed connection, a detachable connection, or an integral connection; they can refer to a mechanical connection or an electrical connection; they can refer to a direct connection or an indirect connection through an intermediate medium; and they can refer to the internal communication between two components. Those skilled in the art will understand the specific meaning of the above terms in this invention based on the specific circumstances.
[0045] Although embodiments of the invention have been shown and described, it will be understood by those skilled in the art that various equivalent changes, modifications, substitutions and alterations can be made to these embodiments without departing from the principles and spirit of the invention, the scope of which is defined by the appended claims and their equivalents.
Claims
1. A peak wavelength extraction acceleration method based on OpenCV parallel computing, characterized in that, Includes the following steps: S1. A line-spectral confocal sensor is used to acquire images of the target object, obtaining two-dimensional grayscale image data including spectral information. S2. The OpenCV parallel computing framework is used to build a multi-threaded processing architecture, configuring thread scheduling parameters to adapt to the row pixel scale of the two-dimensional grayscale image data. S3. Each row of pixel data in the two-dimensional grayscale image is processed synchronously using the OpenCV parallel traversal mechanism, identifying the maximum quantization value of the pixel grayscale value and the column index of the corresponding pixel in each row. S4. Based on the column index, a target column interval is determined. This interval is centered on the column index and extends K pixels to both sides to form a continuous column set, where K is a preset positive integer. S5. A pixel grayscale value ratio threshold is set, and the grayscale values of each pixel in the continuous column set are filtered, retaining pixels whose grayscale value to the maximum quantization value ratio reaches the threshold as valid pixels. S6. The OpenCV parallel computing module performs a weighted operation on the column position information and corresponding grayscale values of all valid pixels, outputting the position parameters corresponding to the peak wavelength.
2. The peak wavelength extraction acceleration method based on OpenCV parallel computing according to claim 1, characterized in that, The pixel grayscale value ratio threshold in S5 is determined by the following model: ,in, Let be the grayscale value of the i-th pixel. Let i be the column position parameter of the i-th pixel. Use extremely small positive numbers to avoid making logarithmic operations meaningless. To adjust the parameters, These are the weighting coefficients. The pixel distribution dispersion parameter, This represents the total number of pixels in a single row and column.
3. The peak wavelength extraction acceleration method based on OpenCV parallel computing according to claim 1, characterized in that, The weighted operation in S6 adopts the following model: ,in, For the peak wavelength position parameter, Let j be the column position index of the j-th valid pixel. Let j be the grayscale value of the j-th valid pixel. Let n be the phase adjustment parameter corresponding to the j-th valid pixel, and n be the total number of valid pixels.
4. The peak wavelength extraction acceleration method based on OpenCV parallel computing according to claim 1, characterized in that, The maximum quantization value identification in S3 adopts the following model: ,in, The maximum quantization value of the pixel in the k-th row. Let be the grayscale value of the i-th pixel in the k-th row. is the row weight adjustment coefficient, k is the row index number, and m is the total number of pixels in a single row and column.
5. The peak wavelength extraction acceleration method based on OpenCV parallel computing according to claim 1, characterized in that, The boundary parameters of the target column interval in S4 are determined by the following model: floor ,in, Index the left boundary column of the target column range. The index is the right boundary column index of the target column range, and Index is the column index obtained in step S3. For the preset expansion coefficient, is the grayscale value of the corresponding row of pixels, m is the total number of pixels in a single row and column, floor is the floor function, and ceil is the floor function.
6. The peak wavelength extraction acceleration method based on OpenCV parallel computing according to claim 1, characterized in that, The peak wavelength position parameter correction model output in S6 is as follows: ,in, The corrected peak wavelength position parameters, This is the initial weighted result. Let j be the column position index of the j-th valid pixel. Let j be the grayscale value of the j-th valid pixel. The exponential decay coefficient is... The attenuation coefficient is the second-order attenuation coefficient. is the correction factor, and n is the total number of valid pixels.
7. The peak wavelength extraction acceleration method based on OpenCV parallel computing according to claim 1, characterized in that, S3 includes the following steps: S31, using the thread pool allocation mechanism of the OpenCV parallel computing framework, an independent processing thread is allocated to each row of pixel data in the 2D grayscale image. Each thread is bound to the corresponding row index and the grayscale values and column position data of all pixels in that row; S32, each independent thread compares the grayscale values of pixels in the bound row one by one according to the preset pixel traversal order, and records the maximum grayscale value and the corresponding column position identifier that appears during the current traversal; S33, the processing results of all threads are collected through the thread synchronization mechanism, and a mapping relationship is established between the row index and the corresponding row maximum quantization value and column subscript index to form an intermediate data set; S34, the intermediate data set is checked for consistency, the maximum quantization value and column subscript index corresponding to abnormal row data are removed, and the results that meet the preset data validity conditions are retained.
8. The peak wavelength extraction acceleration method based on OpenCV parallel computing according to claim 1, characterized in that, S4 includes the following steps: S41, extracting the core column position parameter corresponding to each row from the valid column subscript index obtained in S3, and determining the distribution range of the parameter in the column dimension of the two-dimensional grayscale image; S42, based on the preset expansion coefficient K, calculate the number of columns K1 to the left and the number of columns K2 to the right for each core column position parameter, where K1 and K2 are both equal to K and are positive integers; S43, using the core column position parameter as a reference, subtract K1 from the core column position index on the left to obtain the left boundary column index, and add K2 to the core column position index on the right to obtain the right boundary column index; S44, define all columns between the left boundary column index and the right boundary column index as the target column interval, and record the target column interval range parameter corresponding to each row.
9. The peak wavelength extraction acceleration method based on OpenCV parallel computing according to claim 1, characterized in that, S5 includes the following steps: S51, based on the overall grayscale value distribution characteristics of the two-dimensional grayscale image, a fixed pixel grayscale value ratio threshold is set, which is a preset value greater than 0 and less than 1; S52, all pixels in the target column interval corresponding to each row are traversed to obtain the grayscale value of each pixel and the maximum quantization value of the corresponding row; S53, the ratio of the grayscale value of each pixel to the maximum quantization value of the corresponding row is calculated, and the ratio is compared with the preset pixel grayscale value ratio threshold; S54, pixels with a ratio greater than or equal to the threshold are selected, and the column position index and grayscale value of the pixels are recorded to form a valid pixel dataset.
10. A peak wavelength extraction acceleration method based on OpenCV parallel computing according to any one of claims 1-9, characterized in that, This method is implemented through different units, including: an image data acquisition unit, used to capture spectral images of the target object using a line-spectral confocal sensor, generate two-dimensional grayscale image data including pixel grayscale values and position information, and transmit this data to a parallel computing scheduling unit; a parallel computing scheduling unit, used to receive the two-dimensional grayscale image data transmitted by the image data acquisition unit, build an OpenCV multi-threaded processing architecture, configure thread allocation rules and synchronization mechanisms, and distribute the image data to row pixel processing units; and a row pixel processing unit, used to receive the image data distributed by the parallel computing scheduling unit, identify the maximum quantization value and corresponding column index row by row using the OpenCV parallel traversal mechanism, and process the data. The data is transmitted to the target column interval construction unit. The target column interval construction unit receives the column index transmitted by the row pixel processing unit, expands it with the index as the center by a preset K number of pixels to form a continuous column set, and transmits the column set information to the effective pixel filtering unit. The effective pixel filtering unit receives the continuous column set information transmitted by the target column interval construction unit, filters the effective pixels that meet the conditions based on a preset ratio threshold, and transmits the effective pixel data to the peak position calculation unit. The peak position calculation unit receives the effective pixel data transmitted by the effective pixel filtering unit, processes the column position information and grayscale value through OpenCV parallel weighted calculation, and outputs the position parameter corresponding to the peak wavelength.