>>> 星瞳科技|OpenMV中国官方网站 <<<

>>> 星瞳科技店铺地址|OpenMV中国官方代理,全球九大代理商之一 <<<

>>> OpenMV3 Cam M7 官方高配版上市啦~戳我戳我戳我~ <<<

>>> 星瞳科技-OpenMV中文教程网 <<<

本例程为04-image-Filters-color_binary_filter.py
本例程讲述如何利用颜色特征对图像进行分割。

# Color Binary Filter Example
#
# This script shows off the binary image filter. This script was originally a
# test script... but, it can be useful for showing how to use binary.

import pyb, sensor, image, math

sensor.reset()
sensor.set_framesize(sensor.QVGA)
sensor.set_pixformat(sensor.RGB565)

#设置颜色阈值,如果是rgb图像,六个数字分别为(minL, maxL, minA, maxA, minB, maxB);
#如果是灰度图,则只需设置(min, max)两个数字即可。
red_threshold = (0,100,   0,127,   0,127) # L A B
green_threshold = (0,100,   -128,0,   0,127) # L A B
blue_threshold = (0,100,   -128,127,   -128,0) # L A B

while(True):

    # Test red threshold
    for i in range(100):
        img = sensor.snapshot()
        img.binary([red_threshold])
        #image.binary(thresholds, invert=False)此函数将在thresholds内的
        #图像部分的全部像素变为1白,将在阈值外的部分全部像素变为0黑。invert将图像
        #的0 1(黑 白)进行反转,默认为false不反转。
    # Test green threshold
    for i in range(100):
        img = sensor.snapshot()
        img.binary([green_threshold])
    # Test blue threshold
    for i in range(100):
        img = sensor.snapshot()
        img.binary([blue_threshold])
    # Test not red threshold
    for i in range(100):
        img = sensor.snapshot()
        img.binary([red_threshold], invert = 1)
        #invert=1,将黑白反转
    # Test not green threshold
    for i in range(100):
        img = sensor.snapshot()
        img.binary([green_threshold], invert = 1)
    # Test not blue threshold
    for i in range(100):
        img = sensor.snapshot()
        img.binary([blue_threshold], invert = 1)

实现绿色图像分割:
原图:

执行程序后:
invert = False

invert = 1