星瞳科技给的方法试过了,利用线的角度和来识别,但结果误识别很严重 于是想出了 利用三角形面积占比为外接矩形的一半 再根据与三角形在相机中的周长进一步筛选 应该还可以根据三角形的边长做文章(有待完善) 圆形和矩形都要思考下边长和面积 同理可证
思路简单
调整合适的阈值
thresholds = [(0, 25, 28, 49, 41, 90),
(14, 87, -83, 127, -113, -14),
(30, 44, -46, -9, 7, 44)] #三个阈值更准确些
合适的面积范围
if blob.density()>0.80:
A=...
elif blob.density()>0.60:
A= ...
elif blob.density()>0.40:
A= ...
合理的周长范围
if blob.w()<78:
A= 0
if blob.w()>100:
A= 0
上全部代码
import sensor, image, time, math
from pyb import UART
#thresholds是指颜色阈值,把你想要检测的颜色阈值写成一个元组放进这个list
thresholds = [(0, 25, 28, 49, 41, 90),
(14, 87, -83, 127, -113, -14),
(30, 44, -46, -9, 7, 44)] #三个阈值更准确些
sensor.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QQVGA)
sensor.skip_frames(time = 2000)
sensor.set_auto_gain(False) # must be turned off for color tracking
sensor.set_auto_whitebal(False) # must be turned off for color tracking
clock = time.clock()
sensor.set_auto_gain(False)
sensor.set_auto_whitebal(False)
while(True):
clock.tick()
img = sensor.snapshot()
uart = UART(3,115200)
uart.init(115200,bits=8,parity=None,stop=1)
deflection_math= 0
A=deflection_math
for blob in img.find_blobs(thresholds,pixels_threshold=200,area_threshold=200):
if blob.density()>0.80:
A=0
elif blob.density()>0.60:
A= 0
elif blob.density()>0.40:
A= 3
print("检测为三角型 ",end='')
img.draw_cross(blob.cx(), blob.cy())
print('三角型边长',blob.w())
else:
A= 0
#print("no dectedtion")
if blob.w()<78:
A= 0
if blob.w()>100:
A= 0
uart_buf = bytearray([int (A)])
uart.write(uart_buf)
time.sleep(0)#延时