Giter Club home page Giter Club logo

Comments (7)

jwijffels avatar jwijffels commented on August 19, 2024 1

I understand that you want to include this in the package but I would prefer to have then a separate method as in

image_contour_detector <- function(x, Q=2.0, ...){
  UseMethod("image_contour_detector")
}

image_contour_detector.matrix <- function(x, Q, ...){
}

image_contour_detector.RasterLayer <- function(x, Q, ...){
}

image_contour_detector.SpatialGridDataFrame <- function(x, Q, ...){
}

In general there is certainly a looot of overlap possible between computer vision and spatial communities. The first thing on my mind would be a package which easily converts images between these 2 (especially covering things like r/g/b and that the topleft is (0,0) and regular R has bottomleft as (0,0).
This last should probably be in a different package so that it can easily switch between representations of R package magick, opencv, plain base arrays, sp, sf, raster... objects, HCR objects, textline objects andsoforth.

from image.

jwijffels avatar jwijffels commented on August 19, 2024

There is code at https://github.com/bnosac/image/blob/master/image.ContourDetector/R/contour_detector.R#L85:L88 which extracts the lines as an sp object, does that help you further?

from image.

spono avatar spono commented on August 19, 2024

many thanks!
I found it only later...now I'm trying to figure out how to refer them to the original georeferenced data.
What do the output's x and y refer to? I tried adding the minX and minY (lower left corner) but it seems that somewhere in the input process there's a sort of rescaling, in addition to the coords being lost. In this way I get lines with not real sizes and related to a relative reference system...

from image.

jwijffels avatar jwijffels commented on August 19, 2024

x and y correspond to the horizontal and vertical positions of the points defining the contour line
note that the magick R package the point (0,0) starts at the topleft

from image.

spono avatar spono commented on August 19, 2024

good to know!

BTW, I got a bit deeper and adapted your code to accept RasteLayers as well. I don't know if it might be of interest for your package but I think users using the tool on spatial data may appreciate it :) (there's quite a lot of room for your tools in such field)

image_contour_detector <- function(x, Q=2.0) {
if( is(r,"RasterLayer") ){
  
  minX = raster::extent(x)[1]
  minY = raster::extent(x)[3]
  
  resol = raster::res(x)[1]
  
  x = as.matrix(x)
} else { stopifnot(is.matrix(x)) }

if( anyNA(x) ){
  x[is.na(x)] = 0
  warning("NA values found and set to 0")
}

if( min(x, na.rm = TRUE)>=0 & max(x, na.rm = TRUE) <=255 ){ warning("Values range between 0 and 255: Q might be set to 0")}

contourlines <- image.ContourDetector:::detect_contours(x, 
                                X=nrow(x),
                                Y=ncol(x),
                                Q=Q)
names(contourlines) <- c("x", "y", "curvelimits", "curves", "contourpoints")
contourlines$curvelimits <- contourlines$curvelimits+1L
from <- contourlines$curvelimits
to <- c(tail(contourlines$curvelimits, contourlines$curves-1L)-1L, contourlines$contourpoints)
curve <- unlist(mapply(seq_along(from), from, to, FUN=function(contourid, from, to) rep(contourid, to-from+1L), SIMPLIFY = FALSE))
contourlines$data <- data.frame(x = contourlines$y, y = nrow(x) - contourlines$x, curve_id = curve)
contourlines$data$x = contourlines$data$x * resol + minX
contourlines$data$y = contourlines$data$y * resol + minY
contourlines <- contourlines[c("curves", "contourpoints", "data")]
class(contourlines) <- "cld"
contourlines
}

An idea for further enhancement could be to add a "asSpatial" boolen option to get directly the spatial object, but probably that would be too specific for the general purpose..or not?
BTW thanks and keep up the good work!

from image.

spono avatar spono commented on August 19, 2024

unfortunately I can't help with methods, yet, because I didn't get my hands on the topic...but I hope to contribute in the next future!
I think it's really necessary to expand the capabilities of the package towards the other main representations of arrays

from image.

jwijffels avatar jwijffels commented on August 19, 2024

Fixed with merging #24

from image.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.