Skip to content

Commit

Permalink
Merge pull request #89 from swarm-lab/develop
Browse files Browse the repository at this point in the history
Add approxPolyDP.
  • Loading branch information
sjmgarnier authored Aug 9, 2023
2 parents 41773e7 + 66deed7 commit 8d621d6
Show file tree
Hide file tree
Showing 12 changed files with 161 additions and 27 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ export(add)
export(addWeighted)
export(anisotropicDiffusion)
export(api)
export(approxPolyDP)
export(arcLength)
export(autothreshold)
export(bilateralFilter)
Expand Down
2 changes: 1 addition & 1 deletion R/feature.R
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ goodFeaturesToTrack <- function(image, max_corners, quality_level, min_distance,
if (!all(mask$dim()[1:2] == image$dim()[1:2]))
stop("mask does not have the same dimensions as image.")

if (mask$depth() != 1)
if (mask$depth() != "8U")
stop("mask is not an 8U 'Image' object.")
}

Expand Down
51 changes: 51 additions & 0 deletions R/shape.R
Original file line number Diff line number Diff line change
Expand Up @@ -881,6 +881,57 @@ arcLength <- function(curve, closed = TRUE) {
}


#' @title Calculate a Contour Perimeter or a Curve Length
#'
#' @description \code{approxPolyDP} approximates a curve or a polygon with
#' another curve/polygon with less vertices so that the distance between them
#' is less or equal to the specified precision. It uses the Douglas-Peucker
#' algorithm.
#'
#' @param curve An m x 2 matrix of 2D coordinates.
#'
#' @param epsilon A numeric specifying the approximation accuracy. This is the
#' maximum distance between the original curve and its approximation.
#'
#' @param closed A logical indicating whether the curve is closed (perimeter) or
#' not (default: TRUE).
#'
#' @return A matrix with two columns:
#' \itemize{
#' \item{"x": }{the x coordinates of the approximated curve.}
#' \item{"y": }{the y coordinates of the approximated curve.}
#' }
#'
#' @author Simon Garnier, \email{garnier@@njit.edu}
#'
#' @seealso \code{\link{findContours}}
#'
#' @references Douglas, D. H., & Peucker, T. K. (1973). ALGORITHMS FOR THE
#' REDUCTION OF THE NUMBER OF POINTS REQUIRED TO REPRESENT A DIGITIZED LINE OR
#' ITS CARICATURE. Cartographica: The International Journal for Geographic
#' Information and Geovisualization, 10(2), 112–122.
#' doi:10.3138/FM57-6770-U75U-7727
#'
#' @examples
#' dots <- image(system.file("sample_img/dots.jpg", package = "Rvision"))
#' dots_gray <- changeColorSpace(dots, "GRAY")
#' dots_bin <- dots_gray < 200
#' contours <- findContours(dots_bin)
#' ix <- contours$contours[, 1] == 0
#' approxPolyDP(contours$contours[ix, 2:3], 10)
#'
#' @export
approxPolyDP <- function(curve, epsilon, closed = TRUE) {
if (!is.matrix(curve))
stop("curve must be a m x 2 matrix.")

if (ncol(curve) != 2)
stop("curve must be a m x 2 matrix.")

`_approxPolyDP`(curve, epsilon, closed)
}


#' @title Pixel Values Along a Line Segment
#'
#' @description \code{improfile} finds all pixels intersected by a line segment
Expand Down
2 changes: 1 addition & 1 deletion docs/pkgdown.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ articles:
z5_gpu: z5_gpu.html
z6_queue: z6_queue.html
z7_stack: z7_stack.html
last_built: 2023-08-04T10:50Z
last_built: 2023-08-08T12:51Z
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.

29 changes: 17 additions & 12 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.

3 changes: 3 additions & 0 deletions docs/sitemap.xml
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,9 @@
<url>
<loc>https://swarm-lab.github.io/Rvision/reference/api.html</loc>
</url>
<url>
<loc>https://swarm-lab.github.io/Rvision/reference/approxPolyDP.html</loc>
</url>
<url>
<loc>https://swarm-lab.github.io/Rvision/reference/arcLength.html</loc>
</url>
Expand Down
52 changes: 52 additions & 0 deletions man/approxPolyDP.Rd

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

29 changes: 25 additions & 4 deletions src/shape.h
Original file line number Diff line number Diff line change
Expand Up @@ -354,14 +354,35 @@ Rcpp::List _minAreaRect(arma::Mat< float > points) {
}

double _arcLength(Rcpp::NumericMatrix curve, bool closed) {
std::vector< cv::Point > contourpoints(curve.nrow());
std::vector< cv::Point > curvepoints(curve.nrow());

for (int i = 0; i < curve.nrow(); i++) {
contourpoints[i].x = curve(i, 0);
contourpoints[i].y = curve(i, 1);
curvepoints[i].x = curve(i, 0);
curvepoints[i].y = curve(i, 1);
}

return cv::arcLength(contourpoints, closed);
return cv::arcLength(curvepoints, closed);
}

Rcpp::NumericMatrix _approxPolyDP(Rcpp::NumericMatrix curve, double epsilon, bool closed) {
std::vector< cv::Point > curvepoints(curve.nrow());
std::vector< cv::Point > approxpoints;

for (int i = 0; i < curve.nrow(); i++) {
curvepoints[i].x = curve(i, 0);
curvepoints[i].y = curve(i, 1);
}

cv::approxPolyDP(curvepoints, approxpoints, epsilon, closed);
Rcpp::NumericMatrix out(approxpoints.size(), 2);
colnames(out) = Rcpp::CharacterVector::create("x", "y");

for (int i = 0; i < approxpoints.size(); i++) {
out(i, 0) = approxpoints[i].x;
out(i, 1) = approxpoints[i].y;
}

return out;
}

Rcpp::NumericMatrix _pline(Image& image, Rcpp::NumericVector xi,
Expand Down
1 change: 1 addition & 0 deletions src/visionModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,7 @@ RCPP_MODULE(methods_Shape) {
_["image2"], _["method"]), "");
function("_minAreaRect", &_minAreaRect, List::create(_["points"]), "");
function("_arcLength", &_arcLength, List::create(_["curve"], _["closed"]), "");
function("_approxPolyDP", &_approxPolyDP, List::create(_["curve"], _["epsilon"], _["closed"]), "");
function("_pline", &_pline, List::create(_["image"], _["xi"], _["yi"],
_["connectivity"], _["leftToRight"]), "");
}
Expand Down

0 comments on commit 8d621d6

Please sign in to comment.