项目作者: jc211

项目描述 :
Image distortion functions ported from OpenCV
高级语言: Julia
项目地址: git://github.com/jc211/ImageDistortion.jl.git
创建时间: 2019-01-17T01:07:51Z
项目社区:https://github.com/jc211/ImageDistortion.jl

开源协议:MIT License

下载


Image Distortion

Utility functions to distort and undistort an image or a set of points. The code is for the most part ported over from OpenCV to native Julia.

Installation

  1. Pkg.add("https://github.com/jc211/ImageDistortion.jl")
  2. using ImageDistortion

Distortion Models

OpenCV implements the following distortion model

Distortion Equations

This implementation currently only supports the k1, k2, k3, p1, p2 parameters. They are represented by distcoeffs where a sample distcoeffs is in the form

  1. distcoeffs = [k1, k2, p1, p2, k3]

Overview

Overview

In general, this package has functions to move around the blocks illustrated above.

Usage

Distorting Points

The function distortpoints takes a matrix of shape (n,2) representing points (e.g. [0 0; 10 15; 32 10]) and firstly undoes the linear distortion provided by newcameramatrix and subsequently applies nonlinear distortion by distcoeffs, followed by a linear distortion by cameramatrix

  1. points = [10 23; 100 100.3; 23.1 20.5]
  2. distortedpoints = distortpoints(points, cameramatrix=[517.3 0 318.6; 0 516.5 244.3; 0 0 1], distcoeffs=[0.2624, -0.9531, -0.0054, 0.0026, 1.1633], cameramatrix=[517.3 0 318.6; 0 516.5 244.3; 0 0 1])

Undistorting Points

The function undistortpoints takes a matrix of shape (n,2) representing points (e.g. [0 0; 10 15; 32 10]) and undoes the linear distortion given by cameramatrix, undoes the nonlinear distortion by distcoeffs, and optionally reapplies a linear distortion by newcameramatrix

  1. points = [10 23; 100 100.3; 23.1 20.5]
  2. undistortedpoints = undistortpoints(points, cameramatrix=[517.3 0 318.6; 0 516.5 244.3; 0 0 1], distcoeffs=[0.2624, -0.9531, -0.0054, 0.0026, 1.1633])

Get Optimal Camera Matrix

The function get_optimalnewcameramatrix outputs a new camera matrix that transforms an undistorted image (in normalized coordinates) to one that contains all the information in the original source image if the parameter alpha = 0, or to one that has no missing information (i.e. no black parts in the image) if alpha = 1. A value of alpha in between does some mixture of the two extremes.

  1. K_original = [517.3 0 318.6; 0 516.5 244.3; 0 0 1] # camera calibration of raw image
  2. d_orignal = [0.2624, -0.9531, -0.0054, 0.0026, 1.1633]; # distortion coefficients of raw image
  3. newcameramatrix = get_optimalnewcameramatrix(cameramatrix=K_original, distcoeffs=d_orignal, imgsize=(640,480), alpha=0, newimgsize=(640,480))

Precompute Distortion Maps

The function init_undistortrectifymap precomputes the map shown above.

  1. K_original = [517.3 0 318.6; 0 516.5 244.3; 0 0 1] # camera calibration of raw image
  2. d_orignal = [0.2624, -0.9531, -0.0054, 0.0026, 1.1633]; # distortion coefficients of raw image
  3. newcameramatrix = get_optimalnewcameramatrix(cameramatrix=K_original, distcoeffs=d_orignal, imgsize=(640,480), alpha=0, newimgsize=(640,480))
  4. xmap, ymap = init_undistortrectifymap(cameramatrix=K_original, distcoeffs=d_orignal, newcameramatrix=newcameramatrix, imgsize=(640,480))

Remap

The function remap uses bilinear interpolation to generate the domain of the maps which are generally computed by init_undistortrectifymap

  1. K_original = [517.3 0 318.6; 0 516.5 244.3; 0 0 1] # camera calibration of raw image
  2. d_orignal = [0.2624, -0.9531, -0.0054, 0.0026, 1.1633]; # distortion coefficients of raw image
  3. newcameramatrix = get_optimalnewcameramatrix(cameramatrix=K_original, distcoeffs=d_orignal, imgsize=(640,480), alpha=0, newimgsize=(640,480))
  4. xmap, ymap = init_undistortrectifymap(cameramatrix=K_original, distcoeffs=d_orignal, newcameramatrix=newcameramatrix, imgsize=(640,480))
  5. newimg = remap(src, xmap, ymap)