我们都知道Opencv上有许多算法,可以实现自适应阈值。
https://blog.csdn.net/qq_51491920/article/details/125727129

那么 Openmv 上的自适应阈值要如何实现呢。

废话不多说,直接上代码。如果想要知道原理,可以看上面链接中的文章。

获取前景

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.QVGA)   # Set frame size to QVGA (320x240)
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()         # Take a picture and return the image.
    # to the IDE. The FPS should increase once disconnected.
    hist = img.get_histogram()
    Thresholds = hist.get_threshold()
    print(Thresholds)
    v = Thresholds.value()
    img.binary([(0,v)])

获取背景

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.QVGA)   # Set frame size to QVGA (320x240)
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()         # Take a picture and return the image.
    # to the IDE. The FPS should increase once disconnected.
    hist = img.get_histogram()
    Thresholds = hist.get_threshold()
    print(Thresholds)
    v = Thresholds.value()
    img.binary([(v, 255)])

可以根据个人需求,选择所需要的代码。

Logo

有“AI”的1024 = 2048,欢迎大家加入2048 AI社区

更多推荐