Skip to content

Commit fdff521

Browse files
committed
Invert subtitle image before the OCR
It makes it work way better on my side, especially with subtitles containing multiple lines.
1 parent b058f9d commit fdff521

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

src/vobsub2srt.c++

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,16 @@ TessBaseAPI* init_tesseract(std::string tesseract_data_path, char const *tess_la
121121
return tess_base_api;
122122
}
123123

124+
void invert_image(unsigned width, unsigned height,
125+
unsigned stride, unsigned char *image) {
126+
for (unsigned y=0; y < height; y++) {
127+
for (unsigned x=0; x < width; x++) {
128+
unsigned index = y*stride + x;
129+
image[index] = 255 - image[index];
130+
}
131+
}
132+
}
133+
124134
void do_ocr(ocr_thread_t *ocr_thread, vector<sub_text_t> *conv_subs, mutex *mut,
125135
unsigned counter, unsigned width, unsigned height, unsigned stride,
126136
unsigned char *image_cpy, unsigned start_pts, unsigned end_pts, bool verb) {
@@ -153,6 +163,7 @@ void do_ocr(ocr_thread_t *ocr_thread, vector<sub_text_t> *conv_subs, mutex *mut,
153163

154164
int main(int argc, char **argv) {
155165
bool dump_images = false;
166+
bool disable_invert = false;
156167
bool verb = false;
157168
bool list_languages = false;
158169
std::string ifo_file;
@@ -186,6 +197,7 @@ int main(int argc, char **argv) {
186197
add_option("min-width", min_width, "Minimum width in pixels to consider a subpicture for OCR (Default: 9)").
187198
add_option("min-height", min_height, "Minimum height in pixels to consider a subpicture for OCR (Default: 1)").
188199
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)").
200+
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").
189201
add_unnamed(subname, "subname", "name of the subtitle files WITHOUT .idx/.sub ending! (REQUIRED)");
190202
if(not opts.parse_cmd(argc, argv) or subname.empty()) {
191203
return 1;
@@ -342,6 +354,9 @@ int main(int argc, char **argv) {
342354
unsigned char *image_cpy = (unsigned char *)malloc(image_size);
343355
memcpy(image_cpy, image, image_size);
344356

357+
if (!disable_invert)
358+
invert_image(width, height, stride, image_cpy);
359+
345360
ocr_thread->done = false;
346361
ocr_thread->t = new thread(do_ocr, ocr_thread, &conv_subs, &mut, sub_counter, width, height, stride, image_cpy, start_pts, end_pts, verb);
347362

0 commit comments

Comments
 (0)