imgProcessor.camera package

A collection to execute a camera calibration and to correct am image, removing lens, perspective etc. distortions

Submodules

imgProcessor.camera.CameraCalibration module

class imgProcessor.camera.CameraCalibration.CameraCalibration[source]

Bases: object

Collect a arrays and parameters needed for camera calibration (.add###) Load and save to hard drive (.loadFromFile, .saveToFile) and correct images (.correct)

addDarkCurrent(slope, intercept, date=None, info='', error=None)[source]
Parameters:
  • slope (float) –
  • intercept (np.array) –
  • error (float) –
  • slope – dPx/dExposureTime[sec]
  • error – absolute
  • date (str) – “DD Mon YY” e.g. “30 Nov 16”
addDeconvolutionBalance(balance, date=None, info='', light_spectrum='visible')[source]
addFlatField(arr, date=None, info='', error=None, light_spectrum='visible')[source]

light_spectrum = light, IR ...

addLens(lens, date, info='', light_spectrum='visible')[source]

lens -> instance of LensDistortion or saved file

addNoise(nlf_coeff, date=None, info='', error=None)[source]
Parameters:
  • nlf_coeff (list) –
  • error (float) – absolute
  • info (str) – additional information
  • date (str) – “DD Mon YY” e.g. “30 Nov 16”
addPSF(psf, date=None, info='', light_spectrum='visible')[source]

add a new point spread function

clearOldCalibrations(date=None)[source]

if not only a specific date than remove all except of the youngest calibration

correct(image1, image2=None, bgImages=None, exposure_time=None, light_spectrum=None, threshold=0.2, keep_size=True, date=None, deblur=True)[source]

exposure_time [s]

date -> string e.g. ‘30. Nov 15’ to get a calibration on from date
-> {‘dark current’:‘30. Nov 15’,
‘flat field’:‘15. Nov 15’, ‘lens’:‘14. Nov 15’, ‘noise’:‘01. Nov 15’}
static currentTime()[source]
dates(typ, light=None)[source]
Parameters:
  • typ – type of calibration to look for. See .coeffs.keys() for all types available
  • light (Optional[str]) – restrict to calibrations, done given light source
Returns:

All calibration dates available for given typ

Return type:

list

deleteCoeff(name, date, light=None)[source]
ftype = '.cal'
getCoeff(name, light, date)[source]

try to get calibration for right light source, but use another if they is none existent

getLens(light_spectrum, date)[source]
infos(typ, light=None, date=None)[source]
Parameters:
  • typ – type of calibration to look for. See .coeffs.keys() for all types available
  • date (Optional[str]) – date of calibration
Returns:

all infos available for given typ

Return type:

list

static loadFromFile(path)[source]
overview()[source]
Returns:an overview covering all calibrations infos and shapes
Return type:str
saveToFile(path)[source]
setCamera(camera_name, bit_depth=16)[source]
Parameters:
  • camera_name (str) – Name of the camera
  • bit_depth (int) – depth (bit) of the camera sensor
uncertainty(img=None, light_spectrum=None)[source]

imgProcessor.camera.DarkCurrentMap module

class imgProcessor.camera.DarkCurrentMap.DarkCurrentMap(twoImages, noise_level_function=None, calcVariance=False, **kwargs)[source]

Bases: imgProcessor.utils.baseClasses.Iteratives

Averages given background images removing single time effects

addImg(img, raiseIfConvergence=False)[source]
map()[source]
uncertainty()[source]
uncertaintyMap()[source]
imgProcessor.camera.DarkCurrentMap.averageSameExpTimes(imgs_path)[source]

average background images with same exposure time

imgProcessor.camera.DarkCurrentMap.getDarkCurrentAverages(exposuretimes, imgs)[source]

return exposure times, image averages for each exposure time

imgProcessor.camera.DarkCurrentMap.getDarkCurrentFunction(exposuretimes, imgs, **kwargs)[source]

get dark current function from given images and exposure times

imgProcessor.camera.DarkCurrentMap.getLinearityFunction(expTimes, imgs, mxIntensity=64000, min_ascent=0.001)[source]

returns offset, ascent of image(expTime) = offset + ascent*expTime

imgProcessor.camera.DarkCurrentMap.sortForSameExpTime(expTimes, img_paths, excludeSingleImg=True)[source]

return image paths sorted for same exposure time

imgProcessor.camera.FlatFieldFromImgFit module

class imgProcessor.camera.FlatFieldFromImgFit.FlatFieldFromImgFit(images=None, nstd=3, ksize=None, scale_factor=None)[source]

Bases: object

addImg(i)[source]
flatFieldFromFit()[source]

calculate flatField from 2d-polynomal fit filling all high gradient areas within averaged fit-image

returns flatField, average background level, fitted image, valid indices mask

flatFieldFromFunction()[source]

calculate flatField from fitting vignetting function to averaged fit-image returns flatField, average background level, fitted image, valid indices mask

imgProcessor.camera.FlatFieldFromImgFit.highGrad(arr)[source]

imgProcessor.camera.LensDistortion module

class imgProcessor.camera.LensDistortion.LensDistortion(coeffs={})[source]

Bases: object

class for detecting and correcting the lens distortion within images

based on: http://opencv-python-tutroals.readthedocs.org/en/latest/py_tutorials/py_calib3d/py_calibration/py_calibration.html

addImg(img)[source]

add one chessboard image for detection lens distortion

addImgStream(img)[source]

add images using a continous stream - stop when max number of images is reached

addPoints(points)[source]

add corner points directly instead of extracting them from image points = ( (0,1), (...),... ) [x,y]

calibrate(board_size, method='Chessboard', images=[], max_images=100, sensorSize_mm=None, detect_sensible=False)[source]

sensorSize_mm - (width, height) [mm] Physical size of the sensor

coeffs
correct(image, keepSize=False)[source]

remove lens distortion from given image

distortImage(image)[source]

opposite of ‘correct’

drawChessboard(img=None)[source]

draw a grid fitting to the last added image on this one or an extra image img == None

==False -> draw chessbord on empty image ==img
ftype = 'npz'
getCameraParams()[source]

value positions based on http://docs.opencv.org/modules/imgproc/doc/geometric_transformations.html#cv.InitUndistortRectifyMap

getCoeffStr()[source]

get the distortion coeffs in a formated string

getDeflection(width, height)[source]
getDistortRectifyMap(sizex, sizey)[source]
getUndistortRectifyMap(imgWidth, imgHeight)[source]
readFromFile(filename)[source]

read the distortion coeffs from file

setCameraParams(fx, fy, cx, cy, k1, k2, k3, p1, p2)[source]
setImgShape(shape)[source]

image shape must be known for calculating camera matrix if method==Manual and addPoints is used instead of addImg this method must be called before .coeffs are obtained

standardUncertainties()[source]

returns a list of standard uncertainties for the x and y component: (1x,2x), (1y, 2y), (intensity:None) 1. px-size-changes(due to deflection) 2. reprojection error

undistortPoints(points)[source]
writeToFile(filename, saveOpts=False)[source]

write the distortion coeffs to file saveOpts –> Whether so save calibration options (and not just results)

imgProcessor.camera.NoiseLevelFunction module

imgProcessor.camera.NoiseLevelFunction.boundedFunction(x, minY, ax, ay)[source]

limit [function] to a minimum y value

imgProcessor.camera.NoiseLevelFunction.calcNLF(img, img2=None, signal=None, mn_mx_nbins=None, x=None, averageFn='AAD', signalFromMultipleImages=False)[source]

Calculate the noise level function (NLF) as f(intensity) using one or two image. The approach for this work is published in JPV##########

img2 - 2nd image taken under same conditions
used to estimate noise via image difference
signalFromMultipleImages - whether the signal is an average of multiple
images and not just got from one median filtered image
imgProcessor.camera.NoiseLevelFunction.estimateFromImages(imgs1, imgs2=None, mn_mx=None, nbins=100)[source]

estimate the noise level function as stDev over image intensity from a set of 2 image groups images at the same position have to show the identical setup, so imgs1[i] - imgs2[i] = noise

imgProcessor.camera.NoiseLevelFunction.function(x, ax, ay)[source]

general square root function

imgProcessor.camera.NoiseLevelFunction.oneImageNLF(img, img2=None, signal=None)[source]

Estimate the NLF from one or two images of the same kind

imgProcessor.camera.NoiseLevelFunction.smooth(x, y, weights)[source]

in case the NLF cannot be described by a square root function express it as a group of smoothed splines

imgProcessor.camera.PerspectiveCorrection module

class imgProcessor.camera.PerspectiveCorrection.PerspectiveCorrection(img_shape, obj_height_mm=None, obj_width_mm=None, cameraMatrix=None, distCoeffs=0, do_correctIntensity=False, new_size=None, in_plane=False, cv2_opts={})[source]

Bases: object

camera_position

returns camera position in world coordinates using self.rvec and self.tvec from http://stackoverflow.com/questions/14515200/python-opencv-solvepnp-yields-wrong-translation-vector

correct(img)[source]
...from perspective distortion:
–> perspective transformation –> apply tilt factor (view factor) correction
correctGrid(img, grid)[source]

grid -> array of polylines=((p0x,p0y),(p1x,p1y),,,)

correctPoints(pts)[source]
distort(img, rotX=0, rotY=0, quad=None)[source]

Apply perspective distortion ion self.img angles are in DEG and need to be positive to fit into image

draw3dCoordAxis(img=None, thickness=8)[source]

draw the 3d coordinate axes into given image if image == False:

create an empty image
drawQuad(img=None, quad=None, thickness=30)[source]

Draw the quad into given img

homography
obj_points
pose()[source]
setReference(ref)[source]

ref ... either quad, grid, homography or reference image

quad –> list of four image points(x,y) marking the edges of the quad
to correct

homography –> h. matrix to correct perspective distortion referenceImage –> image of same object without perspective distortion

standardUncertainties(focal_Length_mm, f_number, focusAtYX=None, sigma_best_focus=0, quad_pos_err=0, shape=None, uncertainties=((), (), ()))[source]
focusAtXY - image position with is in focus
if not set it is assumed that the image middle is in focus
sigma_best_focus - standard deviation of the PSF
within the best focus (default blur)
uncertainties - contibutors for standard uncertainty
these need to be perspective transformed to fit the new image shape

imgProcessor.camera.flatFieldFromCalibration module

imgProcessor.camera.flatFieldFromCalibration.flatFieldFromCalibration(bgImages, images, calcStd=False)[source]

returns a flat-field correction map through conditional average of multiple images reduced by a background image

calcStd -> set to True to also return the standard deviation