-
Notifications
You must be signed in to change notification settings - Fork 24
/
frankenstein_TSP.R
39 lines (31 loc) · 1.04 KB
/
frankenstein_TSP.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
library(imager)
library(dplyr)
library(ggplot2)
library(scales)
library(TSP)
# Download the image
urlfile="http://ereaderbackgrounds.com/movies/bw/Frankenstein.jpg"
file="frankenstein.jpg"
if (!file.exists(file)) download.file(urlfile, destfile = file, mode = 'wb')
# Load, convert to grayscale, filter image (to convert it to bw) and sample
load.image(file) %>%
grayscale() %>%
threshold("45%") %>%
as.cimg() %>%
as.data.frame() %>%
sample_n(8000, weight=(1-value)) %>%
select(x,y) -> data
# Compute distances and solve TSP (it may take a minute)
as.TSP(dist(data)) %>%
solve_TSP(method = "arbitrary_insertion") %>%
as.integer() -> solution
# Rearrange the original points according the TSP output
data_to_plot <- data[solution,]
# A little bit of ggplot to plot results
ggplot(data_to_plot, aes(x,y)) +
geom_path() +
scale_y_continuous(trans=reverse_trans())+
coord_fixed()+
theme_void()
# Do you like the result? Save it! (Change the filename if you want)
ggsave("frankyTSP.png", dpi=600, width = 4, height = 5)