Quantcast
Channel: OpenCV Q&A Forum - RSS feed
Viewing all articles
Browse latest Browse all 2088

Python - Undistort script won't work

$
0
0
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: #Modules 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): ")) os.chdir(filepath) 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): ")) os.chdir(imgpath) 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) cv2.waitKey(0) 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 not since even after I assign each specific value by hand in the script, the undistort is not performed. What do you think might be the problem?

Viewing all articles
Browse latest Browse all 2088

Trending Articles