存储文件
插入sd卡,会将默认储存地址flash改为sd卡。
image.save("/example.jpg")
串口通信
传输面积最大的色块的坐标
import sensor, image, time
from pyb import UART
import json
yellow_threshold = (65, 100, -10, 6, 24, 51)
sensor.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QQVGA)
sensor.skip_frames(10)
sensor.set_auto_whitebal(False)
clock = time.clock()
uart = UART(3, 115200)
def find_max(blobs):
max_size=0
for blob in blobs:
if blob.pixels() > max_size:
max_blob=blob
max_size = blob.pixels()
return max_blob
while(True):
img = sensor.snapshot()
blobs = img.find_blobs([yellow_threshold])
if blobs:
max_blob=find_max(blobs)
print('sum :', len(blobs))
img.draw_rectangle(max_blob.rect())
img.draw_cross(max_blob.cx(), max_blob.cy())
output_str="[%d,%d]" % (max_blob.cx(),max_blob.cy())
print('you send:',output_str)
uart.write(output_str+'\r\n')
else:
print('not found!')
优化图像
直方图均衡化是通过拉伸像素强度分布范围来增强图像对比度的一种方法.说得更清楚一些, 以上面的直方图为例, 你可以看到像素主要集中在中间的一些强度值上. 直方图均衡化要做的就是 拉伸 这个范围. 见下面左图: 绿圈圈出了 少有像素分布其上的 强度值. 对其应用均衡化后, 得到了中间图所示的直方图. 均衡化的图像见下面右图.
import sensor, image, time
from pyb import UART
import json
yellow_threshold = (65, 100, -10, 6, 24, 51)
sensor.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QQVGA)
sensor.skip_frames(10)
sensor.set_auto_whitebal(False)
clock = time.clock()
uart = UART(3, 115200)
def find_max(blobs):
max_size=0
for blob in blobs:
if blob.pixels() > max_size:
max_blob=blob
max_size = blob.pixels()
return max_blob
while(True):
img = sensor.snapshot()
blobs = img.find_blobs([yellow_threshold])
if blobs:
max_blob=find_max(blobs)
print('sum :', len(blobs))
img.draw_rectangle(max_blob.rect())
img.draw_cross(max_blob.cx(), max_blob.cy())
output_str="[%d,%d]" % (max_blob.cx(),max_blob.cy())
print('you send:',output_str)
uart.write(output_str+'\r\n')
else:
print('not found!')
image.histeq([adaptive=False[, clip_limit=-1[, mask=None]]])
在图像上运行直方图均衡算法。 直方图均衡化使图像中的对比度和亮度标准化。
如果 adaptive 传递为True,那么将在图像上运行自适应直方图均衡方法,这通常比非自适应直方图限定更好,但运行时间更长
clip_limit 提供了一种限制自适应直方图均衡的对比度的方法。 使用较小的值(例如10)可以生成良好的直方图均衡对比度受限图像。
mask 是另一个用作绘图操作的像素级掩码的图像。掩码应该是一个只有黑色或白色像素的图像,并且应该与你正在绘制的 image 大小相同。 仅掩码中设置的像素被修改。
返回图像对象,以便您可以使用 . 表示法调用另一个方法。
不支持压缩图像和bayer图像。
线性回归(循迹)
线性回归能找到视野中任何的线,但是处理速度就会很慢,为了能更快的寻到我们想要的轨迹,把颜色调为灰度,不去处理追踪颜色,图像大小设成QQVGA,缩小图像面积,来增加速度。通过二值化,能让我们想要的线条更加清晰,使用binary()来获取图像信息,用mean(2)来消噪。
==image.erode(size[, threshold[, mask=None]])==从分割区域的边缘删除像素。
这一方法通过卷积图像上((size2)+1)x((size2)+1)像素的核来实现,如果相邻像素集的总和小于 threshold ,则对内核的中心像素进行归零。若 threshold 未设定,这个方法的功能如标准腐蚀方法一样。若threshold设定,您就可以指定腐蚀的特定像素,例如:设置低于2个的像素周围阈值为2。
mask 是另一个用作绘图操作的像素级掩码的图像。掩码应该是一个只有黑色或白色像素的图像,并且应该与你正在绘制的 image 大小相同。最后通过image.get_regression(阀值)来找线。
巡线代码如下
while(True):
clock.tick()
img = sensor.snapshot().binary([THRESHOLD]) if BINARY_VISIBLE else sensor.snapshot()
line = img.get_regression([(255,255) if BINARY_VISIBLE else THRESHOLD],roi = ROI,robust = True)
img.mean(2)
if (line):
img.draw_line(line.line(), color = 255)
th = line.theta()
if th > 90:
th = 180 - th
if th > 7:
print('L',th)
else:
if line.theta() > 7:
print('R',line.theta())
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)