Reputation: 319
I'm trying to gain some experience with automatic text recognition and i'm using the package tesseract to perform ocr on some images (i.e. some screenshots I took).
To improve the performance of my program's recognition of the prices in the image below, I implemented some preprocessing on the image using the magick package by increasing the contrast of the image by changing brightness and saturation parameters.
However, I think the performance could be further increased by converting to a black and white image.
How can this be efficiently achieved in R?
Upvotes: 9
Views: 7237
Reputation: 422
To convert the image to black and white using magick
library in R, the following steps need to be performed:
The result is a black and white image. Here is a code snippet:
library(magick)
library(magrittr)
img <- image_read('https://i.sstatic.net/nn9k0.png')
img %>%
image_convert(colorspace = "Gray") %>%
image_threshold(type = "black", threshold = "50%") %>%
image_threshold(type = "white", threshold = "50%")
The threshold can be different.
Upvotes: 0
Reputation: 43344
You can convert the colorspace with magick::image_quantize
:
library(magick)
#> Linking to ImageMagick 6.9.9.25
#> Enabled features: cairo, fontconfig, freetype, fftw, lcms, pango, rsvg, webp
#> Disabled features: ghostscript, x11
i <- image_read('https://i.sstatic.net/nn9k0.png')
i
i %>% image_quantize(colorspace = 'gray')
Depending on your desired image structure, you could also use image_convert
to do the same thing:
i %>% image_convert(colorspace = 'gray')
# or
i %>% image_convert(type = 'Grayscale')
or to convert to true black and white (not grayscale),
i %>% image_convert(type = 'Bilevel')
which in this case returns an image with salt and pepper noise, which may or may not be useful.
Note, however, that while this might be good practice for OCR, it would be a lot simpler to get this data by webscraping, e.g. with rvest should it be permissible (presumably the same issues apply to grabbing these images). Better, should it contain the information you need, is to use the appropriate RyanAir API.
Upvotes: 9
Reputation: 53071
In ImageMagick command line, you can simply threshold at some percent. I used 50% here, but adjust as desired.
convert image.png -threshold 50% result.png
In Imagick, the command is Imagick::thresholdImage. See http://php.net/manual/en/imagick.thresholdimage.php. Sorry I do not know which "Magick" package you are using.
Upvotes: 5