The idea: I have one screenshot and want to find all characters and numbers with its postition on this image. The easiest way is to use opencv match template and compare all characters (around 800) I have as ".png" to the screenshots.
myTemplatesPath = "C:/MyPath/Templates/"
allTemplateFiles = [os.path.join(root, name) for root, dirs, files in os.walk(myTemplatesPath) for name in files]
Templates_all = [cv2.imread(f, cv2.IMREAD_GRAYSCALE) for f in allTemplateFiles]
imgrey = cv2.cvtColor(screenshot, cv2.COLOR_BGR2GRAY)
for template in Templates_all:
results = cv2.matchTemplate(imgrey, template, cv2.TM_CCOEFF_NORMED)
results = np.where(results > 0.99)
Image:
Templates with different font sizes (just some examples):
This is working 100% fine. The only problem I have is the speed. It takes about 6s to find all positions in the image because it has to compare 800 templates with this 1 image. I would like to improve this time.
I had several ideas to improve this speed:
- Use OCR -> to unreliable, did not recognize every character
- Feature detection did not detect all characters like the large "L" had no features.
- Devide image into ROIs by using find contours and then extract the width and height of those contours and then just compare the templates which fit the size of the extracted width and height. This would reduce the screen compares drastically and improve speed but finding contours lead to contours which devided the characters in 3 or more parts which would lead to incorrect height and width.
So I'm still searching for a good way to find the locations of the characters which is 100% reliable but faster. (I prefer idea number 3 but I'm open for every proposal)