logo

为什么这个程序他只能识别出红色?

2021-10-30 17:40
T
小侯要加油
import sensor, image, time, math
 
# Color Tracking Thresholds (L Min, L Max, A Min, A Max, B Min, B Max)
# The below thresholds track in general red/green things. You may wish to tune them...
thresholds = [(13, 30, 1, 45, -24, 47), # generic_red_thresholds
             (23, 55, -44, -24, -2, 39), # generic_green_thresholds
             (0, 15, 0, 40, -80, -20)] # generic_blue_thresholds
# You may pass up to 16 thresholds above. However, it's not really possible to segment any
# scene with 16 thresholds before color thresholds start to overlap heavily.
 
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()
 
# Only blobs that with more pixels than "pixel_threshold" and more area than "area_threshold" are
# returned by "find_blobs" below. Change "pixels_threshold" and "area_threshold" if you change the
# camera resolution. Don't set "merge=True" becuase that will merge blobs which we don't want here.
 
 
while(True):
   clock.tick()
   red=[]
   green=[]
   blue=[]
   shit=[]
   order=[]
   img = sensor.snapshot().lens_corr(1.8)
   for r in img.find_rects(threshold = 10000):
       img.draw_rectangle(r.rect(), color = (255, 0, 0))
       for p in r.corners(): img.draw_circle(p[0], p[1], 5, color = (0, 255, 0))
       area = (r.rect())
       #area为识别到的矩形的区域
       statistics = img.get_statistics(roi=area)#像素颜色统计i=0,j=0,n=0
       #print(statistics)
       #(0,100,0,120,0,120)是红色的阈值
       #l_mode(),a_mode(),b_mode()是L通道,A通道,B通道的众数。
       if 8<statistics.l_mode()<20 and 10<statistics.a_mode()<45 and 0<statistics.b_mode()<36:
 
          img.draw_rectangle(area, color = (255, 0, 0))#识别到的红色矩形用红色的矩形框出来
          if 1.8<(r.w()/r.h())<2.2:
 
               red.append(r.y())
 
          elif (0.8<(r.w()/r.h())<1.2):
 
 
               red.append(r.y())
               red.append(r.y()-r.h()/2)
 
 
           elif (0.5<=(r.w()/r.h())<=0.79):
 
               red.append(r.y()-2*r.h()/3)
               red.append(r.y()-r.h()/3)
               red.append(r.y())
 
       if 30<statistics.l_mode()<51 and -45<statistics.a_mode()<-21 and 9<statistics.b_mode()<46:
 
           img.draw_rectangle(area, color = (255, 0, 0))
           if 1.8<=r.w()/r.h()<=2.2:
 
               green.append(r.y())
 
           elif(0.8<=r.w()/r.h()<=1.2):
 
               green.append(r.y())
               green.append(r.y()-r.h()/2)
 
           elif(0.5<=r.w()/r.h()<0.8):
 
               green.append(r.y())
               green.append(r.y()-2*r.h()/3)
               green.append(r.y()-r.h()/3)
 
       if 44<statistics.l_mode()<55 and -30<statistics.a_mode()<-5 and -24<statistics.b_mode()<0:
 
           img.draw_rectangle(area, color = (255, 0, 0))
           if 1.8<=r.w()/r.h()<=2.2:
 
               blue.append(r.y())
 
           elif(0.8<=r.w()/r.h()<=1.2):
 
               blue.append(r.y()-r.h()/2)
               blue.append(r.y())
 
           elif(0.5<=r.w()/r.h()<0.8):
 
               blue.append(r.y())
               blue.append(r.y()-2*r.h()/3)
               blue.append(r.y()-h/3)
 
   l1=len(red)
   l2=len(green)
   l3=len(blue)
   i=0
   while i<l1:
      shit.append(red[i])
      order.append(3)
      i=i+1
   while i<(l1+l2):
 
       shit.append(green[i-l1])
       order.append(1)
       i=i+1
 
   while i<(l1+l2+l3):
 
       shit.append(blue[i-l1-l2])
       order.append(2)
       i=i+1
 
   i=0
   n=0
   print('red')
   print(red)
   while len(shit)<6:
 
       shit.append(0)
       order.append(0)
       n=n+1
 
    for i in range(5):
       for j in range(5-i):
           if shit[j]>shit[j+1]:
 
               shit[j],shit[j+1]=shit[j+1],shit[j]
               order[j],order[j+1]=order[j+1],order[j]
 
 
 
   print(order)
T
小侯要加油

这段代码大致就是六个色块,从上往下排列,一个色块有一种颜色(总共三种),一个数字代表一个色块,然后你按顺序输出对应的数字,但是他最后只能识别到红色,(我觉得我阈值设置的没有问题

2021-11-02 13:42
I
网红小智智 星瞳科技

在第42行,62行,80行,都加入print,你就能看出来到底是什么。逻辑问题自己慢慢就能挑出来。