Hi everyone!

I am a novice at python programming and @muzammil360 I’m trying to follow the code you shared (matlab?). I’ve managed to obtain most parameters being passed into GetProjectionOnDistortedSpace() but for P. Could you point me in the right direction to obtain P please? How do I produce an undistorted image afterwards?

I’d be glad if someone could share their code in python please (working or not)?

Many thanks

My half baked python code below if someone could help:

import cv2

import numpy as np

import exiftool

import math

import sys

import numpy.linalg as lin

def GetProjectionOnDistortedSpace(P, distCoef, affineMat, camMatK, cx, cy):

p0 = distCoef[0]

p1 = distCoef[1]

p2 = distCoef[2]

p3 = distCoef[3]

```
p = lin.solve(camMatK.T.dot(camMatK),camMatK.T.dot(P)) #camMatK\P ->left matrix division or inv(camMatK)*P
C = affineMat[0]
D = affineMat[1]
E = affineMat[2]
F = affineMat[3]
return Pdistorted
```

def correct_Fisheyedistortion(meta, image):

#get the two principal points

pp = np.array(meta.get_item(‘XMP:PrincipalPoint’).split(’,’)).astype(np.float)

# values in pp are in [mm] and need to be rescaled to pixels

FocalPlaneXResolution = float(meta.get_item(‘EXIF:FocalPlaneXResolution’))

FocalPlaneYResolution = float(meta.get_item(‘EXIF:FocalPlaneYResolution’))

```
cx = pp[0] * FocalPlaneXResolution
cy = pp[1] * FocalPlaneYResolution
fx = FocalPlaneXResolution * float(meta.get_item('XMP:FocalLength'))
fy = FocalPlaneYResolution * float(meta.get_item('XMP:FocalLength'))
# Is this how you obtain P?
h, w = image.shape
x = np.arange(0, w, 1)
y = np.arange(0, h, 1)
rows = []
for iy in y:
for ix in x:
rows.append([ix,iy])
P = np.array(rows)
# set up camera matrix for undistortfisheye
camMatK = np.zeros((3, 3))
camMatK[0, 0] = fx
camMatK[1, 1] = fy
camMatK[2, 2] = 1.0
camMatK[0, 2] = cx
camMatK[1, 2] = cy
distCoef = meta.get_item('XMP:FisheyePolynomial').split(',')
affineMat = meta.get_item('XMP:FisheyeAffineMatrix').split(',')
Pdistorted = GetProjectionOnDistortedSpace(P, distCoef, affineMat, camMatK, cx, cy)
undistorted_img = cv2.remap(image, P, Pdistorted, interpolation=cv2.INTER_LINEAR, borderMode=cv2.BORDER_CONSTANT)
return undistorted_img
```