0

I have an image with "you" word and code to process that image: enter image description here

Mat dst;
Mat gray;

Mat image = imread( "you.jpg" );  

cvtColor(image, gray, COLOR_BGR2GRAY);
threshold(gray,dst, 170, 255, THRESH_BINARY);

imshow("dst Image", dst);

I am trying to make that "you" more readable (to read using tesseract) like this: enter image description here

But the code above gives me black screen: enter image description here

How to get that word without changing threshold numbers (170, 255), because I have other images to process and threshold like (75, 150) doesn't work for them.

socm_
  • 783
  • 11
  • 28
  • Seems you need to show "dst" not "gray" – Andrey Smorodov Nov 25 '19 at 17:55
  • @AndreySmorodov thank you, updated – socm_ Nov 25 '19 at 17:57
  • Have you tried Otsu or adaptive thresholding? See https://docs.opencv.org/master/d7/d4d/tutorial_py_thresholding.html The code is in Python, but the theory is the important part. – beaker Nov 25 '19 at 18:02
  • Check my answer here: https://stackoverflow.com/questions/22122309/opencv-adaptive-threshold-ocr/22127181#22127181 – Andrey Smorodov Nov 25 '19 at 18:23
  • You should consider using Otsu's threshold so you dont have to use a fixed threshold number. Otsu automatically calculates the threshold value. Another way is to use Adaptive thresholding – nathancy Nov 25 '19 at 20:44

1 Answers1

0

Try also this one. It is a bit more "adaptive", that is, have less "magical" threshold numbers and takes into account that the background has (may have) some color gradient. So it should work for a wider set of images with different threshold needs.

The input image is too small, so further improvements (for example, smoother contours of letters) are quite limited. Hope you find this useful.

Mat dst;
Mat gray;

Mat image = imread("you.jpg");

cvtColor(image, gray, COLOR_BGR2GRAY);
cv::GaussianBlur(gray, gray, cv::Size(3, 3), 0);
cv::adaptiveThreshold(gray, dst, 255, cv::ADAPTIVE_THRESH_GAUSSIAN_C, cv::THRESH_BINARY, 31, -25);
cv::Mat kernel = getStructuringElement(cv::MORPH_RECT, cv::Size(3, 3));
cv::morphologyEx(dst, dst, cv::MORPH_DILATE, kernel, cv::Point(-1, -1), 1);

imshow("dst Image", dst);
waitKey(0);

Here is the resulting image.

Resulting image

Cynichniy Bandera
  • 5,991
  • 2
  • 29
  • 33