Skip to content

Commit

Permalink
Invert subtitle image before the OCR
Browse files Browse the repository at this point in the history
It makes it work way better on my side, especially
with subtitles containing multiple lines.
  • Loading branch information
MatMaul committed Feb 22, 2018
1 parent b058f9d commit fdff521
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/vobsub2srt.c++
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,16 @@ TessBaseAPI* init_tesseract(std::string tesseract_data_path, char const *tess_la
return tess_base_api;
}

void invert_image(unsigned width, unsigned height,
unsigned stride, unsigned char *image) {
for (unsigned y=0; y < height; y++) {
for (unsigned x=0; x < width; x++) {
unsigned index = y*stride + x;
image[index] = 255 - image[index];
}
}
}

void do_ocr(ocr_thread_t *ocr_thread, vector<sub_text_t> *conv_subs, mutex *mut,
unsigned counter, unsigned width, unsigned height, unsigned stride,
unsigned char *image_cpy, unsigned start_pts, unsigned end_pts, bool verb) {
Expand Down Expand Up @@ -153,6 +163,7 @@ void do_ocr(ocr_thread_t *ocr_thread, vector<sub_text_t> *conv_subs, mutex *mut,

int main(int argc, char **argv) {
bool dump_images = false;
bool disable_invert = false;
bool verb = false;
bool list_languages = false;
std::string ifo_file;
Expand Down Expand Up @@ -186,6 +197,7 @@ int main(int argc, char **argv) {
add_option("min-width", min_width, "Minimum width in pixels to consider a subpicture for OCR (Default: 9)").
add_option("min-height", min_height, "Minimum height in pixels to consider a subpicture for OCR (Default: 1)").
add_option("max-threads", max_threads, "Maximum number of threads to use to do the OCR, use 0 to autodetect the number of cores (Default: 0)").
add_option("disable-invert", disable_invert, "By default the image will be inverted before the OCR because it works better with black on white background").
add_unnamed(subname, "subname", "name of the subtitle files WITHOUT .idx/.sub ending! (REQUIRED)");
if(not opts.parse_cmd(argc, argv) or subname.empty()) {
return 1;
Expand Down Expand Up @@ -342,6 +354,9 @@ int main(int argc, char **argv) {
unsigned char *image_cpy = (unsigned char *)malloc(image_size);
memcpy(image_cpy, image, image_size);

if (!disable_invert)
invert_image(width, height, stride, image_cpy);

ocr_thread->done = false;
ocr_thread->t = new thread(do_ocr, ocr_thread, &conv_subs, &mut, sub_counter, width, height, stride, image_cpy, start_pts, end_pts, verb);

Expand Down

0 comments on commit fdff521

Please sign in to comment.