1

My code get many small images with digits in them. I try to compare it with my templates and get right data. It worked..

enter image description here enter image description here

original= cv2.imread('im/10.png')
sought = (254,254,254)
result = np.count_nonzero(np.all(original==sought,axis=2))

As you can see in these cases, white digits are at different corners of pics, and no problem, results (quanity of white pixels) are equal and I got, that 18=18. but now there are new picsenter image description here,enter image description here.

First of all, digits here aren't (254,254,254). I think image is more dim maybe. or less quality and i try to use:

result = np.count_nonzero(np.all(original > 200,axis=2))

Unfortunately, it give me different data. Two pics with 13 inside aren't equal.

What i want:

Method of pointing out white digits from dark background, except thin white circle around. (at 13 pics) Circle isn't a problem, if I crop image at center and get rid of circle my results wouldn't change - 13 still != 13.

Maybe it possible with converting image to grayscale, maybe to HSV, maybe just to make it brighter or more contrast.

Pixels counting is good for me it is quetly fast and usually accurate.

I'll repeat: now my code see two pics with 13 like different (there are difference colors, or brightness or black/white ratio, I don't know) I want to get rid of this problem.

Kirill
  • 103
  • 1
  • 3
  • 10
  • You should try a threshold using OTSU as first step and then (depending of the result) count all the non zero pixels directly. – John_Sharp1318 Sep 26 '18 at 20:56
  • Your method to recognize the object by counting pixels is wrong. – Kinght 金 Sep 27 '18 at 00:29
  • Pixel counting is unlikely to be very accurate as a means of differentiating digits/letters. Try Googling *"Template Matching"* and/or *"Optical Character Recognition"* and **Tesseract**. Good luck! – Mark Setchell Sep 27 '18 at 08:31

1 Answers1

1

I propose that you first threshold the image, meaning that pixels with letters will be of value 255, and the rest 0. This can be done with OpenCV's Adaptive Threshold function (you can see this answer).

Then, you will need a better way to determine the numbers. Simply counting the number of white pixels is not robust enough. For example, the numbers 13 and 31 should have the same number of white pixels. This can break your algorithm.

The best solution here is AI. You can look into Python Tesseract for example, but there are a lot of Python packages and tutorials. Just Google this, and you get to helpful answers like this one. There are even tutorials like this one.

If you don't want to use AI, then your algorithm will always break somewhere.

Hein Wessels
  • 937
  • 5
  • 15
  • Okay, my question is: What the way to get rid of noise, there are no perfect black background on my pics, there are some white shadows, I want to point out white digits from other parts of image, make them more bright or contrast.. – Kirill Sep 27 '18 at 12:11
  • 1
    The `AdaptiveThreshold` method I proposed does get rid of noise, and will work well if the images look the ones in your question. `Thresholding` turns a noisy image into a perfect `0`/`255` image. If you are trying to get rid of the circle your can try dialating and then erroding your image (see [this](https://pythonprogramming.net/morphological-transformation-python-opencv-tutorial/) tutorial). – Hein Wessels Sep 27 '18 at 12:16