Skip to content

Commit

Permalink
Merge pull request #91 from swarm-lab/develop
Browse files Browse the repository at this point in the history
Add affine transform matrix computation and string argument for streams.
  • Loading branch information
sjmgarnier authored Aug 12, 2023
2 parents 8d621d6 + 95bde7a commit 6b684bb
Show file tree
Hide file tree
Showing 16 changed files with 209 additions and 34 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ export(fps)
export(frame)
export(full)
export(gaussianBlur)
export(getAffineTransform)
export(getGaborKernel)
export(getPerspectiveTransform)
export(getProp)
Expand Down
28 changes: 23 additions & 5 deletions R/StreamClass.R
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,18 @@
#' @description Function for creating \code{\link{Stream}} objects from video
#' streams.
#'
#' @param index An integer value corresponding to the index of the camera to
#' read a stream from (default: 0; 0 is usually the default webcam on most
#' computers).
#' @param ... Either:
#' \itemize{
#' \item{index: }{An integer value corresponding to the index of the camera
#' to read a stream from (default: 0; 0 is usually the default webcam on
#' most computers). Or...}
#' \item{stream_string: }{...a character string corresponfing to the URL of
#' video stream (eg. protocol://host:port/script_name?script_params|auth)
#' or a GStreamer pipeline string in gst-launch tool format if GStreamer is
#' used as backend API. Note that each video stream or IP camera feed has
#' its own URL scheme. Please refer to the documentation of source stream
#' to know the right URL format.}
#' }
#'
#' @param api A character string corresponding to the API to use for reading the
#' stream from the camera (see Note; default: "ANY").
Expand Down Expand Up @@ -77,8 +86,17 @@
#' }
#'
#' @export
stream <- function(index = 0, api = "ANY") {
new(Stream, index = index, api = api)
stream <- function(..., api = "ANY") {
args <- list(...)

if (!inherits(args[[1]], "character"))
args[[1]] <- as.integer(args[[1]])

if (length(args) > 1)
if (inherits(args[[2]], "character"))
api <- args[[2]]

new(Stream, args[[1]], api = api)
}


Expand Down
2 changes: 1 addition & 1 deletion R/feature.R
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,7 @@ ORBkeypoints <- function(image, mask = NULL, n_features = 500, scale_factor = 1.

st <- switch(score_type,
"HARRIS" = 0,
"FAST" = 0,
"FAST" = 1,
stop("Invalid score type.")
)

Expand Down
45 changes: 45 additions & 0 deletions R/transform.R
Original file line number Diff line number Diff line change
Expand Up @@ -453,6 +453,51 @@ getPerspectiveTransform <- function(from, to, from_dim, to_dim = from_dim) {
}


#' @title Affine Transform
#'
#' @description \code{getAffineTransform} computes the matrix of an affine
#' transform from 4 pairs of corresponding points in a source and destination
#' image.
#'
#' @param from A 4x2 matrix indicating the location (x, y) of 4 points in the
#' source image.
#'
#' @param to A 4x2 matrix indicating the location (x, y) of 4 points in the
#' destination image. The order of the points must correspond to the order in
#' \code{from}.
#'
#' @param from_dim A vector which first two elements indicate the number of rows
#' and columns of the source image.
#'
#' @param to_dim A vector which first two elements indicate the number of rows
#' and columns of the destination image. If not specified, \code{from_dim} will
#' be used as a default.
#'
#' @return A 3x3 matrix.
#'
#' @author Simon Garnier, \email{garnier@@njit.edu}
#'
#' @seealso \code{\link{warpAffine}}
#'
#' @examples
#' from <- matrix(c(1, 1, 2, 5, 6, 5, 5, 1), nrow = 4, byrow = TRUE)
#' to <- matrix(c(1, 1, 1, 5, 5, 5, 5, 1), nrow = 4, byrow = TRUE)
#' getAffineTransform(from, to, c(1080, 1920), c(1080, 1920))
#'
#' @export
getAffineTransform <- function(from, to, from_dim, to_dim = from_dim) {
if (any(dim(from) != c(4, 2)) | any(dim(to) != c(4, 2)))
stop("'from' and 'to' must be 4x2 matrices.")

from[, 1] <- from[, 1] - 1
from[, 2] <- -from[, 2] + from_dim[1]
to[, 1] <- to[, 1] - 1
to[, 2] <- -to[, 2] + from_dim[1] - (from_dim[1] - to_dim[1])

`_getAffineTransform`(from, to)
}


#' @title Perspective Transformation
#'
#' @description \code{warpPerspective} applies a perspective transformation to
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-08T12:51Z
last_built: 2023-08-09T18:21Z
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 @@ -279,6 +279,9 @@
<url>
<loc>https://swarm-lab.github.io/Rvision/reference/gaussianBlur.html</loc>
</url>
<url>
<loc>https://swarm-lab.github.io/Rvision/reference/getAffineTransform.html</loc>
</url>
<url>
<loc>https://swarm-lab.github.io/Rvision/reference/getGaborKernel.html</loc>
</url>
Expand Down
43 changes: 43 additions & 0 deletions man/getAffineTransform.Rd

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

17 changes: 13 additions & 4 deletions man/stream.Rd

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

15 changes: 15 additions & 0 deletions src/Stream.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
class Stream : public Capture {
public:
Stream(int index, std::string api);
Stream(std::string stream_string, std::string api);
bool open(int index, std::string api);
bool openStr(std::string stream_string, std::string api);

private:
};
Expand All @@ -12,9 +14,22 @@ Stream::Stream(int index, std::string api) {
}
}

Stream::Stream(std::string stream_string, std::string api) {
if (!this->cap.open(stream_string, getAPIId(api))) {
Rcpp::stop("Could not open the stream.");
}
}

bool Stream::open(int index, std::string api) {
if (!this->cap.open(index, getAPIId(api)))
Rcpp::stop("Could not open the stream.");

return true;
}

bool Stream::openStr(std::string stream_string, std::string api) {
if (!this->cap.open(stream_string, getAPIId(api)))
Rcpp::stop("Could not open the stream.");

return true;
}
Loading

0 comments on commit 6b684bb

Please sign in to comment.