Skip to content

Commit

Permalink
Merge pull request #87 from swarm-lab/develop
Browse files Browse the repository at this point in the history
Add pyramid resampling functions.
  • Loading branch information
sjmgarnier authored Jul 27, 2023
2 parents 4743cea + 3448412 commit dcf483a
Show file tree
Hide file tree
Showing 12 changed files with 269 additions and 13 deletions.
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,8 @@ export(plotOF)
export(polarToCart)
export(pow)
export(pset)
export(pyrDown)
export(pyrUp)
export(queue)
export(randn)
export(randu)
Expand Down
113 changes: 113 additions & 0 deletions R/filters.R
Original file line number Diff line number Diff line change
Expand Up @@ -1454,4 +1454,117 @@ getStructuringElement <- function(k_shape = "rectangle", k_height = 5, k_width =
"ellipse" = 2,
stop("This is not a valid kernel shape"))
`_getStructuringElement`(sh, k_width, k_height, anchor[1], anchor[2])
}


#' @title Pyramid Downsampling
#'
#' @description \code{prDown} blurs an image and then downsamples it.
#'
#' @param image An \code{\link{Image}} object.
#'
#' @param target The location where the results should be stored. It can take 2
#' values:
#' \itemize{
#' \item{"new":}{a new \code{\link{Image}} object is created and the results
#' are stored inside (the default).}
#' \item{An \code{\link{Image}} object:}{the results are stored in another
#' existing \code{\link{Image}} object. This is fast and will not replace the
#' content of \code{image} but will replace that of \code{target}. Note that
#' if \code{target} does not have the same number of channels, and bit depth
#' as \code{image}, an error may be thrown. The dimensions of \code{target}
#' must satisfy the following conditions:
#' \itemize{
#' \item{}{\code{ abs(ncol(target) * 2 - ncol(image)) <= 2 }}
#' \item{}{\code{ abs(nrow(target) * 2 - nrow(image)) <= 2 }}
#' }
#' }
#' }
#'
#' @return If \code{target="new"}, the function returns an \code{\link{Image}}
#' object. If \code{target} is an \code{\link{Image}} object, the function
#' returns nothing and modifies that \code{\link{Image}} object in place.
#'
#' @author Simon Garnier, \email{garnier@@njit.edu}
#'
#' @seealso \code{\link{pyrUp}}
#'
#' @examples
#' balloon <- image(system.file("sample_img/balloon1.png", package = "Rvision"))
#' small_balloon <- pyrDown(balloon)
#'
#' @export
pyrDown <- function(image, target = "new") {
if (!isImage(image))
stop("This is not an Image object.")

if (isImage(target)) {
if (target$nchan() != image$nchan() | target$depth() != image$depth())
stop("target is not of the same type and depth as image.")

`_pyrDown`(image, target)
} else if (target == "new") {
out <- resize(image, (image$nrow() + 1) / 2, (image$ncol() + 1) / 2)
`_pyrDown`(image, out)
out
} else {
stop("Invalid target.")
}
}


#' @title Pyramid Upsampling
#'
#' @description \code{prUp} upsamples an image and then blurs it.
#'
#' @param image An \code{\link{Image}} object.
#'
#' @param target The location where the results should be stored. It can take 2
#' values:
#' \itemize{
#' \item{"new":}{a new \code{\link{Image}} object is created and the results
#' are stored inside (the default).}
#' \item{An \code{\link{Image}} object:}{the results are stored in another
#' existing \code{\link{Image}} object. This is fast and will not replace the
#' content of \code{image} but will replace that of \code{target}. Note that
#' if \code{target} does not have the same number of channels, and bit depth
#' as \code{image}, an error may be thrown. The dimensions of \code{target}
#' must satisfy the following conditions:
#' \itemize{
#' \item{}{\code{ abs(ncol(target) - ncol(image) * 2) <= ncol(target) \%\% 2 }}
#' \item{}{\code{ abs(nrow(target) - nrow(image) * 2) <= nrow(target) \%\% 2 }}
#' }
#' }
#' }
#'
#' @return If \code{target="new"}, the function returns an \code{\link{Image}}
#' object. If \code{target} is an \code{\link{Image}} object, the function
#' returns nothing and modifies that \code{\link{Image}} object in place.
#'
#' @author Simon Garnier, \email{garnier@@njit.edu}
#'
#' @seealso \code{\link{pyrDown}}
#'
#' @examples
#' balloon <- image(system.file("sample_img/balloon1.png", package = "Rvision"))
#' big_balloon <- pyrUp(balloon)
#'
#' @export
pyrUp <- function(image, target = "new") {
if (!isImage(image))
stop("This is not an Image object.")

if (isImage(target)) {
if (target$nchan() != image$nchan() | target$depth() != image$depth())
stop("target is not of the same type and depth as image.")

`_pyrUp`(image, target)
} else if (target == "new") {
out <- zeros(image$nrow() * 2, image$ncol() * 2, image$nchan(),
image$depth(), image$space)
`_pyrUp`(image, out)
out
} else {
stop("Invalid target.")
}
}
4 changes: 2 additions & 2 deletions docs/pkgdown.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
pandoc: 3.1.5
pandoc: 3.1.6
pkgdown: 2.0.7
pkgdown_sha: ~
articles:
Expand All @@ -9,7 +9,7 @@ articles:
z5_gpu: z5_gpu.html
z6_queue: z6_queue.html
z7_stack: z7_stack.html
last_built: 2023-07-24T06:03Z
last_built: 2023-07-25T15:44Z
urls:
reference: https://swarm-lab.github.io/Rvision/reference
article: https://swarm-lab.github.io/Rvision/articles
Expand Down
2 changes: 1 addition & 1 deletion docs/reference/convexHull.html

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

8 changes: 4 additions & 4 deletions docs/reference/fitEllipse.html

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

10 changes: 10 additions & 0 deletions docs/reference/index.html

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

8 changes: 4 additions & 4 deletions docs/reference/minAreaRect.html

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

6 changes: 6 additions & 0 deletions docs/sitemap.xml
Original file line number Diff line number Diff line change
Expand Up @@ -444,6 +444,12 @@
<url>
<loc>https://swarm-lab.github.io/Rvision/reference/pset.html</loc>
</url>
<url>
<loc>https://swarm-lab.github.io/Rvision/reference/pyrDown.html</loc>
</url>
<url>
<loc>https://swarm-lab.github.io/Rvision/reference/pyrUp.html</loc>
</url>
<url>
<loc>https://swarm-lab.github.io/Rvision/reference/queue.html</loc>
</url>
Expand Down
48 changes: 48 additions & 0 deletions man/pyrDown.Rd

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

48 changes: 48 additions & 0 deletions man/pyrUp.Rd

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

30 changes: 29 additions & 1 deletion src/filters.h
Original file line number Diff line number Diff line change
Expand Up @@ -256,4 +256,32 @@ arma::Mat< float > _getStructuringElement(int k_shape, int k_width, int k_height
// k = cv::getGaussianKernel(ksize, sigma, 6);
// cv2arma(k, out);
// return out;
// }
// }

void _pyrDown(Image& image, Image& target) {
if (image.GPU) {
if (target.GPU)
return cv::pyrDown(image.uimage, target.uimage, cv::Size(target.uimage.cols, target.uimage.rows));

return cv::pyrDown(image.uimage, target.image, cv::Size(target.image.cols, target.image.rows));
}

if (target.GPU)
cv::pyrDown(image.image, target.uimage, cv::Size(target.uimage.cols, target.uimage.rows));

return cv::pyrDown(image.image, target.image, cv::Size(target.image.cols, target.image.rows));
}

void _pyrUp(Image& image, Image& target) {
if (image.GPU) {
if (target.GPU)
return cv::pyrUp(image.uimage, target.uimage, cv::Size(target.uimage.cols, target.uimage.rows));

return cv::pyrUp(image.uimage, target.image, cv::Size(target.image.cols, target.image.rows));
}

if (target.GPU)
cv::pyrUp(image.image, target.uimage, cv::Size(target.uimage.cols, target.uimage.rows));

return cv::pyrUp(image.image, target.image, cv::Size(target.image.cols, target.image.rows));
}
3 changes: 2 additions & 1 deletion src/visionModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,6 @@ RCPP_MODULE(methods_Morphology) {

#include "filters.h"
RCPP_MODULE(methods_Filters) {

function("_filter2D", &_filter2D, List::create(_["image"], _["kernel"], _["target"]), "");
function("_sepFilter2D", &_sepFilter2D, List::create(_["image"], _["kernel_x"],
_["kernel_y"], _["target"]), "");
Expand Down Expand Up @@ -277,6 +276,8 @@ RCPP_MODULE(methods_Filters) {
_["anchor_x"], _["anchor_y"]), "");
// function("_getGaussianKernel", &_getGaussianKernel, List::create(_["ksize"],
// _["sigma"]), "");
function("_pyrDown", &_pyrDown, List::create(_["image"], _["target"]), "");
function("_pyrUp", &_pyrUp, List::create(_["image"], _["target"]), "");
}

#include "display.h"
Expand Down

0 comments on commit dcf483a

Please sign in to comment.