1.色块识别实战
目标识别出色块,并用矩形框框起来,输出色块的各个信息
1.首先需要打开感光元件配置相关参数
import sensor#引入感光元件的模块
# 设置摄像头
sensor.reset()#初始化感光元件
sensor.set_pixformat(sensor.RGB565)#设置为彩色
sensor.set_framesize(sensor.QVGA)#设置图像的大小
sensor.set_auto_gain(False)# 自动增益开启(True)或者关闭(False)。在使用颜色追踪时,需要关闭自动增益。
sensor.set_auto_whitebal(False)#关闭白平衡
sensor.skip_frames()#跳过n张照片,在更改设置后,跳过一些帧,等待感光元件变稳定。
什么时候需要开曝光呢?答:正对光线弱时需要开曝光
什么时候需要开白平衡呢?
2.我们还需要获取图像才能这这个色块
img=sensor.snapshot()#获取一张图像
img是我们定义的一个引用来指定这个图像对象
3.从图像中获取想要颜色的色块
这里我以黑色的眼镜盒为例:
获取黑色色块img.find_blobs()->会返回一个列表
black_blobs = img.find_blobs(thresholds, roi=Auto, x_stride=2, y_stride=1, invert=False, area_threshold=10, pixels_threshold=10, merge=False, margin=0, threshold_cb=None, merge_cb=None)
- thresholds是颜色的阈值,注意:这个参数是一个列表,可以包含多个颜色。如果你只需要一个颜色,那么在这个列表中只需要有一个颜色值,如果你想要多个颜色阈值,那这个列表就需要多个颜色阈值。注意:在返回的色块对象blob可以调用code方法,来判断是什么颜色的色块。
red = (xxx,xxx,xxx,xxx,xxx,xxx)#颜色元组会放在列表里传入创建色块函数 blue = (xxx,xxx,xxx,xxx,xxx,xxx)#这里面的值是可以通过IDE看到的 yellow = (xxx,xxx,xxx,xxx,xxx,xxx) img=sensor.snapshot() #创建一种颜色色块 red_blobs = img.find_blobs([red]) #创建多种颜色色块 color_blobs = img.find_blobs([red,blue, yellow])
-
roi是“感兴趣区”。
left_roi = [0,0,160,240]
blobs = img.find_blobs([red],roi=left_roi) -
x_stride 就是查找的色块的x方向上最小宽度的像素,默认为2,如果你只想查找宽度10个像素以上的色块,那么就设置这个参数为10:
blobs = img.find_blobs([red],x_stride=10)
-
y_stride 就是查找的色块的y方向上最小宽度的像素,默认为1,如果你只想查找宽度5个像素以上的色块,那么就设置这个参数为5:
blobs = img.find_blobs([red],y_stride=5)
-
invert 反转阈值,把阈值以外的颜色作为阈值进行查找
-
area_threshold 面积阈值,如果色块被框起来的面积小于这个值,会被过滤掉
-
pixels_threshold 像素个数阈值,如果色块像素数量小于这个值,会被过滤掉
-
merge 合并,如果设置为True,那么合并所有重叠的blob为一个。
注意:这会合并所有的blob,无论是什么颜色的。如果你想混淆多种颜色的blob,只需要分别调用不同颜色阈值的find_blobs。 - margin 边界,如果设置为1,那么两个blobs如果间距1一个像素点,也会被合并。
获取到这个色块我们就可以找到最大的色块并框起来
找最大色块
def drawMaxblob (blobs):
BlobsAreaMax = 0
MaxBlobs = None
for blob in blobs:
if blob[2]*blob[3] > BlobsAreaMax:
MaxBlobs = blob#保存最大面积的色块
BlobsAreaMax = blob[2]*blob[3]
return MaxBlobs
2.在图像上画图
画线
- image.draw_line(line_tuple, color=White) 在图像中画一条直线。
- line_tuple的格式是(x0, y0, x1, y1),意思是(x0, y0)到(x1, y1)的直线。
- 颜色可以是灰度值(0-255),或者是彩色值(r, g, b)的tupple。默认是白色
画框
- image.draw_rectangle(rect_tuple, color=White) 在图像中画一个矩形框。
- rect_tuple 的格式是 (x, y, w, h)。
画圆
- image.draw_circle(x, y, radius, color=White) 在图像中画一个圆。
- x,y是圆心坐标
- radius是圆的半径
画十字
- image.draw_cross(x, y, size=5, color=White) 在图像中画一个十字
- x,y是坐标
- size是两侧的尺寸
写字
- image.draw_string(x, y, text, color=White) 在图像中写字 8x10的像素
- x,y是坐标。使用\n, \r, and \r\n会使光标移动到下一行。
- text是要写的字符串。
总体代码
# Untitled - By: 17334 - Tue May 14 2024
import sensor, image, time
sensor.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QVGA)
sensor.set_auto_gain(False)# 自动增益开启(True)或者关闭(False)。在使用颜色追踪时,需要关闭自动增益。
sensor.set_auto_whitebal(False)#关闭白平衡
sensor.skip_frames(time = 2000)
clock = time.clock()
value_black = [(0, 17, -3, 5, 14, -4)]
value_white = [(45, 98, -8, 10, -6, 9)]
#找到面积最大的色块相关函数
#find_blobs()
#输入寻找色块颜色阈值
def find_blobs (img,color):
black_blobs_list = img.find_blobs(color,x_stride = 50, y_stride = 50)
return black_blobs_list
#drawMaxblob()
#输入寻找色块最大的那个
def drawMaxblob (blobs):
BlobsAreaMax = 0
MaxBlobs = None
for blob in blobs:
if blob[2]*blob[3] > BlobsAreaMax:
MaxBlobs = blob#保存最大面积的色块
BlobsAreaMax = blob[2]*blob[3]
return MaxBlobs
#找到最大颜色的色块并将其框起来
def find_color_MAX_blob(img,color):
ObjectBlobslist = find_blobs(img,color)
MaxBlob = drawMaxblob(ObjectBlobslist)
if MaxBlob:
img.draw_rectangle(MaxBlob.rect())
img.draw_cross(MaxBlob.cx(),MaxBlob.cy())
while(True):
img = sensor.snapshot()
clock.tick()
find_color_MAX_blob(img,value_black)
find_color_MAX_blob(img,value_white)