logo

请问一下我先识别色块后识别圆形,不用消除桶形畸变,为什么图像还是一卡一卡的。

2019-08-27 22:01
5
euqa
请在这里粘贴代码

# 色块监测 例子

#

# 这个例子展示了如何通过find_blobs()函数来查找图像中的色块

# 这个例子查找的颜色是深绿色

import sensor, image, time, pyb

# 颜色追踪的例子,一定要控制环境的光,保持光线是稳定的。

#green_threshold = (45, 92, -44, -11, -8, 19)

#pink_threshold = (74, 52, 24, 69, -21, 15)#(74, 52, 31, 69, -8, 41)

blue_threshold = (45, 79, -14, 8, -45, -18)

#White_threshold = (93, 100, -13, 2, -2, 14)

#White_threshold = (90, 100, -6, 9, -9, 5)

#Red_threshold = (32, 90, 18, 55, -9, 12)

Black_threshold = (5, 60, -18, 33, -11, 26)

#设置绿色的阈值,括号里面的数值分别是L A B 的最大值和最小值(minL, maxL, minA,

# maxA, minB, maxB),LAB的值在图像左侧三个坐标图中选取。如果是灰度图,则只需

#设置(min, max)两个数字即可。

from pyb import UART

uart = UART(3, 19200)

sensor.reset() # 初始化摄像头

sensor.set_pixformat(sensor.RGB565) # 格式为 RGB565.

sensor.set_framesize(sensor.QQVGA) # 使用 QQVGA 速度快一些

sensor.skip_frames(10) # 跳过10帧,使新设置生效

sensor.set_auto_whitebal(False)

#关闭白平衡。白平衡是默认开启的,在颜色识别中,一定要关闭白平衡。

sensor.set_vflip(True)

clock = time.clock() # 追踪帧率

def find_max(blobs):

max_size=0

for blob in blobs:

if blob[2]*blob[3] > max_size:

max_blob=blob

max_size = blob[2]*blob[3]

return max_blob

while(True):

clock.tick() # Track elapsed milliseconds between snapshots().

img = sensor.snapshot() # 从感光芯片获得一张图像

#img.lens_corr(1.8) #消除桶形畸变

blobs = img.find_blobs([blue_threshold])

if blobs: #如果找到了目标颜色

max_blob = find_max(blobs)

img.draw_cross(max_blob.cx(), max_blob.cy()) # cx, cy #在目标颜色区域的中心画十字形标记

output_str="Coor%03d%03d%03d%02d" % (max_blob.cx(),max_blob.w(),max_blob.h(),clock.fps())

print('R:',output_str)

uart.write(output_str+'\r\n')

for max_blob in img.find_circles(threshold = 3500, x_margin = 10, y_margin = 10, r_margin = 10,

r_min = 2, r_max = 100, r_step = 2):

area = (max_blob.x()-max_blob.r(), max_blob.y()-max_blob.r(), 2*max_blob.r(), 2*max_blob.r())

#area为识别到的圆的区域,即圆的外接矩形框

statistics = img.get_statistics(roi=area)#像素颜色统计

print(statistics)

#(0,100,0,120,0,120)是红色的阈值,所以当区域内的众数(也就是最多的颜色),范围在这个阈值内,就说明是红色的圆。

#l_mode(),a_mode(),b_mode()是L通道,A通道,B通道的众数。

if 45<statistics.l_mode()<79 and -14<statistics.a_mode()<8 and -45<statistics.b_mode()<-18:#if the circle is red

img.draw_circle(max_blob.x(), max_blob.y(), max_blob.r(), color = (255, 0, 0))#识别到的红色圆形用红色的圆框出来

else :

output_no="No"

uart.write(output_no+'\r\n')

blobs = img.find_blobs([Black_threshold])

if blobs: #如果找到了目标颜色

max_blob = find_max(blobs)

img.draw_cross(max_blob.cx(), max_blob.cy()) # cx, cy #在目标颜色区域的中心画十字形标记

output_str="Coor%03d%03d%03d%02d" % (max_blob.cx(),max_blob.w(),max_blob.h(),clock.fps())

print('R:',output_str)

uart.write(output_str+'\r\n')

for max_blob in img.find_circles(threshold = 3500, x_margin = 10, y_margin = 10, r_margin = 10,

r_min = 2, r_max = 100, r_step = 2):

area = (max_blob.x()-max_blob.r(), max_blob.y()-max_blob.r(), 2*max_blob.r(), 2*max_blob.r())

#area为识别到的圆的区域,即圆的外接矩形框

statistics = img.get_statistics(roi=area)#像素颜色统计

print(statistics)

#(0,100,0,120,0,120)是红色的阈值,所以当区域内的众数(也就是最多的颜色),范围在这个阈值内,就说明是红色的圆。

#l_mode(),a_mode(),b_mode()是L通道,A通道,B通道的众数。

if 5<statistics.l_mode()<60 and -18<statistics.a_mode()<33 and -11<statistics.b_mode()<26:#if the circle is red

img.draw_circle(max_blob.x(), max_blob.y(), max_blob.r(), color = (0, 225, 225))#识别到的红色圆形用红色的圆框出来

else :

output_no="No"

uart.write(output_no+'\r\n')

#print(blob.cx(),blob.cy(),clock.fps())

#print(clock.fps()) # 注意: 你的OpenMV连到电脑后帧率大概为原来的一半

请在这里粘贴代码
2019-08-28 14:24
I
网红小智智 星瞳科技

识别圆形计算量大,会慢。

2019-08-28 21:00
5
euqa

那请问如何减小识别圆形计算量?谢谢

2019-08-29 17:28
I
网红小智智 星瞳科技

@euqa 没办法