Image distortion correction with Python OpenCV

Asked 2 years ago, Updated 2 years ago, 96 views

I would like to correct the distortion of the image with Python's OpenCV, but
from the chessboard taken with the following code. I understand that the distortion coefficient is calculated, but how should I calculate this coefficient when correcting distortion of images uploaded by users (image size is also irregular)?

Please let me know if there is a way to correct the distortion without using findChessboard Corners or findCirclesGrid.

#-*-coding:utf-8-*-
    import numpy as np
    import cv2
    import glob

    fileName="chess.jpg"
    imagePath="./"+fileName

    image=cv2.imread(imagePath)
    grayImage=cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)
    ChessboardPatternSize=(9,7)

    height, width = image.shape [:2]

    objPoints=[]#3d points
    imgPoints=[]#2d points 

    # termination criteria
    criteria=(cv2.TERM_CRITERIA_EPS+cv2.TERM_CRITERIA_MAX_ITER, 0, 0.1)

    objp = np.zero((np.prod(ChessboardPatternSize), 3), np.float32)
    objp[:,:2] = np.indices(ChessboardPatternSize).T.reshape(-1,2)
    objp*=1

    ret,corners=cv2.findChessboardCorners(grayImage, ChessboardPatternSize)

    ifret==True:

      corners2 = cv2.cornerSubPix (grayImage, corners, (11, 11), (-1,-1), criteria)

      objPoints.append(objp)
      imgPoints.append(corners.reshape(-1,2)))

      print objPoints
      print imgPoints
      np.save ('objPoints.npy', objPoints)
      np.save ('imgPoints.npy', imgPoints)

python opencv

2022-09-30 16:17

1 Answers

If there is a way to compensate for distortion without findingChessboardCorners or findCirclesGrid,
I want to correct the distortion of uploaded images by wearing wide-angle lenses on my smartphone.

It's impossible.Image correction for unknown distortion cannot be performed without calibration.

You cannot model "distortion" with just the given assumptions, and you cannot define the original image you want to restore, or reverse transformation.Calibration using a checker board or the like assumes the distortion model formulation of the imaging system and performs correction by inverse estimation of the parameters of the model.Restoring the original image without such auxiliary data is impossible (except for human intuition).

Just a quick supplement: If you can fix the optical properties of a wide-angle lens attached to your smartphone and the mounting position, you can record pre-calibrated parameters and correct all images with the same parameters.


2022-09-30 16:17

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.