logo

我想要将颜色识别、形状识别、测距三种融合在一起可以实现吗?

2019-07-23 15:59
T
TAO

一下是我根据例程结合的 但是实现不了功能

import sensor, image, time

sensor.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QVGA)
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()

K=5000#the value should be measured
yellow_threshold   = ( 56,   83,    5,   57,   63,   80)

while(True):
    clock.tick()
    img = sensor.snapshot().lens_corr(1.8)
    for c 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 = (c.x()-c.r(), c.y()-c.r(), 2*c.r(), 2*c.r())
        #area为识别到的圆的区域,即圆的外接矩形框
        statistics = img.get_statistics(roi=area)#像素颜色统计
       #print(statistics)
        #(94, 100, -128, 127, -128, 127)是红色的阈值,所以当区域内的众数(也就是最多的颜色),范围在这个阈值内,就说明是红色的圆。
        #l_mode(),a_mode(),b_mode()是L通道,A通道,B通道的众数。
        if 94<statistics.l_mode()<100 and -128<statistics.a_mode()<127 and -128<statistics.b_mode()<127:#if the circle is red
            img.draw_rectangle(area, color = (255, 255, 255))      
            #将非红色的圆用白色的矩形框出来

    blobs = img.find_blobs([yellow_threshold])
    if len(blobs) == 1:
        # Draw a rect around the blob.
        b = blobs[0]
        img.draw_rectangle(b[0:4]) # rect
        img.draw_cross(b[5], b[6]) # cx, cy
        Lm = (b[2]+b[3])/2
        length = K/Lm
        print(Lm)
2019-07-23 16:39
I
网红小智智 星瞳科技

什么叫做实现不了功能?你想做什么?代码有什么错误?

2019-07-23 17:22
T
TAO

就是我想要同时识别到颜色和形状 然后串口终端返回与物块的距离

现在是识别不到物块 Lm的值也没有返回

2019-07-23 19:18
I
网红小智智 星瞳科技

代码有问题,如果不只有一个色块,底下的就不会执行。

起码你要加一个find_max之类的。

2019-07-23 21:28
T
TAO

那我现在应该怎么修改呢?

import sensor, image, time

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()

K=5000#the value should be measured
yellow_threshold   = (94, 100, -128, 127, -128, 127)

while(True):
    clock.tick()
    img = sensor.snapshot().lens_corr(1.8)
    sensor.set_hmirror(True) #水平方向翻转

    sensor.set_vflip(True) #垂直方向翻转

    for c 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 = (c.x()-c.r(), c.y()-c.r(), 2*c.r(), 2*c.r())
        #area为识别到的圆的区域,即圆的外接矩形框
        statistics = img.get_statistics(roi=area)#像素颜色统计
       #print(statistics)
        #(94, 100, -128, 127, -128, 127)是红色的阈值,所以当区域内的众数(也就是最多的颜色),范围在这个阈值内,就说明是红色的圆。
        #l_mode(),a_mode(),b_mode()是L通道,A通道,B通道的众数。
        if 94<statistics.l_mode()<100 and -128<statistics.a_mode()<127 and -128<statistics.b_mode()<127:#if the circle is red
            img.draw_rectangle(area, color = (255, 255, 255))
            #将非红色的圆用白色的矩形框出来
            
    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
       
    blobs = img.find_blobs([yellow_threshold])
    if len(blobs) == 1:
        # Draw a rect around the blob.
        b = blobs[0]
        img.draw_rectangle(b[0:4]) # rect
        img.draw_cross(b[5], b[6]) # cx, cy
        Lm = (b[2]+b[3])/2
        length = K/Lm
        print(Lm)

这是我重新修改的 还是实现不了 麻烦您指教

2019-07-24 16:27
6
egxs

@4oqs 你这纯复制粘贴。。。。咋会有用呢,k的值没改,距离是不对的,而且输出的是像素,不是距离

另外这个是识别圆形区域的

2019-07-24 20:44
T
TAO

@egxs emmm……那想问问您 方形的识别该怎么写?能教一下我吗回复

T
TAO

@kidswong999 想请教您 我现在该怎么去修改 或者是重新去写?

2019-07-25 12:40
6
egxs

@4oqs

import sensor, image, time

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()

K=500#the value should be measured
yellow_threshold   = (56, 79, 35, 127, -128, 127)
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()
    img = sensor.snapshot().lens_corr(1.8)
    sensor.set_hmirror(False) #水平方向翻转
    sensor.set_vflip(False) #垂直方向翻转
    for r in img.find_rects(threshold = 20000):
        img.draw_rectangle(r.rect(), color = (255, 0, 0))
        area = (r.x(),r.y(),r.w(),r.h())        
        for p in r.corners(): img.draw_circle(p[0], p[1], 5, color = (0, 255, 0))
  #  for c in img.find_circles(threshold = 3500, x_margin = 10, y_margin = 10, r_margin = 10,
   #     r_min = 2, r_max = 100, r_step = 2):
        print (r)
        #area为识别到的圆的区域,即圆的外接矩形框
        statistics = img.get_statistics(roi=area)#像素颜色统计
       #print(statistics)
        #(94, 100, -128, 127, -128, 127)是红色的阈值,所以当区域内的众数(也就是最多的颜色),范围在这个阈值内,就说明是红色的圆。
        #l_mode(),a_mode(),b_mode()是L通道,A通道,B通道的众数。
        if 56<statistics.l_mode()<79 and 35<statistics.a_mode()<127 and -128<statistics.b_mode()<127:#if the circle is red
            img.draw_rectangle(area, color = (255, 255, 255))
            #将非红色的圆用白色的矩形框出来

    blobs = img.find_blobs([yellow_threshold])
    
    if len(blobs) == 1:
        # Draw a rect around the blob.
        max_blob=find_max(blobs)
        b = blobs[0]
        img.draw_rectangle(b[0:4]) # rect
        img.draw_cross(b[5], b[6]) # cx, cy
        Lm = (b[2]+b[3])/2
        length = K/Lm
        #print(Lm)
        print(length)

你试试看,我这里用的是红色的阈值

2019-07-29 15:11
T
TAO

@egxs 感谢您!

但是识别起来还是有点飘 会框到其他的地方

2019-07-30 14:59
0
有点强

@4oqs 在 [我想要将颜色识别、形状识别、测距三种融合在一起可以实现吗?](/post/12649) 中说:

>

> 此回复已被删除!

k值都不改,真的是纯复制粘贴,原理都不没搞清楚,哈哈哈

2019-08-07 11:34
F
n2kt

你好,我想问你的这个问题解决了吗??