Skip to content

Instantly share code, notes, and snippets.

@dmi3kno
Created May 5, 2020 18:39
Show Gist options
  • Save dmi3kno/0e7f5b24f981eb4aee12573e86004352 to your computer and use it in GitHub Desktop.
Save dmi3kno/0e7f5b24f981eb4aee12573e86004352 to your computer and use it in GitHub Desktop.
Font segmentation
library(magick)
library(hocr)
library(bunny)
library(bbx)
library(dplyr)
img <- image_read("https://pbs.twimg.com/media/EXKzsiAWoAYH9Pv?format=png")
img_bw <- img %>%
image_threshold("white", threshold = "5%") %>%
image_convert(type = "Grayscale") %>%
image_morphology("Open") %>%
image_negate()
bbox_df <- img_bw %>%
image_ocr(HOCR = TRUE) %>%
hocr_parse() %>%
tidy_tesseract()
bb <- bbox_df %>%
slice(1) %>%
pull(ocrx_word_bbox)
img_ltr_slots<- image_crop(img_bw, geometry = bbx_to_geometry(bb)) %>%
image_morphology("HMT", paste0("Rectangle:1x",bbx_h(bb))) %>%
image_morphology("Thinning", "ThinSe")
## hough line method
ii <- image_info(img_ltr_slots)
iiw <- ii$width
iih <- ii$height
img_ltr_slots %>%
image_hough_txt(geometry=paste0("1x1+", iih)) %>%
bunny::tidy_hough_mvg() %>%
.[["lines_data"]] %>%
filter(line_angle==0) %>%
pull(line_bbox) %>%
bbx_to_bbm() %>%
{rbind(matrix(c(0,0,0,0), nrow = 1),.,
matrix(c(iiw, iih, iiw, iih), nrow=1))} %>%
{mapply(function(i, f) f(.[,i],-1), seq(ncol(.)),
list(head, head, tail, tail))} %>%
bbm_to_bbx() %>%
bbx_translate(bbx_to_geometry(bb)) %>%
bbx_pad_width(side="right") %>%
bbx_to_geometry() %>%
lapply(function(x)image_crop(img_bw, x)) %>%
{Reduce(c,.)} %>%
image_scale("300%") %>%
image_animate(delay=50) %>%
image_write_gif("coolbutuseless_arcadefont.gif")
@dmi3kno
Copy link
Author

dmi3kno commented May 5, 2020

coolbutuseless_arcadefont

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment