Skip to content

Commit

Permalink
add image.CannyEdges
Browse files Browse the repository at this point in the history
  • Loading branch information
jwijffels committed Feb 27, 2017
1 parent bbcb134 commit ff1212e
Show file tree
Hide file tree
Showing 22 changed files with 17,233 additions and 0 deletions.
2 changes: 2 additions & 0 deletions image.CannyEdges/.Rbuildignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
^.*\.Rproj$
^\.Rproj\.user$
7 changes: 7 additions & 0 deletions image.CannyEdges/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
.Rproj.user
.Rhistory
.RData
.Ruserdata
src/*.o
src/*.so
src/*.dll
16 changes: 16 additions & 0 deletions image.CannyEdges/DESCRIPTION
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
Package: image.CannyEdges
Type: Package
Title: Implementation of the Canny Edge Detector for Images
Version: 1.0
Author: c(person("Jan", "Wijffels", role = c("aut", "cre", "cph"), email = "[email protected]"), person("BNOSAC", role = "cph"), person("Rafael Grompone von Gioi", role = "cph", email = "[email protected]"), person("Vincent Maioli", role = "cph", email = "[email protected]"))
Maintainer: Jan Wijffels <[email protected]>
Description: An implementation of the Canny Edge Detector. The package provides in interface to the algorithm available at <https://github.com/Neseb/canny>.
License: GPL-3
Encoding: UTF-8
Imports:
Rcpp (>= 0.12.9)
LinkingTo: Rcpp
Suggests:
pixmap
RoxygenNote: 6.0.1
SystemRequirements: libpng, fftw3
9 changes: 9 additions & 0 deletions image.CannyEdges/NAMESPACE
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Generated by roxygen2: do not edit by hand

S3method(plot,image_canny)
S3method(print,image_canny)
export(image_canny_edge_detector)
importFrom(Rcpp,evalCpp)
importFrom(graphics,plot)
importFrom(graphics,rasterImage)
useDynLib(image.CannyEdges)
7 changes: 7 additions & 0 deletions image.CannyEdges/R/RcppExports.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Generated by using Rcpp::compileAttributes() -> do not edit by hand
# Generator token: 10BE3573-1514-4C36-9D1C-5A225CD40393

canny_edge_detector <- function(image, X, Y, s = 2, low_thr = 3, high_thr = 10, accGrad = FALSE) {
.Call('image_CannyEdges_canny_edge_detector', PACKAGE = 'image.CannyEdges', image, X, Y, s, low_thr, high_thr, accGrad)
}

77 changes: 77 additions & 0 deletions image.CannyEdges/R/canny_edges_detector.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
#' @title Canny Edge Detector for Images
#' @description Canny Edge Detector for Images. See \url{https://en.wikipedia.org/wiki/Canny_edge_detector}.
#' Adapted from \url{https://github.com/Neseb/canny}.
#' @param x a matrix of image pixel values in the 0-255 range.
#' @param s sigma, the Gaussian filter variance. Defaults to 2.
#' @param low_thr lower threshold value of the algorithm. Defaults to 3.
#' @param high_thr upper threshold value of the algorithm. Defaults to 10
#' @param accGrad logical indicating to trigger higher-order gradient
#' @return a list with element edges which is a matrix with values 0 or 255 indicating
#' in the same dimension of \code{x}. Next to that
#' the list also contains the input parameters s, low_thr, high_thr and accGrad,
#' the number of rows (nx) and columns of the image (ny) and the number of pixels which
#' have value 255 (pixels_nonzero).
#' @export
#' @examples
#' library(pixmap)
#' imagelocation <- system.file("extdata", "chairs.pgm", package="image.CannyEdges")
#' image <- read.pnm(file = imagelocation, cellres = 1)
#' x <- image@grey * 255
#'
#' edges <- image_canny_edge_detector(x)
#' edges
#' plot(edges)
#'
#' \dontrun{
#' ##
#' ## image_canny_edge_detector expects a matrix as input
#' ## if you have a jpg/png/... convert it to pgm first or take the r/g/b channel
#' f <- tempfile(fileext = ".pgm")
#' library(magick)
#' x <- image_read(system.file("extdata", "atomium.jpg", package="image.CannyEdges"))
#' x <- image_convert(x, format = "pgm", depth = 8)
#' image_write(x, path = f, format = "pgm")
#'
#' image <- read.pnm(f, cellres = 1)
#' edges <- image_canny_edge_detector(image@grey * 255)
#' plot(edges)
#' }
image_canny_edge_detector <- function(x, s = 2, low_thr = 3, high_thr = 10, accGrad = TRUE) {
x <- canny_edge_detector(as.integer(x), nrow(x), ncol(x), s, low_thr, high_thr, accGrad)
class(x) <- "image_canny"
x
}


#' @export
print.image_canny <- function(x, ...){
cat("Canny edge detector", sep = "\n")
cat(sprintf(" %s x %s matrix", x$nx, x$ny), sep = "\n")
cat(sprintf(" number of pixels on edge %s", x$pixels_nonzero), sep = "\n")
cat(sprintf(" sigma %s", x$s), sep = "\n")
cat(sprintf(" low_thr %s", x$low_thr), sep = "\n")
cat(sprintf(" high_thr %s", x$high_thr), sep = "\n")
cat(sprintf(" accGrad %s", x$accGrad), sep = "\n")
}



#' @title Plot the result of the Canny Edge Detector
#' @description Plot the result of \url{image_canny_edge_detector}
#' @param x an object of class image_canny as returned by \code{\link{image_canny_edge_detector}}
#' @param ... further arguments passed on to plot, except type, xlab and ylab which are set inside the function
#' @method plot image_canny
#' @return invisible()
#' @export
#' @examples
#' library(pixmap)
#' imagelocation <- system.file("extdata", "chairs.pgm", package="image.CannyEdges")
#' image <- read.pnm(file = imagelocation, cellres = 1)
#' edges <- image_canny_edge_detector(image@grey * 255)
#' plot(edges)
plot.image_canny<- function(x, ...){
ok <- grDevices::as.raster(x$edges, max = 255)
plot(c(1, x$nx), c(1, x$ny), type = "n", xlab = "", ylab = "", ...)
rasterImage(ok, 0, 0, x$nx, x$ny)
invisible()
}
11 changes: 11 additions & 0 deletions image.CannyEdges/R/pkg.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#' @title Implementation of the Canny Edge Detector for Images
#' @description Canny Edge Detector for Images. See \url{https://en.wikipedia.org/wiki/Canny_edge_detector}.
#' Adapted from \url{https://github.com/Neseb/canny}.
#' @name image.CannyEdges-package
#' @aliases image.CannyEdges-package
#' @docType package
#' @importFrom Rcpp evalCpp
#' @importFrom graphics plot rasterImage
#' @useDynLib image.CannyEdges
#' @seealso \link{image_canny_edge_detector}
NULL
31 changes: 31 additions & 0 deletions image.CannyEdges/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# image.CannyEdges - Canny Edge Detector for Images

The **image.CannyEdges** R package detects edges in images.

- It contains 1 main function **image_canny_edge_detector**. If you give it an image matrix with grey scale values in the 0-255 range, it will find edges in the image.

## Examples

Read in an image with values in the 0-255 range (pgm image: http://netpbm.sourceforge.net/doc/pgm.html)

```r
library(image.CannyEdges)
library(pixmap)
imagelocation <- system.file("extdata", "chairs.pgm", package="image.CannyEdges")
image <- read.pnm(file = imagelocation, cellres = 1)
x <- image@grey * 255

edges <- image_canny_edge_detector(x)
edges
plot(edges)
```

## Installation

See instructions at https://github.com/bnosac/image

## Support in image recognition

Need support in image recognition?
Contact BNOSAC: http://www.bnosac.be

21 changes: 21 additions & 0 deletions image.CannyEdges/image.CannyEdges.Rproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
Version: 1.0

RestoreWorkspace: Default
SaveWorkspace: Default
AlwaysSaveHistory: Default

EnableCodeIndexing: Yes
UseSpacesForTab: Yes
NumSpacesForTab: 2
Encoding: UTF-8

RnwWeave: Sweave
LaTeX: pdfLaTeX

AutoAppendNewline: Yes
StripTrailingWhitespace: Yes

BuildType: Package
PackageUseDevtools: Yes
PackageInstallArgs: --no-multiarch --with-keep.source
PackageRoxygenize: rd,collate,namespace
Binary file added image.CannyEdges/inst/extdata/atomium.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit ff1212e

Please sign in to comment.