在K210弄颜色识别,想要将小于一定大小的方框过滤掉,自己在模板上改了代码后就报错,然后搜索问题发现没有解决的前例,于是就来提问了
以下是例程
import sensor
import image
import lcd
import time
lcd.init()
sensor.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QVGA)
sensor.run(1)
green_threshold = (0, 80, -70, -10, -0, 30)
while True:
img=sensor.snapshot()
blobs = img.find_blobs([green_threshold])
if blobs:
for b in blobs:
tmp=img.draw_rectangle(b[0:4])
tmp=img.draw_cross(b[5], b[6])
c=img.get_pixel(b[5], b[6])
lcd.display(img)
这是我改过后的代码
import sensor
import image
import lcd
import time
lcd.init()
sensor.reset(freq=24000000,)
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QVGA)
sensor.run(1)
lcd.rotation(2)
green_threshold = (90, 100, -40, -20, 30, 50)
while True:
img=sensor.snapshot()
blobs = img.find_blobs([green_threshold])
mn = blobs.rest()
wn = mn[2]
hn = mn[3]
image_x = int(wn)
image_y = int(hn)
image_m = image_x*image_y
if image_m >= 100:
if blobs:
for b in blobs:
tmp=img.draw_rectangle(b.rect(),thickness=1)
tmp=img.draw_cross(b[5], b[6])
c=img.get_pixel(b[5], b[6])
lcd.display(img)
print(blobs)
Traceback (most recent call last):
File "", line 18, in
AttributeError: 'list' object has no attribute 'rest'
MicroPython v0.6.2-84-g8fcd84a58 on 2022-08-26; Sipeed_M1 with kendryte-k210
Type "help()" for more information.
去看过元组的数据取出,但没有感觉什么错误
在识别时过滤掉过小的方框,以及十字。
我重新变化了思路,去找了例程。
import sensor,image,lcd,time
#常用初始化
lcd.init()
sensor.reset() #复位摄像头
sensor.set_pixformat(sensor.RGB565) #设置像素格式 RGB565
sensor.set_framesize(sensor.QVGA) #设置帧尺寸 QVGA (320x240)
sensor.skip_frames(time = 2000) #跳过不稳定画面
green_threshold = (85, 95, -35, -30, 10, 40)
#寻找最大色块函数定义
def find_max(blobs):
global max_blob #定义全局变量,防止变量未定义
max_size=0
for blob in blobs:
if blob.area() > max_size:
max_blob=blob
max_size = blob.area()
return max_blob
while True:
img=sensor.snapshot()
blobs = img.find_blobs([green_threshold],merge=True)#把拍摄的一张图片里满足的色块纳入集合中
if blobs:
max_blob = find_max(blobs)#调用函数,返回最大色块 #保证max.blob出现的地方已经定义了
img.draw_rectangle(max_blob.rect(),color=(255,0,0))#用红色框出最大色块
img.draw_string(max_blob.x(),max_blob.y(), "(x,y) =")
img.draw_string(max_blob.x()+40,max_blob.y(), str(max_blob.cx()))
img.draw_string(max_blob.x()+60,max_blob.y(), str(max_blob.cy()))#在框图左上角显示色块的中心坐标
lcd.display(img)
这能实现我想要的功能
其中
blobs.rect() #返回一个矩形元组(x, y, w, h) ,用于如色块边界框的 image.draw_rectangle 等 其他的 image 方法。
需要赋值给相同的元组
在这次中感觉最重要的就是前缀要在它该在的地方