logo

怎么能很好的同时实现颜色识别和直线识别?

2020-05-29 15:24
O
_6lm5

进行的是红色的颜色识别,单独进行颜色识别或直线识别的话,是可以完成的,可是把代码放在一起,无论颜色识别还是直线识别都会特别乱,识别出一些不必要的东西

58361be0-e9d6-4b6d-9d85-f624fe79df2e.png
enable_lens_corr = False
import sensor, image, time, math,usocket, sys, json,socket,network

threshold_index = 0
thresholds = [(0, 100, 0, 127, 0, 127)]
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()

def find_max(blobs):
    max_size=0
    for blob in blobs:
        if blob.pixels() > max_size:
            max_blob=blob
            max_size = blob.pixels()
    return max_blob
    
min_degree = 0
max_degree = 179

while(True):
 #   clock.tick()
    img = sensor.snapshot()
   # for blob in img.find_blobs(thresholds, pixels_threshold=10, area_threshold=10):
    blobs = img.find_blobs(thresholds, pixels_threshold=20, area_threshold=20, merge=True)
    if blobs:
        max_blob=find_max(blobs )
        for blob in blobs:
            if blob.code():
                img.draw_cross(blob.cx(), blob.cy())
                img.draw_rectangle(blob.rect())
        if blob.cx() > img.width()/2:
          
            print(03)
         
        elif blob.cx() < img.width()/2:
        
            print(04)
         
        else:
          
            print(00)
        

        if blob.cy() < img.height()/2:
         
            print(01)
          
        else:
        
            print(00)
          
        if enable_lens_corr: img.lens_corr(1.8)
        for l in img.find_lines(threshold = 1000, theta_margin = 25, rho_margin = 25):
            if (min_degree <= l.theta()) and (l.theta() <= max_degree):
                img.draw_line(l.line(), color = (255, 0, 0))
                if l.x1() > img.height()/2:
         
                    print(16)
        
                else:
        
                    print(16)
2020-05-29 18:37
I
网红小智智 星瞳科技

draw需要放在最后面,否则找直线会把手动画的框识别到。