An image correction method based on Python interpolation

CN115375585BActive Publication Date: 2025-07-29NANTONG JIAJUN INFORMATION TECH CO LTD
View PDF 2 Cites 0 Cited by

Patent Information

Application Number
CN202211129926.1
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2022-09-16
Publication Date
2025-07-29
Estimated Expiration
2042-09-16

AI Technical Summary

Technical Problem

网格插值算法是一种常用的图像处理技术,不同的编程语言会有不同的实现方式,支持的程度也不同,比如matlab的网格插值算法既支持外插值又支持内插值,而python的网格插值算法仅仅支持内插值,对于FOV(视场角)特别大的摄像头,我们常常需要去除图像边缘角落,此时将边缘的数据设置为NAN,对于这种情况,当我们将matlab程序的运行结果和相应的python程序的运行结果比较,如果算法用到网格插值,两者的结果有可能会有很大差异,由于python对网格外插值算法和处理NAN不够完善,导致结果不是很准

Benefits of technology

[0013] The present invention makes up for the defect that griddata in python does not support the external interpolation algorithm. The implemented interpolation result is very close to the result of griddedInterpolant in matlab and can be used as an implementation solution for external interpolation required in python. The present invention takes into account both speed and memory. If the RBF interpolation algorithm is used entirely, although it supports external interpolation, the speed is extremely slow and the required memory is very large.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN115375585B_ABST
    Figure CN115375585B_ABST
Patent Text Reader

Abstract

The present invention provides an image correction method based on Python interpolation, including: (1) sampling the original image, screening out non-NAN positions from the image, and using the griddata interpolation algorithm in Python to interpolate the non-NAN region formed by the non-NAN positions; (2) screening out NAN positions from the data after interpolation in step (1), and using the RBF interpolation algorithm in Python to interpolate the NAN region formed by the NAN positions; (3) screening out NAN positions from the data after two interpolations, constructing a correction matrix and performing shadow correction on the original image. The present invention makes up for the defect that griddata in Python does not support the external interpolation algorithm, and the obtained interpolation result is very close to the result of griddedInterpolant in Matlab, which can be used as an implementation solution for external interpolation when using Python.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention belongs to the technical field of image processing, and particularly relates to an image correction method based on Python interpolation. Background Art

[0002] There are shadows in the edge part of the image captured by the camera. Usually, it is necessary to correct the shadow part so that the edge part can be as bright as other parts as much as possible. In this process, an interpolation algorithm needs to be used. The grid interpolation algorithm is a commonly used image processing technology. Different programming languages have different implementation methods and different degrees of support. For example, the grid interpolation algorithm in Matlab supports both external interpolation and internal interpolation, while the grid interpolation algorithm in Python only supports internal interpolation. For cameras with a particularly large FOV (field of view), we often need to remove the corners of the image edge. At this time, the edge data is set to NAN. For this situation, when we compare the running results of the Matlab program and the corresponding Python program, if the algorithm uses grid interpolation, the results of the two may be very different. Because Python is not perfect enough for grid external interpolation algorithm and handling NAN, the result is not very accurate. Summary of the Invention

[0003] The present invention provides an image correction method based on Python interpolation, which makes up for the defect that griddata in Python does not support external interpolation.

[0004] The image correction method based on Python interpolation provided by the present invention includes:

[0005] (1) Sampling the original image, screening out non-NAN positions from the image, and using the griddata interpolation algorithm in Python to interpolate the non-NAN area formed by the non-NAN positions;

[0006] (2) Screening out NAN positions from the data interpolated in step (1), and using the RBF interpolation algorithm in Python to interpolate the NAN area formed by the NAN positions;

[0007] (3) Screening out NAN positions from the data after two interpolations, and setting the data at the NAN positions to NAN; constructing a correction matrix according to the data obtained after the second interpolation, and performing shadow correction on the original image according to the correction matrix. The data in the correction matrix being NAN means that there is no need to perform shadow correction on the corresponding position of the original image.

[0008] Preferably, the step (1) is specifically as follows: downsampling the original image according to blocks and taking the average value to obtain a thumbnail, then dividing each pixel value in the thumbnail by the maximum pixel value in the thumbnail to obtain the basic data shading of the block; filtering out non-NAN positions from the basic data shading, and establishing an interpolation function z2=f(axisx2, axisy2), wherein z2 is the pixel value of the non-NAN position, and axisx2 and axisy2 are the horizontal and vertical coordinates of the position, respectively; using Python's own interpolation algorithm griddata to calculate the interpolation function f, and taking the original image coordinates (axisx, axisy) corresponding to the block as the input of the interpolation function f, and outputting the interpolation data z3 of all positions of the original image corresponding to the block.

[0009] Preferably, the step (2) is specifically as follows: filtering out the NAN position after the first interpolation from the interpolation data z3, the coordinates of which are (axisx_nan, axisy_nan); establishing a new interpolation function z2=f1(axisx2, axisy2), and using Python's built-in interpolation algorithm RBF to obtain a new interpolation function f1; inputting the coordinates (axisx_nan, axisy_nan) into f1, thereby obtaining the data z_nan of the NAN position of the original image after the first interpolation, and then assigning z_nan to the corresponding coordinate z3 according to the coordinates (axisx_nan, axisy_nan) to obtain shading_profile.

[0010] Preferably, the screening out of the NAN position in the step (3) is specifically as follows: according to the NAN position in the basic data shading, the NAN position in the shading_profile is inferred, that is, the coordinates (shading_profile_axisx, shading_profile_axisy) are the NAN positions, wherein shading_profile_axisx = x*bin_size+start_x+interval, shading_profile_axisy = y*bin_size+start_y+interval, x, y are the horizontal coordinate and vertical coordinate of the original image corresponding to the NAN position, start_x, start_y are the starting horizontal coordinate and starting vertical coordinate of the horizontal downsampling of the original image, interval represents an array of [-bin_size, bin_size], the block is a square, and bin_size is the side length of the block.

[0011] Preferably, if the coordinates (shading_profile_axisx, shading_profile_axisy) are not within the length and width range of the original image, the coordinates are excluded.

[0012] Preferably, the method for screening non-NAN positions is to set a threshold, and the positions where the pixel values are greater than the threshold are non-NAN positions.

[0013] The present invention makes up for the defect that griddata in python does not support the external interpolation algorithm. The implemented interpolation result is very close to the result of griddedInterpolant in matlab and can be used as an implementation solution for external interpolation required in python. The present invention takes into account both speed and memory. If the RBF interpolation algorithm is used entirely, although it supports external interpolation, the speed is extremely slow and the required memory is very large. Description of the Drawings

[0014] Figure 1 It is a comparison graph of interpolation using the present invention and matlab interpolation. Detailed Embodiment

[0015] The image correction method based on Python interpolation provided by the present invention mainly lies in the following three steps:

[0016] 1. Adopt the griddata interpolation algorithm to process Figure 1 the blank area in, that is, interpolate the non-NAN area. At this time, most of the data can be interpolated, and only the data at the edge that needs to be ignored remains. The advantage of using griddata to process most of the data is that it is fast and occupies less memory.

[0017] 2. Adopt the RBF interpolation algorithm to process Figure 1 the black area in, that is, interpolate the NAN area after the first interpolation. In this way, the amount of data processed by RBF is small. Although RFB is time-consuming and occupies a large amount of memory, due to the small amount of data processed, it can still be quickly implemented.

[0018] 3. Find the positions that should be NAN after external interpolation, and then set the data that needs to be ignored in the result to NAN. Construct a correction matrix according to the data (including NAN) obtained after the second interpolation. Most of the data in this correction matrix is real numbers, and only a very small amount of data is NAN (at the edge). The data being NAN in the correction matrix means that the corresponding position of the original image does not need to be shaded and corrected. Perform shading correction on the original image according to the correction matrix after interpolation to make the brightness of the original image tend to be consistent.

[0019] The present invention realizes image interpolation based on some algorithms in Python, and can achieve the interpolation effect of the algorithms provided by Matlab, specifically as follows:

[0020] The first step: The abscissa of the entire image image is represented by axisx, the ordinate is represented by axisy, and the pixel value corresponding to each coordinate (axisy, axisx) is represented by z. The entire image image is downsampled by blocks and averaged to obtain a thumbnail image1 of each block. The block is usually selected as a square (bin_size * bin_size), which is convenient for calculation, and can also be other shapes such as rectangles. Then, image1 is divided by the largest numerical value of the pixels in image1 to obtain the basic data shading for interpolation, and at the same time, the starting coordinates (axisx1, axisy1) of each block during the downsampling process of the original image are recorded. The coordinates of the non-NAN positions (shading_non_nan_index) in shading are taken out, and then according to this coordinate, the data z2 at the non-NAN positions in shading and the corresponding coordinates (axisx2, axisy2) are selected, and then an interpolation function z2 = f(axisx2, axisy2) is established. At this time, the data z2, axisx2, and axisy2 are all known data, and z2 does not contain NAN, so the built-in interpolation algorithm griddata in Python can be used to find the interpolation function f. Then, according to the interpolation function f, the interpolated data after interpolation of the original image is obtained, that is, the coordinates (axisx, axisy) of the original image are used as the input of the interpolation function f, and the output is the interpolated data z3 at all positions of the original image.

[0021] For the case where the FOV is particularly large, the presented effect is that the pixels in the outer periphery are relatively small and the pixels in the middle are relatively large. At this time, a threshold is set, such as 60, and then all pixels less than 60 are set to NAN. For the case where the FOV is relatively small, no threshold is set, and all data are valid, and there is no NAN at this time. After determining which pixels are NAN, the built-in algorithm np.isnan() in Python is used to determine the coordinates corresponding to these pixels, so as to take out the non-NAN coordinates in shading.

[0022] Step 2: According to z3, filter out the NAN positions (axisx_nan, axisy_nan) after the first interpolation, that is, the NAN positions determined by filtering in the first step. At this time, establish a new interpolation function z2 = f1(axisx2, axisy2). All the data of z2, axisx2, and axisy2 are known data, and the built-in interpolation algorithm RBF in Python can be used to find the new interpolation function f1. After finding f1, input the new coordinates (axisx_nan, axisy_nan), and the data z_nan at the NAN positions of the original image after the first interpolation can be obtained. Then, according to the coordinates (axisx_nan, axisy_nan), assign z_nan to z3 at the corresponding coordinates to obtain shading_profile. At this time, all positions of shading_profile are real numbers.

[0023] Step 3: Since the matlab grid interpolation algorithm stipulates that if there is a NAN in the coordinate points corresponding to the abscissa axisx1, all the data in the interpolation region mapped by this position are NAN. Obviously, the result is different from that of matlab because all the data in shading_profile in the previous step are real numbers. Therefore, it is necessary to find the coordinates of the NAN positions among all the points with abscissa axisx1, then calculate the mapping regions of each coordinate interpolation, and then set the data in these regions to NAN.

[0024] First, based on the coordinates of nan_index in shading (the coordinates where the values in shading are NAN), a new interpolation function f2 = f1(f(x, y, z)) is established. Here, y represents the ordinate in the original image (i.e., axisy in the first step), x represents the abscissa in the original image (i.e., axisx in the first step), and z represents the average value of the rectangular area with (x, y) as the center point and bin_size as the side length in the original image, and the coordinates of the positions that should be NAN in shading_profile are inferred. The positions that should be NAN in shading_profile after interpolation can be inferred based on the NAN positions in shading. The radiation range of one NAN coordinate in shading is (-bin_size, bin_size) (assuming bin_size = 16). For example, the x coordinates are 0, 16, 32, 48, 64..., the y coordinates are 4, 20, 36, 52, 68..., and the z values are 50, NAN, 60, 80, 90.... If the abscissa of the inserted point x11 is 16, then the abscissas of the interpolated data z11 in the range [0, 32] are all affected by the current x11. In the original data (x, y) of the f2 function, the horizontal and vertical radiation ranges are both (-bin_size, bin_size), and the affected area is a rectangular area.

[0025] When the original coordinates are (x, y) and z = NAN, and the inserted data is within the range (-bin_size, bin_size) horizontally and vertically centered on (x, y), the interpolated data is NAN. For example, centered on the origin (0, 0) with bin_size = 16, the radiation range is (-16, 16), forming a rectangular area centered on the origin with a vertical distance of 16 from the origin in all four directions (the coordinates of the four vertices are (16, 16), (16, -16), (-16, 16), (-16, -16)), and the interpolated data within this range is all NAN.

[0026] Speculate the coordinates of the positions that should be NAN in the shading_profile. Specifically, based on the original image coordinates (x, y), the influence range formula for the current point of the original image coordinates can be obtained as shading_profile_axisx = x * bin_size + start_x + interval, where start_x represents the starting abscissa of the horizontal downsampling of the original image image, and interval represents an array of [-bin_size, bin_size]. Similarly, shading_profile_axisy = y * bin_size + start_y + interval, where start_y represents the starting ordinate of the vertical downsampling of the original image image. Then, use the built-in algorithm np.meshgrid in Python to tile the shading_profile_axisx and shading_profile_axisy data to the entire original image screen, and filter out abnormal coordinates, that is, determine whether a certain coordinate is within the length and width range of the original image. If the coordinate exceeds the coordinates of the image area, then the coordinate is an abnormal coordinate. Finally, according to the position of the found coordinates, set the corresponding position of the shading_profile to NAN. Substitute shading_profile_axisx = x * bin_size + start_x + interval into the coordinates when the abscissa x is NAN. Substitute shading_profile_axisy = y * bin_size + start_y + interval into the coordinates when the ordinate y is NAN. The coordinates after tiling the shading_profile_axisx and shading_profile_axisy data to the entire screen are the final required coordinates, that is, the position where the coordinates are (shading_profile_axisx, shading_profile_axisy) is NAN, and set the data at this position to NAN.

[0027] Construct a correction matrix based on the data (including NAN) obtained after the second interpolation. Most of the data in this correction matrix are real numbers, and only a very small amount of data are NAN (at the edges). The data being NAN in the correction matrix means that there is no need to perform shadow correction on this position of the original image. According to the corrected matrix after interpolation, perform shadow correction on the original image to make the brightness of the original image tend to be consistent. The method of shadow correction is the conventional method.

[0028] From Figure 1 It can be seen that the white points located on the left and right edges represent the NAN positions after matlab interpolation, and the black points represent the NAN positions after interpolation using the present invention. The two are almost the same.

[0029] By numerically comparing the results obtained by Matlab interpolation with those obtained by the interpolation of the present invention, it can be found that the maximum fluctuation of the difference between the two arrays is only 0.0025. Thus, it can be seen that their differences only stem from numerical precision.

Claims

1. An image correction method based on Python interpolation, characterized in that, Including: (1) Sampling the original image, screening out non-NAN positions from the image, and using the griddata interpolation algorithm in Python to interpolate the non-NAN area formed by the non-NAN positions; (2) Screening out NAN positions from the data after interpolation in step (1), and using the RBF interpolation algorithm in Python to interpolate the NAN area formed by the NAN positions; (3) Screening out NAN positions from the data after two interpolations, and setting the data at the NAN positions to NAN; Constructing a correction matrix based on the data obtained after the second interpolation, and performing shadow correction on the original image according to the correction matrix. The data in the correction matrix being NAN indicates that no shadow correction is required for the corresponding position of the original image.

2. The image correction method based on Python interpolation according to claim 1, wherein, The specific steps of step (1) are as follows: Downsample the original image by blocks and take the average value to obtain a thumbnail, and then divide each pixel value in the thumbnail by the maximum pixel value in the thumbnail to obtain the basic data shading of the block; Screen out non-NAN positions from the basic data shading, and establish an interpolation function z2 = f(axisx2, axisy2), where z2 is the pixel value at the non-NAN position, and axisx2 and axisy2 are the abscissa and ordinate of this position respectively; Use the built-in interpolation algorithm griddata in Python to find the interpolation function f, and use the coordinates (axisx, axisy) of the original image corresponding to the block as the input of the interpolation function f, and the output is the interpolation data z3 of all positions of the original image corresponding to the block.

3. The image correction method based on Python interpolation according to claim 2, wherein, The specific steps of step (2) are as follows: Screen out the NAN positions after the first interpolation from the interpolation data z3, and the coordinates of this position are (axisx_nan, axisy_nan); Establish a new interpolation function z2 = f1(axisx2, axisy2), and use the built-in interpolation algorithm RBF in Python to find the new interpolation function f1; Input the coordinates (axisx_nan, axisy_nan) into f1 to obtain the data z_nan at the NAN position of the original image after the first interpolation, and then assign z_nan to z3 at the corresponding coordinates according to the coordinates (axisx_nan, axisy_nan) to obtain shading_profile.

4. The image correction method based on Python interpolation according to claim 3, characterized in that, The specific NAN positions screened in step (3) are as follows: Based on the NAN positions in the basic data shading, infer the NAN positions in shading_profile, that is, the coordinates (shading_profile_axisx, shading_profile_axisy) are the NAN positions, where shading_profile_axisx = x * bin_size + start_x + interval, shading_profile_axisy = y * bin_size + start_y + interval, x and y are the abscissa and ordinate of the original image corresponding to the NAN position respectively, start_x and start_y are the starting abscissa and starting ordinate of the horizontal downsampling of the original image respectively, interval represents an array of [-bin_size, bin_size], the block is square, and bin_size is the side length of the block.

5. The image correction method based on Python interpolation according to claim 4, characterized in that, If the coordinates (shading_profile_axisx, shading_profile_axisy) are not within the length and width range of the original image, these coordinates will be excluded.

6. The image correction method based on Python interpolation according to any one of claims 2-5, characterized in that The method for screening non-NAN positions is to set a threshold, and the positions where the pixel values are greater than the threshold are non-NAN positions.

Citation Information

Patent Citations

  • Two-stage interpolation-based matrix completion two-dimensional image processing method

    CN107067374A

  • Small target detection method based on feature descriptor comparison on locally restored blurred picture

    CN110334676A