/
helpers_magick.R
99 lines (92 loc) · 2.59 KB
/
helpers_magick.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#' Add helpers for working with image-magick
#'
#' @param x `image-magick` object
#' @param threshold to feed [magick::image_threshold]
#' @param canny_geometry to feed [magick::image_canny]
#'
#' @details For image_mask, some "missed" objects can
#' be avoided with large image files or by adjusting threshold
#' and canny_edge radius.
#'
#' @name image_helpers
#'
#' @examples
#' Momit_data("malus/malus1.jpg") %>%
#' # you dont need magick:: if your library(magick before)
#' magick::image_read() -> m0
#' m0
#' m1 <- m0 %>% image_mask()
#' m1
#' mx <- m1 %>% image_explode
#'
#' \dontrun{
#' mx
#' }
#'
#' mx[1] %>% image_rasterize2D %>% algo_Conte()
NULL
#' @describeIn image_helpers tests if is of image-magick class
#' @export
is_image <- function(x){
inherits(x, "magick-image")
}
#' @describeIn image_helpers converts image-magick to 2D raster matrix
#' @export
image_rasterize2D <- function(x){
x %>%
# magick::image_channel("Gray") %>%
`[[`(1) %>%
# turn into integers
as.integer() %>%
# and into a matrix (not a single slice array)
`[`(,,1)
}
#' @describeIn image_helpers turn an inmage into a mask
#' @export
image_mask <- function(x, threshold="50%", canny_geometry = "0x1+10%+30%") {
x %>%
# extract lighness
magick::image_channel("lightness") %>%
# blur helps
# magick::image_blur(radius = blur_radius, sigma = blur_sigma) %>%
# binarize now
magick::image_threshold(type="white", threshold = threshold) %>%
# canny edges
magick::image_canny(geometry = canny_geometry) %>%
# black border to prevent border artifacts
magick::image_border(color="black", geometry="20x20") %>%
# fill with white
magick::image_fill("white", fuzz=5) %>%
# remove border now
magick::image_chop(geometry="20x20") %>%
magick::image_channel("lightness")
}
#' @describeIn image_helpers turn a multi-object image into multiple image with single object
#' @export
image_explode <- function(x){
x %>%
magick::image_connect(connectivity = 1) %>%
magick::image_split() %>%
# remove background layer
`[`(-1) %>%
magick::image_trim() %>%
magick::image_border(color="white", geometry="2x2")
}
#' @describeIn image_helpers returns image center coordinates
#' @export
image_center <- function(x){
# magick case
if (is_image(x)){
x %>%
magick::image_info() %>%
dplyr::select(.data$width, .data$height) %>%
unlist() -> d
}
# matrix or array case
if (is.array(x)){
dim(x)[1:2] %>%
`names<-`(c("width", "height")) -> d
}
# ceiling is for (only theoretical) dim(1, 1) case
ceiling(d/2)
}