Example: 02-Image-Processing/01-Image-Filters/median_adaptive_threshold_filter.py
# 本作品采用MIT许可证授权。
# 版权所有 (c) 2013-2023 OpenMV LLC。保留所有权利。
# https://github.com/openmv/openmv/blob/master/LICENSE
#
# Median Adaptive Threshold Filter Example
#
# This example shows off median filtering with adaptive thresholding.
# When median(threshold=True) the median() method adaptive thresholds the image
# by comparing the median of the pixels around a pixel, minus an offset, with that pixel.
import sensor
import time
sensor.reset() # 初始化相机传感器。
sensor.set_pixformat(sensor.RGB565) # 或 sensor.GRAYSCALE
sensor.set_framesize(sensor.QQVGA) # 或 sensor.QVGA(或其他)
sensor.skip_frames(time=2000) # 让新设置生效。
clock = time.clock() # 跟踪FPS。
while True:
clock.tick() # Track elapsed milliseconds between snapshots().
img = sensor.snapshot() # 拍照并返回图像。
# The first argument to the median filter is the kernel size, it can be
# 0、1或2,分别对应1x1、3x3或5x5内核。 The second
# argument "percentile" is the percentile number to choose from the NxN
# neighborhood. 0.5 is the median, 0.25 is the lower quartile, and 0.75
# would be the upper quartile.
img.median(1, percentile=0.5, threshold=True, offset=5, invert=True)
print(clock.fps()) # 注意:您的 OpenMV Cam 在连接到
# 计算机时运行速度会减半。断开连接后,FPS 应该会增加。