Creating a Template and which image parameter to find a char-sequence


I’m new with OpenMW and hope, someone could give me some hints to get the most of my Cam M7.

My first project should be the following:

I have a labeling machine. It put product labels on bottles. This works fine.

The machine has an ink-jet printer which should print some character (which always start with CH-B.).
Sometimes the ink-jet printer just doesn’t print and I would like to test with the Cam M7 if the text get’s printed.

The printer prints black on a white background. I know the position and could set the camera directly to this position.

Every time one label is printed (and transported) I get a signal to pin0, a buzzer is connected to pin2 and a reset-Button is on pin1.

Here is my code:

import sensor, image, time
from pyb import Pin
from pyb import LED
from image import SEARCH_EX, SEARCH_DS

sensor.reset()                      # Reset and initialize the sensor.
sensor.set_windowing(((640-160)//2, (480-120)//2, 160, 120))
template = image.Image("/template.pgm")

clock = time.clock()                # Create a clock object to track the FPS.

pinStart = Pin('P0', Pin.IN)
pinReset = Pin('P1', Pin.IN)
pinBeep  = Pin('P2', Pin.OUT_PP, Pin.OUT_PP)

found = 1
err = 0

    img = sensor.snapshot()     
    if err==0:
        r = img.find_template(template, 0.50, step=4, search=SEARCH_DS) #, roi=(10, 0, 50, 40))
        if r:
            found = 1
        if pinStart.value()==1:
            if found==0:
                err = 1

        if pinReset.value()==1:
            err = 0
            found = 1

I’ve tested it on my desk, It works but the recognition rate is low (I hold the cam in my hand and the parameter and the template are not optimized).
What parameter, resolution, threshold could I optimize to get the best results?
Which distance from the camera to the label would you use?
What’s the best way to create a template (the camera image is not so sharp and has some noise errors), but creating the text in an image program is not so good because of different bold and line width.

So what’s the best way to create a template?

Any help is welcome,

best regards


Is it possible to use something other than template matching? Template matching literally has to find the exact template otherwise it returns low results. Um, are there any features like color/lines/etc you can look for instead?

If you need to use template matching however I recommend not matching the image but a processed version. Like, binarize the template and the image coming in. This boosts the results by a huge amount. To choose the binarization threshold just pick like a grey value higher than the background.

Hi Nyamekye,

thank you for your quick answer.

I think template matching would be the best solution and with a adjusted template and transforming I hope it will work.

Another possible check would be to locate the Position where the text should be (I could locate it searching for a “line” nearby. And than looking if the rectangle where the text should be printed is white (empty) or mixed black and white (printed character).
But for example if the printer will print unclean, than only method 1 will spot this.

I now try to use:

thre = (0,170)
template = image.Image("/template.pgm").binary([thre])

Tomorrow I will try this out.

Best regards Mark