Gray code-based lens distortion correction method
Through the lens distortion correction method based on Gray code, Gray code projection pictures and perspective transformation are used to establish the mapping relationship between lens distortion pictures and non-distortion pictures, which solves the problem of poor lens distortion correction effect in the existing technology and realizes the lens distortion correction method. Effective correction of distortion.
Patent Information
- Application Number
- CN202310195954.1
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2023-03-02
- Publication Date
- 2024-09-03
AI Technical Summary
In the existing technology, Zhang's calibration method cannot give good results for the edge portion of the lens with a large degree of distortion, and cannot effectively correct lens distortion.
The lens distortion correction method based on Gray code is used to generate Gray code projection pictures, build the shooting environment, and use perspective transformation and interpolation methods to establish the mapping relationship between distorted pictures and non-distorted pictures to complete the lens distortion correction.
For a lens with a large degree of distortion, a one-to-one correspondence between image pixel positions before and after distortion correction is achieved, and the problem of poor lens distortion correction in the prior art is solved.
Smart Images

Figure CN118590767A
Abstract
Description
Technical Field
[0001] The invention belongs to the technical field of image processing, and in particular relates to a method for correcting lens distortion based on Gray code. Background Art
[0002] In the prior art, the purpose of camera calibration is to establish a camera imaging geometry model and correct lens distortion. In the field of image processing, Zhang's calibration method is a camera calibration method based on calibration objects. OpenCV or MATLAB both provide corresponding tools and methods. Zhang's calibration, in simple terms, is a process of solving the camera intrinsic parameters through the constraints of the homography matrix.
[0003] However, for lenses with large distortion, Zhang's calibration method cannot give good results for the edge parts.
[0004] In addition, commonly used technical terms in the prior art include:
[0005] 1. Gray code: In the encoding of a set of numbers, if any two adjacent codes differ by only one binary digit, this encoding is called Gray code.
[0006] 2. Gray code projection: a set of black and white stripe patterns with spatial coding, where the bright area represents 1 and the dark area represents 0. Its coding in the x or y direction has the characteristics of Gray code.
[0007] 3. Lens distortion: Lens distortion is actually a general term for the inherent perspective distortion of optical lenses. Common lens distortions are classified into pincushion distortion, barrel distortion, and linear distortion.
[0008] 4. Distortion correction: Distortion correction is a mapping that projects distorted pixels to corrected pixel positions. Summary of the invention
[0009] In order to solve the above problem, the purpose of the present application is to obtain a one-to-one correspondence between the positions of image pixels before and after distortion correction by encoding each pixel.
[0010] Specifically, the present invention provides a method for lens distortion correction based on Gray code, the method comprising the following steps:
[0011] S1. Generate Gray code projection image:
[0012] Gray code projection image generation adopts the Gray code image encoding and decoding method of the structured_light module under the open source library opencv contribute. This module includes how to generate a set of Gray code images and decode the generated Gray code images.
[0013] S2. Set up the shooting environment:
[0014] Gray code image shooting method, the camera is located in front of the center of the display device, and the symmetry of the four corners is used to determine whether the sensor and the device are approximately parallel. Approximate parallelism means that the angle between the planes is less than 5 degrees.
[0015] S3, using the Gray code of the four edge points of the distorted image, find the position corresponding to the original image, and obtain the non-distorted image that should be taken if the lens is not distorted through perspective transformation;
[0016] S4. Through the one-to-one correspondence between Gray codes, the mapping relationship between the distorted image and the non-distorted image is obtained by interpolation, thereby completing the distortion correction of the camera.
[0017] In the step S1, assuming a 10-bit, 1024*768 Gray code picture, the 10 groups of pictures encoded in the x direction are from 0 (0000000000B) to 1023 (1111111111B), and the 10 groups of pictures encoded in the y direction are from 0 (0000000000B) to 767 (1011111111B).
[0018] The step S2 further comprises:
[0019] Gray code projected images are displayed on electronic screens or printed on paper;
[0020] Assuming that the camera, i.e., the lens, that needs distortion correction is located on an electronic screen vertical line from the center of the screen, and its distance from the screen is related to the field of view of the camera, i.e., the lens. It is required that the content captured by the camera must not exceed the display range of the Gray code image, that is, there is no distance limit, and the content captured by the camera must not exceed the display range of the Gray code image on the screen. Under this premise, more Gray code image content is captured; assuming a 1024*768 image, the Gray code image should be displayed in proportion when played on the screen, and the aspect ratio of the display should be 4:3; if printed on paper, the aspect ratio of the print should also be 4:3.
[0021] The step S3 further comprises:
[0022] S3.1, the distorted Gray code image captured:
[0023] Adjust the angle of the camera to ensure that the imaging sensor and the Gray code display device are in an approximately parallel state, where the approximately parallel means that the plane angle is less than 5 degrees. The photographed image sets the indication distances at the four corners to determine whether the adjustment is in place. The indication distance is the distance from the four vertices of the rectangular image to the closest stripe dividing line, where the closest distance is within 1 / 5 of the image height. If the distances from the two vertices on the upper edge of the rectangular image to the same dividing line are approximately equal, where the approximately equal means that the difference is within 10 pixels, and the distances from the two vertices on the lower edge of the image to the same dividing line are also approximately equal, where the approximately equal means that the difference is within 10 pixels, then it is considered that the imaging plane of the camera and the Gray code display plane are in an approximately parallel state, where the approximately parallel means that the plane angle is less than 5 degrees. The photographed image is only a part of the original image and will not exceed the original image range. Because it is necessary to find the correspondence between the points in the photographed image and the actually displayed Gray code, it is required that the content of the photographed image cannot exceed the Gray code original image, otherwise the correspondence cannot be found. The above requirements can be achieved by moving the position of the camera.
[0024] S3.2, calculate the Gray code of the distorted image taken:
[0025] Calculate the Gray code encoding of a set of images using the Gray code encoding and decoding module of the structured_light module of the OpenCV contribution library: for each pixel, there is a corresponding Gray code value in the x and y directions, and there are multiple pixels with the same Gray code value;
[0026] S3.3, generate undistorted images:
[0027] Record the Gray code values of the midpoints (a, b, c, d) of the four sides of the distorted image in the x and y directions. In the original 1024*768 image, there must be and only four points (a', b', c', d') that correspond to each other and have the same Gray code values in the x and y directions. Through the perspective transformation of the four pairs of points, the theoretically corrected non-distorted image is obtained.
[0028] S3.4, calculate the Gray code of the undistorted image:
[0029] The method is the same as step S3.2, but the undistorted Gray code image obtained in step S3.3 is calculated.
[0030] The step S3.3 generates a non-distorted image: assuming that the coordinates of the midpoints a, b, c, and d of the four sides of the distorted image are (0, 360), (640, 0), (1279, 360), and (640, 719), respectively, and the corresponding Gray codes are (218, 394), (838, 394), (514, 230), and (518, 534), respectively, which correspond to the coordinates of the original image (1024×768), that is, the Gray code values in the x and y directions. Therefore, through the perspective transformation from ((218,394), (838,394), (514,230), (518,534)) to ((0,360), (640,0), (1279,360), (640,719)), we can get the 1280*720 picture that should be taken in the theoretical case without distortion.
[0031] The step S4 further comprises: calculating a one-to-one correspondence between image pixels before and after distortion correction according to the Gray codes of the distorted image and the non-distorted image:
[0032] After obtaining the image after distortion correction, we can find the corresponding points in the original image. Assume that there are m points with Gray code values (Gx, Gy) in the x and y directions of the image after distortion correction, which are located in the interval ((x1', x2'), (y1', y2')). In the image before distortion correction, there are n points with Gray code values (Gx, Gy) in the x and y directions, which are located in the interval ((x1, x2), (y1, y2)). Then we can obtain the corresponding relationship between the two by interpolation: x = (x'-x1')(x2-x1) / (x2'-x1')+x1, y = (y'-y1)(y2-y1) / (y2'-y1')+y1';
[0033] By traversing each pixel of the non-distorted image using this method, we can obtain the mapping matrix from the distorted image to the non-distorted image, (x, y) = map (x', y'); the subsequent images can be corrected for the lens distortion through this mapping matrix.
[0034] Therefore, the advantage of the present application is that for a lens with a large degree of distortion, each pixel is encoded by a simple method, so as to obtain a one-to-one correspondence between the positions of image pixels before and after distortion correction, thereby solving the problems existing in the prior art. BRIEF DESCRIPTION OF THE DRAWINGS
[0035] The drawings described herein are used to provide a further understanding of the present invention, constitute a part of this application, and do not constitute a limitation of the present invention.
[0036] Figure 1 Schematic diagram of Gray code projection image in this method.
[0037] Figure 2 Schematic diagram of the Gray code shooting environment in this method.
[0038] Figure 3 is a schematic diagram of an example of adjusting the camera angle in this method.
[0039] Figure 4 This is a schematic diagram of a theoretically corrected non-distorted image obtained by perspective transformation of four pairs of points in this method.
[0040] Figure 5 is a schematic diagram of the process of this method. DETAILED DESCRIPTION
[0041] In order to more clearly understand the technical content and advantages of the present invention, the present invention is now further described in detail in conjunction with the accompanying drawings.
[0042] like Figure 5 As shown, the present application provides a method for lens distortion correction based on Gray code, comprising:
[0043] S1. Generate Gray code projection image:
[0044] The Gray code projection image generation method adopts the Gray code image encoding and decoding method of the structured_light module under the open source library opencv contribute. This module includes how to generate a set of Gray code images and decode the generated Gray code images; the Gray code image actually used in the embodiment is generated by this code;
[0045] S2. Set up the shooting environment:
[0046] Gray code image shooting method, located in front of the center of the display device, and through the symmetry of the four corners, determine whether the sensor and the device are approximately parallel (the plane angle is less than 5 degrees);
[0047] S3, using the Gray code of the four edge points of the distorted image (the embodiment of this article uses the midpoints of the four edges) to find the position of the original image that should be taken, and through perspective transformation, obtain the non-distorted image that should be taken when the lens is not distorted;
[0048] S4. Through the one-to-one correspondence between Gray codes, the mapping relationship between the distorted image and the non-distorted image is obtained by interpolation, thereby completing the distortion correction of the camera.
[0049] The method further comprises:
[0050] S1, generate Gray code projection image:
[0051] The Gray code projection image generation method of this application adopts the Gray code image encoding and decoding method of the structured_light module under the open source library opencv contribute. Take a 10-bit, 1024*768 Gray code image as an example. Figure 1 As shown, the 10 groups of pictures on the left are encoded in the x direction, from 0 (0000000000B) to 1023 (1111111111B), and the 10 groups of pictures on the right are encoded in the y direction, from 0 (0000000000B) to 767 (1011111111B).
[0052] S2, set up the shooting environment:
[0053] Gray code projection images can be displayed on electronic screens or printed on paper. Figure 2 As shown, taking the electronic screen as an example, the camera (lens) that needs distortion correction is located on the vertical line of the center point of the screen, and the position from the screen is related to the field of view of the camera (lens) (the larger the field of view, the more content is captured, and the closer it needs to be to the screen; the smaller the field of view, the farther it can be from the screen). The content captured by the camera must not exceed the display range of the gray code image. In order to improve the accuracy, the captured gray code image should occupy as much proportion of the original image size as possible, that is, there is no distance limit, so that the content captured by the camera does not exceed the display range of the gray code image on the screen. Under this premise, more gray code image content is captured. When playing gray code images on the screen, they should be displayed in equal proportions. Taking a 1024*768 image as an example, the aspect ratio of the display should be 4:3. If printed on paper, the aspect ratio of the print should also be 4:3.
[0054] S3.1, the distorted Gray code image captured:
[0055] Adjust the camera angle to ensure that the imaging sensor and the Gray code display device are approximately parallel (the plane angle is less than 5 degrees). Figure 3 For example, you can set the white arrows at the four corners to indicate whether the distance is equal to determine whether the adjustment is in place. The indicated distance is the distance from the four vertices of the rectangular image to the stripe dividing line that is closer (within 1 / 5 of the image height). If the distances from the two vertices along the upper edge of the rectangular image to the same dividing line are approximately equal (within 10 pixels), and the distances from the two vertices along the lower edge of the image to the same dividing line are also approximately equal (within 10 pixels), then it is considered that the imaging plane of the camera and the Gray code display plane are in an approximately parallel state (the plane angle is less than 5 degrees); the captured image is only a part of the original image and will not exceed the original image range. Take the size of the captured image as 1280*720 as an example. Note that this is only used for illustration and there is no exact correspondence with the size of the Gray code image.
[0056] S3.2, calculate the Gray code of the distorted image taken:
[0057] Calculate the Gray code of a set of images using the same method as the Gray code encoding and decoding module of the structured_light module of the OpenCV contribution library. For each pixel, there is a corresponding Gray code value in the x and y directions, and there are cases where multiple pixels have the same Gray code value.
[0058] S3.3, generate undistorted images:
[0059] Record the Gray code values of the midpoints (a, b, c, d) of the four sides of the distorted image in the x and y directions. In the original image (1024*768), there must be only four points (a', b', c', d') that correspond to each other and have the same Gray code values in the x and y directions. Figure 4 As shown, through the perspective transformation of four pairs of points, a theoretically corrected non-distorted image is obtained. The Gray code of the four edge points of the distorted image is used in this special case to calculate the Gray code of the four edges of the distorted image and find the corresponding position of the original image.
[0060] For example, the coordinates of the midpoints a, b, c, and d of the four sides of the distorted image are (0, 360), (640, 0), (1279, 360), and (640, 719), respectively. The corresponding Gray codes are (218, 394), (838, 394), (514, 230), and (518, 534), which correspond to the coordinates of the original image (1024×768), that is, the Gray code values in the x and y directions. Therefore, through the perspective transformation from ((218, 394), (838, 394), (514, 230), (518, 534)) to ((0, 360), (640, 0), (1279, 360), (640, 719)), we can get the 1280*720 image that should be taken in the theoretical case of no distortion.
[0061] S3.4, calculate the Gray code of the undistorted image:
[0062] As in step S3.2, what is calculated is the undistorted Gray code image obtained in step S3.3.
[0063] S4, according to the Gray code of the distorted image and the non-distorted image, calculate the one-to-one correspondence between the image pixels before and after distortion correction:
[0064] Depend on Figure 4It can be seen that after the distortion correction, the corresponding points can be found in the original image. Assume that there are m points with Gray code values (Gx, Gy) in the x and y directions of the distortion correction image, which are located in the interval ((x1', x2'), (y1', y2')). In the image before distortion correction, there are n points with Gray code values (Gx, Gy) in the x and y directions, which are located in the interval ((x1, x2), (y1, y2)). Then, the corresponding relationship between the two can be obtained by interpolation, x = (x'-x1')(x2-x1) / (x2'-x1')+x1, y = (y'-y1)(y2-y1) / (y2'-y1')+y1'. For each pixel point of the non-distorted image, this method is used to traverse it once, and the mapping matrix from the distorted image to the non-distorted image can be obtained, (x, y) = map(x', y'). The subsequent pictures taken can complete the distortion correction of the lens through the mapping matrix.
[0065] The above description is only a preferred embodiment of the present invention and is not intended to limit the present invention. For those skilled in the art, the embodiments of the present invention may have various modifications and variations. Any modification, equivalent replacement, improvement, etc. made within the spirit and principle of the present invention shall be included in the protection scope of the present invention.
Claims
1. A method for lens distortion correction based on Gray code, characterized in that: The method comprises the following steps: S1. Generate Gray code projection image: Gray code projection image generation adopts the Gray code image encoding and decoding method of the structured_light module under the open source library opencv contribute. This module includes how to generate a set of Gray code images and decode the generated Gray code images. S2. Set up the shooting environment: Gray code image shooting method, the camera is located in front of the center of the display device, and the symmetry of the four corners is used to determine whether the sensor and the device are approximately parallel. Approximate parallelism means that the plane angle is less than 5 degrees; S3, using the Gray code of the four edge points of the distorted image, find the position corresponding to the original image, and obtain the non-distorted image that should be taken if the lens is not distorted through perspective transformation; S4. Through the one-to-one correspondence between Gray codes, the mapping relationship between the distorted image and the non-distorted image is obtained by interpolation, thereby completing the distortion correction of the camera.
2. The method for lens distortion correction based on Gray code according to claim 1, characterized in that: In the step S1, assuming a 10-bit, 1024*768 Gray code picture, the 10 groups of pictures encoded in the x direction are from 0 (0000000000B) to 1023 (1111111111B), and the 10 groups of pictures encoded in the y direction are from 0 (0000000000B) to 767 (1011111111B).
3. The method for lens distortion correction based on Gray code according to claim 1, characterized in that: The step S2 further comprises: Gray code projected images are displayed on electronic screens or printed on paper; Assuming that the camera, i.e., the lens, that needs distortion correction is located on an electronic screen vertical line from the center of the screen, and its distance from the screen is related to the field of view of the camera, i.e., the lens. It is required that the content captured by the camera must not exceed the display range of the Gray code image, that is, there is no distance limit, and the content captured by the camera must not exceed the display range of the Gray code image on the screen. Under this premise, more Gray code image content is captured; assuming a 1024*768 image, the Gray code image should be displayed in proportion when played on the screen, and the aspect ratio of the display should be 4:3; if printed on paper, the aspect ratio of the print should also be 4:
3.
4. The method for correcting lens distortion based on Gray code according to claim 3, characterized in that: The step S3 further comprises: S3.1, the distorted Gray code image captured: Adjust the angle of the camera to ensure that the imaging sensor and the Gray code display device are in an approximately parallel state, where the approximately parallel means that the plane angle is less than 5 degrees. The photographed image sets the indication distances at the four corners to determine whether the adjustment is in place. The indication distance is the distance from the four vertices of the rectangular image to the closest stripe dividing line, where the closest distance is within 1 / 5 of the image height. If the distances from the two vertices on the upper edge of the rectangular image to the same dividing line are approximately equal, where the approximately equal means that the difference is within 10 pixels, and the distances from the two vertices on the lower edge of the image to the same dividing line are also approximately equal, where the approximately equal means that the difference is within 10 pixels, then it is considered that the imaging plane of the camera and the Gray code display plane are in an approximately parallel state, where the approximately parallel means that the plane angle is less than 5 degrees. The photographed image is only a part of the original image and will not exceed the original image range. Because it is necessary to find the correspondence between the points in the photographed image and the actually displayed Gray code, it is required that the content of the photographed image cannot exceed the Gray code original image, otherwise the correspondence cannot be found. The above requirements can be achieved by moving the position of the camera. S3.2, calculate the Gray code of the distorted image taken: Calculate the Gray code encoding of a set of images using the Gray code encoding and decoding module of the structured_light module of the OpenCV contribution library: for each pixel, there is a corresponding Gray code value in the x and y directions, and there are multiple pixels with the same Gray code value; S3.3, generate undistorted images: Record the Gray code values of the midpoints (a, b, c, d) of the four sides of the distorted image in the x and y directions. In the original 1024*768 image, there must be and only four points (a', b', c', d') that correspond to each other and have the same Gray code values in the x and y directions. Through the perspective transformation of the four pairs of points, the theoretically corrected non-distorted image is obtained. S3.4, calculate the Gray code of the undistorted image: The method is the same as step S3.2, but the undistorted Gray code image obtained in step S3.3 is calculated.
5. The method for correcting lens distortion based on Gray code according to claim 4, characterized in that: The step S3.3 generates a non-distorted image: Assume that the coordinates of the midpoints a, b, c, and d of the four sides of the distorted image are (0, 360), (640, 0), (1279, 360), and (640, 719), respectively, and the corresponding Gray codes are (218, 394), (838, 394), (514, 230), and (518, 534), respectively, corresponding to the original image (1024×7 68), which are the gray code values in the x and y directions. Therefore, through the perspective transformation from ((218,394), (838,394), (514,230), (518,534)) to ((0,360), (640,0), (1279,360), (640,719)), we can get the 1280*720 picture that should be taken in the theoretical case without distortion.
6. The method for lens distortion correction based on Gray code according to claim 1, characterized in that: The step S4 further comprises: calculating a one-to-one correspondence between image pixels before and after distortion correction according to the Gray codes of the distorted image and the non-distorted image: After obtaining the image after distortion correction, we can find the corresponding points in the original image. Assume that there are m points with Gray code values (Gx, Gy) in the x and y directions of the image after distortion correction, which are located in the interval ((x1', x2'), (y1', y2')). In the image before distortion correction, there are n points with Gray code values (Gx, Gy) in the x and y directions, which are located in the interval ((x1, x2), (y1, y2)). Then we can obtain the corresponding relationship between the two by interpolation: x = (x'-x1')(x2-x1) / (x2'-x1')+x1, y = (y'-y1)(y2-y1) / (y2'-y1')+y1'; By traversing each pixel of the non-distorted image using this method, we can obtain the mapping matrix from the distorted image to the non-distorted image, (x, y) = map (x', y'); the subsequent images can be corrected for the lens distortion through this mapping matrix.
