Created
April 25, 2020 23:58
-
-
Save dmi3kno/dee5221b54a988a5cb201711f8d8a30f to your computer and use it in GitHub Desktop.
{magick} code for distracted boyfriend
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(magick) | |
library(bunny) | |
# function for calculating mode | |
mode_n <- function(x, n=1){ | |
res <- with(rle(sort(x)), values[order(lengths, decreasing = TRUE)]) | |
res[n] | |
} | |
img <- image_read("EWOwbehXkAgxeHO.jpeg") %>% | |
image_resize("50%") | |
# Find edges for tracing straight lines | |
img_th <- img %>% | |
image_threshold("black", "99%") %>% | |
image_canny() | |
#img_th %>% | |
# image_hough_draw(geometry = "100x200+400", overlay=TRUE) | |
# find boundaries of the image | |
mvg <- img_th %>% | |
image_hough_txt(geometry = "100x200+400") | |
library(dplyr) | |
library(purrr) | |
library(hocr) | |
img_gg <- mvg %>% | |
tidy_hough_mvg() %>% | |
.[["xsect_data"]] %>% | |
summarise(x1=min(xsect_x), | |
y1=min(xsect_y), | |
x2=max(xsect_x), | |
y2=max(xsect_y)) %>% | |
paste(collapse = " ") %>% | |
bbox_to_geometry() %>% | |
{image_crop(img, geometry=.)} | |
# Threshold again for finding grid size | |
img_gg_th <- img_gg %>% | |
image_threshold("black", "99%") %>% | |
image_canny() | |
# Prepare mask for removing the grid later | |
img_gg_msk <- img_gg_th %>% | |
image_convert(format = "png") %>% | |
image_morphology("Close", "Square") %>% | |
image_negate() | |
#img_gg_th %>% | |
# image_hough_draw(geometry = "10x10+200", overlay=TRUE) | |
step_x <- img_gg_th %>% | |
image_hough_txt(geometry = "10x10+200") %>% | |
tidy_hough_mvg() %>% | |
.[["lines_data"]] %>% | |
filter(line_angle==0) %>% | |
summarise(med_lined=mode_n(diff(line_distance),2)) %>% | |
pull(med_lined) | |
step_x | |
step_y <- img_gg_th %>% | |
image_hough_txt(geometry = "10x10+200") %>% | |
tidy_hough_mvg() %>% | |
.[["lines_data"]] %>% | |
filter(line_angle==90) %>% | |
summarise(med_lined=mode_n(diff(line_distance),2)) %>% | |
pull(med_lined) | |
step_y | |
img_gg <- img_gg %>% | |
image_composite(img_gg_msk, operator = "CopyOpacity") %>% | |
image_background("white") %>% | |
image_flatten() | |
ii <- image_info(img_gg) | |
ii | |
xs <- seq.int(from=step_x/2, to=ii$width, by=step_x) | |
ys <- seq.int(from=floor(step_y/2), to=ii$height, by=step_y) | |
dd <- expand.grid(xs, ys) %>% | |
mutate(x=match(Var1, unique(Var1)), | |
y=match(Var2, unique(Var2)), | |
geometry=paste0("+", Var1, "+", Var2), | |
colr=map_chr(geometry, ~image_getpixel(img_gg, .x))) | |
dd | |
library(ggplot2) | |
dd %>% | |
filter(!colr%in%c("#fefefeff", "#ffffffff")) %>% | |
ggplot()+ | |
geom_point(aes(x=x, y=y, color=I(colr)), shape=15, size=4)+ | |
scale_y_reverse()+ | |
theme_void() | |
ggsave("distracted_boyfriend.png") |
Author
dmi3kno
commented
Apr 25, 2020
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment