logo

判断色块坐标是否在画面中心坐标一定范围内,报错?希望能看下我这逻辑代码是否可以?

2019-04-27 11:50
6
uye4
# Blob Detection Example
#
# This example shows off how to use the find_blobs function to find color
# blobs in the image. This example in particular looks for dark green objects.

import sensor, image, time, pyb,mjpeg
from pyb import UART,Pin,Timer
import utime
import json
uart=pyb.UART(3,115200,timeout_char=1000)
uart.init(115200, bits=8, parity=None, stop=1)

p_out = Pin('P7',Pin.OUT_PP)
p_out.low()

# For color tracking to work really well you should ideally be in a very, very,
# very, controlled enviroment where the lighting is constant...
blue_threshold_01 = (0, 100, -16, 127, -128, -31)

#设置蓝色的阈值,括号里面的数值分别是L A B 的最大值和最小值(minL, maxL, minA,
# maxA, minB, maxB),LAB的值在图像左侧三个坐标图中选取。如果是灰度图,则只需
#设置(min, max)两个数字即可。

# You may need to tweak the above settings for tracking green things...
# Select an area in the Framebuffer to copy the color settings.

sensor.reset() # Initialize the camera sensor.
sensor.set_pixformat(sensor.RGB565) # use RGB565.
sensor.set_framesize(sensor.QVGA) # use QVGA for quailtiy ,use QQVGA for speed.
sensor.skip_frames(10) # Let new settings take affect.
sensor.set_auto_whitebal(False)
#关闭白平衡。白平衡是默认开启的,在颜色识别中,需要关闭白平衡。
clock = time.clock() # Tracks FPS.

'''
  扩宽roi
'''
def expand_roi(roi):
    # set for QQVGA 160*120
    extra = 5
    win_size = (160, 120)
    (x, y, width, height) = roi
    new_roi = [x-extra, y-extra, width+2*extra, height+2*extra]

    if new_roi[0] < 0:
        new_roi[0] = 0
    if new_roi[1] < 0:
        new_roi[1] = 0
    if new_roi[2] > win_size[0]:
        new_roi[2] = win_size[0]
    if new_roi[3] > win_size[1]:
        new_roi[3] = win_size[1]

    return tuple(new_roi)





while(True):
    clock.tick() # Track elapsed milliseconds between snapshots().
    img = sensor.snapshot() # Take a picture and return the image.
    #  pixels_threshold=100, area_threshold=100
    blobs = img.find_blobs([blue_threshold_01], area_threshold=150)

    if blobs:
    #如果找到了目标颜色
      # print(blobs)
       for blob in blobs:
       #迭代找到的目标颜色区域
            is_circle = False
            max_circle = None
            max_radius = -1

            new_roi = expand_roi(blob.rect())

            for c in img.find_circles(threshold = 2000, x_margin = 20, y_margin = 20, r_margin = 10, roi=new_roi):
                is_circle = True
                # img.draw_circle(c.x(), c.y(), c.r(), color = (255, 255, 255))
                if c.r() > max_radius:
                    max_radius = c.r()
                    max_circle = c
            if is_circle:
                # 如果有对应颜色的圆形 标记外框
                # Draw a rect around the blob.
                img.draw_rectangle(new_roi) # rect
                img.draw_rectangle(blob.rect()) # rect
                #用矩形标记出目标颜色区域
                img.draw_cross(blob[5], blob[6]) # cx, cy
                img.draw_circle(max_circle.x(), max_circle.y(), max_circle.r(), color = (0, 0, 255))
                img.draw_circle(max_circle.x(), max_circle.y(), max_circle.r() + 1, color = (0, 0, 255))
                print(blob[5], blob[6])
                z=100            
                x_h=(blob[5]>>8)&0xff
                x_l=blob[5]&0xff
                y_h=(blob[6]>>8)&0xff
                y_l=blob[6]&0xff
                z_h=(z>>8)&0xff
                z_l=z&0xff
                data=bytearray([0x02,x_h,x_l,y_h,y_l,z_h,z_l,0x06,0x0D,0X0A])
                print('you send:',data)
                uart.write(data)
            if (sensor.width/2 -10<=blob[5]<=sensor.width/2 +10) and (sensor.height/2 -10<=blob[6]<=sensor.height/2 +10):
                z=50
                x_h=(blob[5]>>8)&0xff
                x_l=blob[5]&0xff
                y_h=(blob[6]>>8)&0xff
                y_l=blob[6]&0xff
                z_h=(z>>8)&0xff
                z_l=z&0xff
                data1=bytearray([0x02,x_h,x_l,y_h,y_l,z_h,z_l,0x06,0x0D,0X0A])
                print('you send:',data1)
                uart.write(data1)
                timesleep(1000)
                timesleep(1000)
                timesleep(1000)
                z=0
                x_h=(blob[5]>>8)&0xff
                x_l=blob[5]&0xff
                y_h=(blob[6]>>8)&0xff
                y_l=blob[6]&0xff
                z_h=(z>>8)&0xff
                z_l=z&0xff
                data2=bytearray([0x02,x_h,x_l,y_h,y_l,z_h,z_l,0x06,0x0D,0X0A])
                print('you send:',data2)
                uart.write(data2)
                #p_out.high()


    print(clock.fps()) # Note: Your OpenMV Cam runs about half as fast while
    # connected to your computer. The FPS should increase once disconnected.!
967b4708-78a3-4a39-bde2-37813c2d4d07.png
2019-04-27 12:57
I
网红小智智 星瞳科技

sensor.width是个啥?

函数调用要用括号。

sensor.width()