Python - Undistort script won't work

asked 2016-07-17 08:32:50 -0500

n.argirop gravatar image

I wrote the following script to undistort multiple photos in one go by defining a containing folder. Because of the fact that the camera I am using is calibrated professionally and all of the intrinsic and distortion parameters are known, I have specified a camera matrix and the distortion coefficients. The script runs without any errors but the output images are exactly the same as the original-distorted images. There is no way that the changes are difficult to detect since I am using a GoPro camera with the (in)famous fisheye distortion. Here is the script:

import os
from os import listdir
from os.path import isfile, join
import cv2
import numpy as np
from xlrd import open_workbook,cellname
from xlutils.copy import copy

#Excel file loading
filepath = str(raw_input("\nEnter Excel file path (e.g. C:/Users/user/Desktop): "))
filename = str(raw_input("\nEnter Excel file name (e.g. camera.xls): "))

#Excel file values
rb = open_workbook(filepath+filename,formatting_info=True)
r_sheet = rb.sheet_by_index(0)
wb = copy(rb)
sheet = wb.get_sheet(0)
psize = r_sheet.cell(0,0).value #Pixel pitch
focal = r_sheet.cell(0,1).value #Focal length in mm
pixden = 1/psize #Sensor pixels/mm
fx = pixden*f
fy = pixden*f
cx = r_sheet.cell(0,2).value #P.P. x-offset
cy = r_sheet.cell(0,3).value #P.P. y-offset
k1 = r_sheet.cell(0,4).value
k2 = r_sheet.cell(1,4).value
k3 = r_sheet.cell(2,4).value
k4 = r_sheet.cell(3,4).value
k5 = r_sheet.cell(4,4).value
k6 = r_sheet.cell(5,4).value
p1 = r_sheet.cell(0,5).value
p2 = r_sheet.cell(0,6).value

#Camera Matrix
cammat = np.zeros((3, 3), dtype=np.float32)
cammat[0,0] = fx
cammat[0,1] = 0
cammat[0,2] = cx
cammat[1,0] = 0
cammat[1,1] = fy
cammat[1,2] = cy
cammat[2,0] = 0
cammat[2,1] = 0
cammat[2,2] = 0

#Distortion Coefficients Vector
radd = np.zeros((4,1), np.float64)
radd[0,0] = k1
radd[1,0] = k2
radd[2,0] = p1
radd[3,0] = p2

#Load all images in the specified directory
imgpath = str(raw_input("\nEnter image path (e.g. C:/Users/user/Desktop): "))
allimgfiles = [f for f in listdir(imgpath) if isfile(join(imgpath,f))]
images = np.empty(len(allimgfiles), dtype=object)

#Apply undistortion
for n in range(0, len(allimgfiles)):
    images[n] = cv2.imread(join(imgpath, allimgfiles[n]))
    dimg = cv2.imread("E:/DSC06786.JPG")
    udimg = cv2.undistort(dimg, cammat, radd, None)

    #Save undistorted images
    cv2.imwrite("e:/undistorted.jpg", udimg)

    #Display undistorted image
    window_width = 1000
    window_height = 750
    cv2.namedWindow("Top 'k' features", cv2.WINDOW_NORMAL)
    cv2.resizeWindow("Top 'k' features", window_width, window_height)
    cv2.imshow("Top 'k' features", dimg)

The intrinsic and distortion parameters are saved in an excel file as you will notice which I cannot upload unfortunately. The values are parsed correctly and it is not were the problem lies. I have quadraple checked if the excel file is the problem, but it is ... (more)

edit retag flag offensive close merge delete


shouldn't cammat[2,2] = 1 ?

berak gravatar imageberak ( 2016-07-17 09:05:17 -0500 )edit

You are right. I just changed it though and it still doesn't work.

n.argirop gravatar imagen.argirop ( 2016-07-17 09:23:52 -0500 )edit

The excel file and an image sample can be found here

n.argirop gravatar imagen.argirop ( 2016-07-17 09:30:43 -0500 )edit