简体   繁体   中英

Image recognizing with counting pixels of specific color, using opencv

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

在此处输入图片说明 在此处输入图片说明

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 pics 在此处输入图片说明 , 在此处输入图片说明 .

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM