In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
Today, I would like to share with you the relevant knowledge of how python uses OpenCV to achieve camera correction. The content is detailed and the logic is clear. I believe most people still know too much about this knowledge, so share this article for your reference. I hope you can get something after reading this article. Let's take a look.
1. Camera calibration
According to Zhang Zhengyou correction algorithm, the checkerboard data correction is used to correct the vehicle camera, and the internal parameter matrix, external parameter matrix and distortion coefficient are calculated.
The calibration process is as follows:
Prepare checkerboard data, that is, pictures for calibration
Extract corner information from each picture
Draw the extracted corners on the chessboard (not necessary, just to display the results)
Use the extracted corners to calibrate the camera
Get the parameter information of the camera
two。 About several API used for camera correction:
1. Find the corners of the chessboard.
Rect, corners = cv2.findChessboardCorners (image, pattern_size, flags)
Parameters:
Image: the input chessboard must be an 8-bit grayscale or color image
Pattern_size: the number of corners per column per row (interior corners) in a chessboard chart.
Flags: used to define additional filtering steps to help find the corners of the board. All variables can be used individually or in a logical or combined manner. The main values are:
CV_CALIB_CB_ADAPTIVE_THRESH: converts an image to black and white using an adaptive threshold (calculated from the average image brightness) rather than a fixed threshold.
CV_CALIB_CB_NORMALIZE_IMAGE: before using a fixed threshold or an adaptive threshold for binarization, use cvNormalizeHist to equalize the brightness of the image.
CV_CALIB_CB_FILTER_QUADS: use other criteria (such as contour area, perimeter, square shape) to remove erroneous squares detected during contour detection.
Return:
Corners: corner detected
Rect: returns 1 if the output finds the corner, otherwise it returns 0.
2. After detecting the corner, you can draw the measured corner on the image. The API used is:
Cv2.drawChessboardCorners (img, pattern_size, corners, rect)
Parameters:
Img: pre-draw an image of detecting corners
Pattern_size: the shape of the pre-drawn corner
Corners: corner matrix
Rect: indicates whether all the corners of the chessboard have been found and can be set to the return value of findChessboardCorners
Note: if all corners are found, the corners will be drawn in a different color (each row will be drawn in a separate color) and the corners will be connected by lines in a certain order.
3. Calculate the internal and external parameters by using the results of calibration.
Ret, mtx, dist, rvecs, tvecs = cv2.calibrateCamera (object_points, image_points, image_size, None, None)
Parameters:
Object_points: the point in the world coordinate system, in the case of using the chessboard, the coordinate value of z is 0, while the XQuery y coordinate is measured inside, and the units of inches are selected, then the results of all parameters are also expressed in inches. The easiest way is to define each square of the chessboard as a unit.
Image_points: the coordinates of the corners found in the image, including all the points provided by object_points
Image_size: the size of an image, measured in pixels
Return:
Ret: return value
Mtx: the camera's internal reference matrix, a matrix of size 3 to 3.
Dist: distortion coefficient, a vector of 5 to 1
Rvecs: rotation variabl
Tvecs: translation variabl
2.1 Image de-distortion
In the previous step, we get the internal parameters and distortion coefficient of the camera, and use them to remove the distortion of the image. the most direct way is to call the function in opencv to get the dedistorted image.
Def img_undistort (img, mtx, dist): dst = cv2.undistort (img, mtx, dist, None, mtx) return dst
Find the distorted API:
Dst = cv2.undistort (img, mtx, dist, None, mtx)
Parameters:
Img: image to be corrected
Mtx: internal parameters of the camera
Dist: distortion coefficient of camera
Return:
Dst: the result of image correction
3. Camera correction import cv2import numpy as npimport matplotlib.pyplot as pltimport matplotlib.image as mpimgimport globdef plot_contrast_imgs (origin_img, converted_img, origin_img_title= "origin_img", converted_img_title= "converted_img", converted_img_gray=False): "" used to compare and display two images "" fig, (ax1, ax2) = plt.subplots (1, 2, figsize= (15) " 20) ax1.set_title (origin_img_title) ax1.imshow (origin_img) ax2.set_title (converted_img_title) if converted_img_gray==True: ax2.imshow (converted_img, cmap= "gray") else: ax2.imshow (converted_img) plt.show () # 1. Parameter setting: define the number of horizontal and vertical corners of the chessboard and specify the position of the correction image nx = 9ny = 6file_paths = glob.glob (". / camera_cal/calibration*.jpg") # 2. Calculate the internal and external parameters of the camera and the distortion coefficient def cal_calibrate_params (file_paths): object_points = [] # points in 3D space: 3D image_points = [] # points in image space: 2d # 2.1 generate real intersection coordinates: similar to (0 * ny 0), (2) np.zeros = np.zeros (nx * ny) 3), np.float32) objp [:,: 2] = np.mgrid [0:nx, 0:ny] .T.reshape (- 1,2) # 2.2 detect the corner coordinates of each image for file_path in file_paths: img = cv2.imread (file_path) # convert the image to a grayscale image gray = cv2.cvtColor (img Cv2.COLOR_BGR2GRAY) # automatically detect the corners of 4 checkerboards (the intersection of 2 white and 2 black) rect, corners = cv2.findChessboardCorners (gray, (nx, ny), None) # if corners are detected Store it in object_points and image_points if rect = = True: object_points.append (objp) image_points.append (corners) # 2.3.Obtain camera parameters ret, mtx, dist, rvecs, tvecs = cv2.calibrateCamera (object_points, image_points, gray.shape [::-1], None, None) return ret, mtx, dist, rvecs, tvecsdef img_undistort (img, mtx) Dist): "" Image distortion "" return cv2.undistort (img, mtx, dist, None, mtx) # Test the effect of the distortion function file_paths = glob.glob (". / camera_cal/calibration*.jpg") ret, mtx, dist, rvecs Tvecs = cal_calibrate_params (file_paths) if mtx.any ()! = None: # a.any () or a.all () img = mpimg.imread (". / camera_cal/calibration1.jpg") undistort_img = img_undistort (img, mtx, dist) plot_contrast_imgs (img, undistort_img) print ("done!") else: print ("failed") these are all the contents of the article "how python uses OpenCV to achieve camera correction" Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.