13

I am working with Opencv for my project. I need to convert the image below to threshold image

Original Image

I tried this function:

Imgproc.threshold(imgGray, imgThreshold, 0, 255, Imgproc.THRESH_BINARY + Imgproc.THRESH_OTSU); 

But the result was not so good, as you see below

threshold

So I tried the adaptiveThreshold function:

Imgproc.adaptiveThreshold(imgGray, imgThreshold, 255, Imgproc.ADAPTIVE_THRESH_GAUSSIAN_C, Imgproc.THRESH_BINARY, 11, 2); 

and it resulted:

adaptiveThreshold

I just expect a binary image with white background and black text only, no black area or noise ( I do not prefer using Photo.fastNlMeansDenoising because it takes a lot of time). Please help me with a solution for this.

Also, I am using Tesseract for Japanese recognization but the accuracy rate is not good. Do you have any suggestion on better OCR for Japanese, or any method to improve Tesseract quality?

Kalyan Chavali
  • 1,330
  • 8
  • 24
Bee Bee
  • 185
  • 1
  • 2
  • 11
  • It's been a long time since I used openCV, so I may be talking nonsense. Can't you search for contours and white everything outside a contour and black everything inside the contour? The result image should be pretty sharp and accurate, given the type of input. – Diego Martinoia Jul 08 '15 at 10:45

1 Answers1

25

adaptiveThreshold is the right choice here. Just need a litte tuning. With these parameters (it's C++, but you can easily translate to Java)

Mat1b gray= imread("path_to_image", IMREAD_GRAYSCALE);
Mat1b result;
adaptiveThreshold(gray, result, 255, ADAPTIVE_THRESH_MEAN_C, THRESH_BINARY, 15, 40);

the resulting image is:

enter image description here

Miki
  • 40,887
  • 13
  • 123
  • 202
  • Thank you very much. This work perfectly for me. I just dont understand the meaning of the last parameter (40). Could you pls explain this? – Bee Bee Jul 10 '15 at 07:07
  • @BeeBee It's a constant subtracted from the mean. Read [opencv doc](http://docs.opencv.org/modules/imgproc/doc/miscellaneous_transformations.html#adaptivethreshold) for further information – Miki Jul 10 '15 at 12:02
  • can show me the java code here even in kotlin is fine need to remove shadow from image – M.Yogeshwaran Nov 04 '19 at 14:55
  • @Miki I get cannot resolve Mat1b in AndroidStudio and there are no suggestions either. I'm using OpenCV4.2. Could you please suggest how/where to resolve this? – helloworld Jul 25 '20 at 09:03
  • Mat is defined in org.opencv.core . – remo Feb 01 '22 at 16:12