图形的运算

二值化 binary

image.binary(thresholds, invert=False)

thresholds阈值为一个数组[...], 数组里面有若干个tuple组成, 每个tuple都由最大值与最小值组成,(lower, upper), 即上界下界

如果是RGB格式的话, 则需要设定六个阈值 (l_lo, l_hi, a_lo, a_hi, b_lo, b_hi)

在阈值范围内的就设定为1(white), 不再阈值范围内的就设定为0(black)

原图

before_binary

实验代码1

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.
    img.binary([(100, 255)],invert=False)

效果

binary_noinvert_demo

实验代码2

反色

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.
    img.binary([(100, 255)],invert=True)

效果

binary_invert_demo

反色 invert

image.invert()

反色就是说,

1(白色) --> 0(黑色)

0(黑色) --> 1(白色)

相当于逻辑中的取反操作, 上述二值化反色的代码, 等同于如下实验代码

实验代码1

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.
    img.binary([(100, 255)],invert=False)
    img.invert()

效果

binary_invert_demo

求不同 different

从这张图片减去另一个图片。比如,对于每个通道的每个像素点,取相减绝对值操作。这个函数,经常用来做移动检测。

image.difference(image)

逻辑运算

首先我们定义两个图形

中间白色的区域是1

黑色的区域即为0

图形1 正方形

bitwise_rectangle

图形2 圆形

bitwise_circle

逻辑与 - and

逻辑与经常被用与遮盖层(MASK), 即去除背景, 选取自己感兴趣的区域.

image.b_and(image)

真值表

A B A AND B
0 0 0
0 1 0
1 0 0
1 1 1

效果

bitwise_and

逻辑或 - or

image.b_or(image)

真值表

A B A OR B
0 0 0
0 1 1
1 0 1
1 1 1

效果

bitwise_or

逻辑与非 - nand

image.b_nand(image)

真值表

A B A NOT AND B
0 0 1
0 1 0
1 0 0
1 1 0

效果

bitwise_nand

逻辑或非 - nor

image.b_nor(image)

真值表

A B A NOR B
0 0 1
0 1 0
1 0 0
1 1 0

效果

bitwise_nor

逻辑抑或 - xor

image.b_xor(image)

真值表

A B A OR B
0 0 0
0 1 1
1 0 1
1 1 0

效果

bitwise_xor

逻辑抑或非 - xnor

image.b_xor(image)

真值表

A B A OR B
0 0 1
0 1 0
1 0 0
1 1 1

效果

bitwise_xnor

TODO

  • 完善difference
  • 结合逻辑运算给出具体的应用场景.
Copyright 杭州云江科技有限公司 2017 all right reserved,powered by Gitbook该文件修订时间: 2019-04-06 01:27:51

results matching ""

    No results matching ""