赞
踩
主要内容分为两部分,1.颜色识别 2.噪点滤波
首先要用二值化图像来取红球的颜色阈值
将lab的值调到我们需要的颜色为白色时,此时的lab值为我们是想要的小球阈值
将阈值写入识别代码中
- red_threshold = (23, 99, 21, 70, 68, -2) #红球的lab阈值
-
- for blob in img.find_blobs([red_threshold],roi = Range,area_threshold=200, pixels_threshold=200, merge=True, margin=10):
具体find_blobs里每个参数是什么作用看openmv官网
当我正常只进行识别时,通过二值化图像我发现存在非常多的噪点,所以接下来我们需要滤波才能让摄像头传输的是我们需要的小球的位置、大小信息
通过将二值化图像输出到lcd屏幕上时,我发现大多数噪点都比较小,都比我需要识别的物体小。
led显示二值化图像
- import lcd
-
- sensor.reset() # Initialize the camera sensor.
- sensor.set_pixformat(sensor.RGB565) # or sensor.GRAYSCALE
- sensor.set_framesize(sensor.QQVGA2) # Special 128x160 framesize for LCD Shield.
- lcd.init() # Initialize the lcd screen.
-
- while(True):
- img = sensor.snapshot() #开始拍照
- img.binary([red_threshold]) #二极化处理
- img.dilate(1) #腐蚀膨胀过滤边沿噪点
- lcd.display(img) #led显示
所以我决定通过大小滤波,因为要滤掉噪点所以我们需要识别的色块不合成,合成了不需要的噪点与物体合成一个,将我们的实验误差大大增加,则需要改变find_blobs方法中的变量
merge=False
然后通过比较大小进行滤波
- for blob in img.find_blobs([red_threshold],roi = Range,area_threshold=200, pixels_threshold=200, merge=False, margin=10):
- if ((blob.h()*blob.w())>(blob_max_b*blob_max_a)):#取最大的那个色块
- blob_max_b = blob.w()
- blob_max_a = blob.h()
- blob_max_x = blob.cx()
- blob_max_y = blob.cy()
- if(flag == 0):#第一次识别到物体(我单片机上需要进行该操作,其他人不需要则可去掉)
- usart3.write("ATOK\n")
- flag = 1
- if blob_max_x != 0: #识别到物体并发送串口数据
- data.append((blob_max_x,blob_max_y,blob_max_a,blob_max_b))
- data_out = json.dumps(set(data))
- usart3.write(data_out +'\n')
- time.sleep_ms(10)
-
- from pyb import UART
- import sensor, image, time
- import json
- import lcd
-
- sensor.reset()
- sensor.set_pixformat(sensor.RGB565)
- sensor.set_framesize(sensor.QQVGA)
-
- sensor.reset() # Initialize the camera sensor.
- sensor.set_pixformat(sensor.RGB565) # or sensor.GRAYSCALE
- sensor.set_framesize(sensor.QQVGA2) # Special 128x160 framesize for LCD Shield.
- lcd.init() # Initialize the lcd screen.
-
- #sensor.set_auto_gain(False) # must be turned off for color tracking
- #sensor.set_auto_whitebal(False)
-
- clock = time.clock()
- sensor.skip_frames(time = 2000) #相当于delay200次拍照的时间消抖
-
- usart3=UART(3,115200) #初始化串口
- usart3.init(115200, bits=8, parity=None, stop=1) #初始化串口
-
- Range = (0,0,160,120)
- flag = 0
- red_threshold = (23, 99, 21, 70, 68, -2) #红球的lab阈值
-
- #筛选色块函数:
- def blob_filter(img):
- global flag
- blob_max_b=0
- blob_max_a=0
- blob_max_x=0
- blob_max_y=0
- data = []
- img = sensor.snapshot()
- for blob in img.find_blobs([red_threshold],roi = Range,area_threshold=200, pixels_threshold=200, merge=False, margin=10):
- if ((blob.h()*blob.w())>(blob_max_b*blob_max_a)):#取最大的那个色块
- blob_max_b = blob.w()
- blob_max_a = blob.h()
- blob_max_x = blob.cx()
- blob_max_y = blob.cy()
- if(flag == 0):#第一次识别到物体(我单片机上需要进行该操作,其他人不需要则可去掉)
- usart3.write("ATOK\n")
- flag = 1
- if blob_max_x != 0: #识别到物体并发送串口数据
- data.append((blob_max_x,blob_max_y,blob_max_a,blob_max_b))
- data_out = json.dumps(set(data))
- usart3.write(data_out +'\n')
- time.sleep_ms(10)
-
-
-
- while(True):
- img = sensor.snapshot() #开始拍照
- img.binary([red_threshold]) #二极化处理
- img.dilate(1) #腐蚀膨胀过滤边沿噪点
- lcd.display(img) #led显示
- #blobs = img.find_blobs([red_threshold],roi = Range,area_threshold=1000, pixels_threshold=1000, merge=False, margin=10)
-
- blob_filter(img)
![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png)
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。