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

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

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

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

本例程为04-image-Filters-edge_detection.py
本例程的目的是实现边缘检测。

# Edge Detection Example:
#
# This example demonstrates using the morph function on an image to do edge
# detection and then thresholding and filtering that image afterwards.

import sensor, image, time

#设置核函数滤波,核内每个数值值域为[-128,127],核需为列表或元组
kernel_size = 1 # kernel width = (size*2)+1, kernel height = (size*2)+1
kernel = [-1, -1, -1,\
          -1, +8, -1,\
          -1, -1, -1]
# This is a high pass filter kernel. see here for more kernels:
# http://www.fmwconcepts.com/imagemagick/digital_image_filtering.pdf
thresholds = [(100, 255)] # grayscale thresholds设置阈值

sensor.reset() # Initialize the camera sensor.
sensor.set_pixformat(sensor.GRAYSCALE) # or sensor.RGB565
sensor.set_framesize(sensor.QQVGA) # or sensor.QVGA (or others)
sensor.skip_frames(10) # Let new settings take affect.
clock = time.clock() # Tracks FPS.

# On the OV7725 sensor, edge detection can be enhanced
# significantly by setting the sharpness/edge registers.
# Note: This will be implemented as a function later.
if (sensor.get_id() == sensor.OV7725):
    sensor.__write_reg(0xAC, 0xDF)
    sensor.__write_reg(0x8F, 0xFF)

while(True):
    clock.tick() # Track elapsed milliseconds between snapshots().
    img = sensor.snapshot() # Take a picture and return the image.

    img.morph(kernel_size, kernel)
    #morph(size, kernel, mul=Auto, add=0),morph变换,mul根据图像对比度
    #进行调整,mul使图像每个像素乘mul;add根据明暗度调整,使得每个像素值加上add值。
    #如果不设置则不对morph变换后的图像进行处理。    
    img.binary(thresholds)
    #利用binary函数对图像进行分割

    # Erode pixels with less than 2 neighbors using a 3x3 image kernel
    img.erode(1, threshold = 2)
    #侵蚀函数erode(size, threshold=Auto),去除边缘相邻处多余的点。threshold
    #用来设置去除相邻点的个数,threshold数值越大,被侵蚀掉的边缘点越多,边缘旁边
    #白色杂点少;数值越小,被侵蚀掉的边缘点越少,边缘旁边的白色杂点越多。

    print(clock.fps()) # Note: Your OpenMV Cam runs about half as fast while
    # connected to your computer. The FPS should increase once disconnected.

原图:

边缘检测后的图像:
img.erode(1, threshold = 2)

img.erode(1, threshold = 4) threshold过大被侵蚀掉的边缘点过多,边缘不清晰连贯,图像偏黑