Measure distance between two paper bands

I would like to use an OpenMV M7 to measure the gap between two paper bands while they run between two rolls on a production machine (I am attaching a picture took inside the machine).

Referring to the picture, I image that a camera each gap between two strips will be required.
Since I am a newbie in computer vision I would really appreciate any kind of suggestions about the right techniques and and the more convenient software example to be followed.


Hi, for this application you’ll need a lot of horizontal res but not vertical res. So, set the resolution to VGA. Set the pix format to grayscale. Then set windowing to (640, 80).

Next, use lens_corr() to undo lens distortion. Also, you should use the wide angle lens with your application.

After this… If you mounted the OpenMV Cam right… You should be able to see two slits and then the start of the next piece of paper.

If so, then to get_historgram() on the image followed by get_threshold() on what get histogram returns. This will give you the best color thresholds to segment the image with.

Finally, do find_blobs() using those color thresholds and measure the maximum length of he biggest blob. This will tell you the paper width in pixels.

I can provide code later tonight if you need it.

Thank you very much for your suggestion.
If you can provide some lines of code it would be very
well appreciated, so will have some base to start.


by re-reading your message I just realized that we had a misunderstanding: actually I need to measure the gap of a slit between two piece of paper.
I think the concept should be the same but, maybe, a littele more difficult to chose the right blob (I cannot pick the widther one…).

Oh, you just invert what color you are tracking and look for the widest blob still.

Sorry to bother you…
I am trying but I got stuck on this error…

Any help would much appreciated!

Hi please update your firmware. It’s out of date.

Hi, this code basically does what you want. Feel free to clean it up:

# Hello World Example
# Welcome to the OpenMV IDE! Click on the green run arrow button below to run the script!

import sensor, image, time

sensor.reset()                      # Reset and initialize the sensor.
sensor.set_pixformat(sensor.GRAYSCALE) # Set pixel format to RGB565 (or GRAYSCALE)
sensor.set_framesize(sensor.VGA)   # Set frame size to QVGA (320x240)
sensor.set_windowing((640, 80))
sensor.skip_frames(time = 2000)     # Wait for settings take effect.
clock = time.clock()                # Create a clock object to track the FPS.

    clock.tick()                    # Update the FPS clock.
    img = sensor.snapshot()
    threshold = img.get_histogram().get_threshold()
    blobs = img.find_blobs([(0, threshold.value())], invert=False)
    if blobs:
        largest = max(blobs, key = lambda x: x.w())
    print(clock.fps())              # Note: OpenMV Cam runs about half as fast when connected
                                    # to the IDE. The FPS should increase once disconnected.

Thank you for the code!

Actually my IDE shows on the bottom right corner “Firmware Version: 2.7.0 - [latest]”
The IDE version is 1.8.1.

I guess it already the newer one…

I noticed that the autocomplete dictionary seems to have the wrong spelling of that method name. Will fix.

Yes, same mistake is on the online documentation.

By the way, the line:
threshold = img.get_histogram().get_threshold()

still return the error " ‘histogram’ object has no attribute ‘get_threshold’ ".

The same if i try to write threshold in the wrong way.

Mmm, this method seems to not have made it in the v2.7.0 firmware.

Attached is the pre-release v2.8.0 firmware. Use Tools → Run bootloader to update your camera. Then the above code works. (1.06 MB)

Hello I made some steps ahead on the project.
I decided to cover the machine width with several CAMs and I cannot predict if the the narrow gap between paper slits will be in front of a camera (maybe some of them will see only a white paper sheet).
I experienced that in this case the CAM is detecting some strange blobs in the image which have no sense for my application.
How could I filter the blobs “quality” in order to make sure that I will only obtain a gap measurement (in case no gap is visible by a CAM I should have no output)?


Hi, I don’t know what’s going on from your description. Can you provide a simplified code snippet and pics of the issue?

Ok. I’ll send it tomorrow.
For the time being you can image that in white paper the cam is detecting a threshold due to some shadows and it result in a blob detection.
The core of the program I am using is practically the one you suggested a few posts above this.


Ah, okay, so, the issue is because you need to adaptive threshold the image. You can use the mean/median/mode/midpoint methods to adaptively threshold the image. This will take care of shadows. You will then just get a black and white image where hard lines are. See the adaptive threshold example under filters.