Skip to content

Commit

Permalink
Add undistort function for individual coordinates.
Browse files Browse the repository at this point in the history
  • Loading branch information
sjmgarnier committed Oct 26, 2023
1 parent 6549db8 commit 7c45e59
Show file tree
Hide file tree
Showing 214 changed files with 1,163 additions and 373 deletions.
2 changes: 2 additions & 0 deletions .Rbuildignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,5 @@ ci_script
^docs$
^pkgdown$
^codecov\.yml$
^doc$
^Meta$
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,5 @@ src/.DS_Store
tmp.def
*.insyncdl
docs
/doc/
/Meta/
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,7 @@ export(threshold)
export(tile)
export(timelapse)
export(undistort)
export(undistortPoints)
export(video)
export(videoStack)
export(videoWriter)
Expand Down
91 changes: 68 additions & 23 deletions R/calib3d.R
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ cornerSubPix <- function(image, corners, win_size = c(11, 11), zero_zone = c(-1,
#'
#' @examples
#' # See the help vignette:
#' \dontrun{ vignette("", package = "Rvision") }
#' \dontrun{ vignette("z8_calib", package = "Rvision") }
#'
#' @export
calibrateCamera <- function(ref_points, img_points, nrow, ncol, fixed_point = 1,
Expand Down Expand Up @@ -251,7 +251,7 @@ calibrateCamera <- function(ref_points, img_points, nrow, ncol, fixed_point = 1,
#'
#' @examples
#' # See the help vignette:
#' \dontrun{ vignette("", package = "Rvision") }
#' \dontrun{ vignette("z8_calib", package = "Rvision") }
#'
#' @export
getOptimalNewCameraMatrix <- function(camera_matrix, dist_coefs, nrow, ncol,
Expand All @@ -267,10 +267,12 @@ getOptimalNewCameraMatrix <- function(camera_matrix, dist_coefs, nrow, ncol,
}


#' @title Compensate for Lens Distortion
#' @title Transform an Image to Compensate for Lens Distortion
#'
#' @description \code{undistort} transforms an image to compensate for radial
#' and tangential lens distortion.
#' and tangential lens distortion.
#'
#' @param image An \code{\link{Image}} object.
#'
#' @param camera_matrix A 3x3 camera intrinsic matrix as returned by
#' \code{\link{calibrateCamera}}.
Expand All @@ -280,7 +282,7 @@ getOptimalNewCameraMatrix <- function(camera_matrix, dist_coefs, nrow, ncol,
#'
#' @param new_camera_matrix A 3x3 camera intrinsic matrix as returned by
#' \code{\link{getOptimalNewCameraMatrix}} if you chose to execute this
#' optional step (default: NULL).
#' optional step (default: \code{camera_matrix}).
#'
#' @param target The location where the results should be stored. It can take 3
#' values:
Expand All @@ -300,15 +302,15 @@ getOptimalNewCameraMatrix <- function(camera_matrix, dist_coefs, nrow, ncol,
#'
#' @author Simon Garnier, \email{garnier@@njit.edu}
#'
#' @seealso \code{\link{findChessboardCorners}}, \code{\link{cornerSubPix}},
#' \code{\link{calibrateCamera}}, \code{\link{getOptimalNewCameraMatrix}}
#' @seealso \code{\link{calibrateCamera}}, \code{\link{getOptimalNewCameraMatrix}},
#' \code{\link{undistortPoints}}
#'
#' @examples
#' # See the help vignette:
#' \dontrun{ vignette("", package = "Rvision") }
#' \dontrun{ vignette("z8_calib", package = "Rvision") }
#'
#' @export
undistort <- function(image, camera_matrix, dist_coefs, new_camera_matrix = NULL,
undistort <- function(image, camera_matrix, dist_coefs, new_camera_matrix = camera_matrix,
target = "new") {
if (!isImage(image))
stop("'image' is not an Image object.")
Expand All @@ -319,29 +321,72 @@ undistort <- function(image, camera_matrix, dist_coefs, new_camera_matrix = NULL
if (!all(dim(new_camera_matrix) == 3))
stop("'new_camera_matrix' should have exactly 3 rows and 3 columns.")

if (!all(dim(dist_coefs) == c(1, 5)))
stop("'dist_coefs' should have exactly 1 row and 5 columns.")
if (nrow(dist_coefs) != 1)
stop("'dist_coefs' should have exactly 1 row.")

if (!(ncol(dist_coefs) %in% c(4, 5, 8, 12, 14)))
stop("'dist_coefs' should have either 4, 5, 8, 12, or 14 columns.")

if (isImage(target)) {
if (identical(image, target))
stop("'image' and 'target' cannot be the same Image object.")

if (is.null(new_camera_matrix)) {
`_undistortNoNCM`(image, camera_matrix, dist_coefs, target)
} else {
`_undistort`(image, camera_matrix, dist_coefs, new_camera_matrix, target)
}
`_undistort`(image, camera_matrix, dist_coefs, new_camera_matrix, target)
} else if (target == "new") {
out <- zeros(image$nrow(), image$ncol(), image$nchan(), image$depth(), image$space)

if (is.null(new_camera_matrix)) {
`_undistortNoNCM`(image, camera_matrix, dist_coefs, out)
} else {
`_undistort`(image, camera_matrix, dist_coefs, new_camera_matrix, out)
}

`_undistort`(image, camera_matrix, dist_coefs, new_camera_matrix, out)
out
} else {
stop("Invalid target.")
}
}


#' @title Transform Coordinates to Compensate for Lens Distortion
#'
#' @description \code{undistortPoints} transforms a set of coordinates
#' representing points in an image to compensate for radial and tangential lens
#' distortion.
#'
#' @param points A 2xN matrix of X/Y coordinates.
#'
#' @param camera_matrix A 3x3 camera intrinsic matrix as returned by
#' \code{\link{calibrateCamera}}.
#'
#' @param dist_coefs A single row matrix with 4, 5, 8, 12 or 14 elements as
#' returned by \code{\link{calibrateCamera}}.
#'
#' @param new_camera_matrix A 3x3 camera intrinsic matrix as returned by
#' \code{\link{getOptimalNewCameraMatrix}} if you chose to execute this
#' optional step (default: \code{camera_matrix}).
#'
#' @return A 2xN matrix of transformed X/Y coordinates.
#'
#' @author Simon Garnier, \email{garnier@@njit.edu}
#'
#' @seealso \code{\link{undistort}}, \code{\link{calibrateCamera}},
#' \code{\link{getOptimalNewCameraMatrix}}
#'
#' @examples
#' # See the help vignette:
#' \dontrun{ vignette("z8_calib", package = "Rvision") }
#'
#' @export
undistortPoints <- function(points, camera_matrix, dist_coefs, new_camera_matrix = camera_matrix) {
if (!all(dim(camera_matrix) == 3))
stop("'camera_matrix' should have exactly 3 rows and 3 columns.")

if (!all(dim(new_camera_matrix) == 3))
stop("'new_camera_matrix' should have exactly 3 rows and 3 columns.")

if (nrow(dist_coefs) != 1)
stop("'dist_coefs' should have exactly 1 row.")

if (!(ncol(dist_coefs) %in% c(4, 5, 8, 12, 14)))
stop("'dist_coefs' should have either 4, 5, 8, 12, or 14 columns.")

if (ncol(points) != 2)
stop("'points' should have exactly 2 columns.")

`_undistortPoints`(points, camera_matrix, dist_coefs, new_camera_matrix)
}
1 change: 1 addition & 0 deletions docs/404.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions docs/LICENSE-text.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions docs/articles/index.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions docs/articles/z1_install.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions docs/articles/z2_io.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions docs/articles/z3_basic.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions docs/articles/z4_inplace.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions docs/articles/z5_gpu.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions docs/articles/z6_queue.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions docs/authors.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions docs/index.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions docs/news/index.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion docs/pkgdown.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ articles:
z5_gpu: z5_gpu.html
z6_queue: z6_queue.html
z7_stack: z7_stack.html
last_built: 2023-10-25T14:32Z
z8_calib: z8_calib.html
last_built: 2023-10-26T21:58Z
urls:
reference: https://swarm-lab.github.io/Rvision/reference
article: https://swarm-lab.github.io/Rvision/articles
Expand Down
1 change: 1 addition & 0 deletions docs/reference/CLAHE.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions docs/reference/Image-class.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions docs/reference/LUT.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions docs/reference/Queue-class.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions docs/reference/Rvision.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions docs/reference/RvisionAck.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions docs/reference/Stream-class.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions docs/reference/Video-class.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions docs/reference/VideoWriter-class.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions docs/reference/adaptiveThreshold.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions docs/reference/addWeighted.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions docs/reference/anisotropicDiffusion.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions docs/reference/api.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions docs/reference/arcLength.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 7c45e59

Please sign in to comment.