Page 1 of 1

Measure distance between two paper bands

Posted: Fri Mar 30, 2018 6:33 am
by frav
Hello,
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).
Francesco
Paper bands.JPG
Paper bands.JPG (78.38 KiB) Viewed 6028 times
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.

Greetings

Re: Measure distance between two paper bands

Posted: Fri Mar 30, 2018 11:12 am
by kwagyeman
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.

Re: Measure distance between two paper bands

Posted: Fri Mar 30, 2018 12:29 pm
by frav
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.

Bye
Francesco

Re: Measure distance between two paper bands

Posted: Fri Mar 30, 2018 3:06 pm
by frav
Hi,
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.
Img.jpg
Img.jpg (5.88 KiB) Viewed 6004 times
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...).

Re: Measure distance between two paper bands

Posted: Fri Mar 30, 2018 3:32 pm
by kwagyeman
Oh, you just invert what color you are tracking and look for the widest blob still.

Re: Measure distance between two paper bands

Posted: Sat Mar 31, 2018 10:14 am
by frav
Sorry to bother you...
I am trying but I got stuck on this error...
Error.JPG
Error.JPG (89.25 KiB) Viewed 5887 times
Any help would much appreciated!

Re: Measure distance between two paper bands

Posted: Sat Mar 31, 2018 11:49 am
by kwagyeman
Hi please update your firmware. It's out of date.

Re: Measure distance between two paper bands

Posted: Sat Mar 31, 2018 12:58 pm
by kwagyeman
Hi, this code basically does what you want. Feel free to clean it up:

Code: Select all

# 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.

while(True):
    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())
        img.draw_rectangle(largest.rect())
        print(largest.w())
    print(clock.fps())              # Note: OpenMV Cam runs about half as fast when connected
                                    # to the IDE. The FPS should increase once disconnected.

Re: Measure distance between two paper bands

Posted: Sat Mar 31, 2018 1:46 pm
by frav
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....

Re: Measure distance between two paper bands

Posted: Sat Mar 31, 2018 2:05 pm
by kwagyeman
I noticed that the autocomplete dictionary seems to have the wrong spelling of that method name. Will fix.

Re: Measure distance between two paper bands

Posted: Sat Mar 31, 2018 2:22 pm
by frav
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.

Re: Measure distance between two paper bands

Posted: Sat Mar 31, 2018 2:56 pm
by kwagyeman
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.

Re: Measure distance between two paper bands

Posted: Tue May 01, 2018 2:28 pm
by frav
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)?

thanks
F

Re: Measure distance between two paper bands

Posted: Tue May 01, 2018 2:32 pm
by kwagyeman
Hi, I don't know what's going on from your description. Can you provide a simplified code snippet and pics of the issue?

Re: Measure distance between two paper bands

Posted: Tue May 01, 2018 2:59 pm
by frav
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.

Bye

Re: Measure distance between two paper bands

Posted: Tue May 01, 2018 4:21 pm
by kwagyeman
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.