yanseshibie312+213。。。

# Multi Color Blob Tracking Example
#
# This example shows off multi color blob tracking using the OpenMV Cam.

#(39, 54, 53, 88, 9, 56) 
#(34, 52, 44, 69, 12, 49)

import sensor, image, time, math
# Color Tracking Thresholds (L Min, L Max, A Min, A Max, B Min, B Max)7, 60, 19, 127, -16, 127
# The below thresholds track in general red/green things. You may wish to tune them...
redthreshold = (30, 55, 40, 80, 12, 50) # generic_red_thresholds(40, 62, 29, 70, -3, 52)
greenthreshold=(18, 59, -51, -19, -9, 27) # generic_green_thresholds
bluethreshold=(30, 60, -43, 30, -60, -16) # 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.
rx=[]
ry=[]
gx=[]
gy=[]
bx=[]
by=[]
ared=0
agreen=0
ablue=0
while(True):
    clock.tick()
    time.sleep(2)
    img = sensor.snapshot()
    #print(rx)
    #print(ry)
    #扫描5次红色,把5次的奇数项拿出来,放在一个数组里面,进行排序,是一个色块的,把偶数项放到一个数组里面,得到色块B,两个进行中值滤波,得到A色块和B色块的坐标。
    while ared<5:
        img = sensor.snapshot()
        redblobs= img.find_blobs([redthreshold],pixels_threshold=200, area_threshold=200)
        if redblobs:
            for blob in redblobs:
                # These values depend on the blob not being circular - otherwise they will be shaky.
                if blob.elongation()> 0.25:
                    img.draw_edges(blob.min_corners(), color=(255,0,0))
                    img.draw_line(blob.major_axis_line(), color=(0,255,0))
                    img.draw_line(blob.minor_axis_line(), color=(0,0,255))
                    # These values are stable all the time.
                    img.draw_rectangle(blob.rect())
                    img.draw_cross(blob.cx(), blob.cy())
                    # Note - the blob rotation is unique to 0-180 only.
                    img.draw_keypoints([(blob.cx(), blob.cy(), int(math.degrees(blob.rotation())))], size=20)
                    time.sleep(50)
                    rx.append(blob.cx())
                    ry.append(blob.cy())
                    print("red find red find red fine red red red")
            ared=1+ared
            

    ##对坐标进行排序,
    if ared==5:
        ared=6
        print("rx ry sort before")
        print(rx)
        print(ry)
        rxa=(rx[0],rx[2],rx[4],rx[6],rx[8])
        rxb=(rx[1],rx[3],rx[5],rx[7],rx[9])
        rya=(ry[0],ry[2],ry[4],ry[6],ry[8])
        ryb=(ry[1],ry[3],ry[5],ry[7],ry[9])

        rxsa=sorted(rxa)
        rxsb=sorted(rxb)

        rysa=sorted(rya)
        rysb=sorted(ryb)

        print("rxs,rys---begin")
        print(rxsa)
        print(rysa)

        print(rxsb)
        print(rysb)
        print("rxs,rys--*******--end")
    #取中间的作为最后的使用数值,有5个元素,取第三个元素,rx[2],ry[2]

    #print(rx)
    #print(ry)
    #扫描5次绿色,得到坐标放入gx,gy中,对gx,gy坐标进行排序,取中间数值作为有效数值
    while agreen<5:
        img = sensor.snapshot()
        greenblobs= img.find_blobs([greenthreshold], pixels_threshold=200, area_threshold=200)
        for blob in greenblobs:
            # These values depend on the blob not being circular - otherwise they will be shaky.
            if blob.elongation() > 0.35:
                img.draw_edges(blob.min_corners(), color=(255,0,0))
                img.draw_line(blob.major_axis_line(), color=(0,255,0))
                img.draw_line(blob.minor_axis_line(), color=(0,0,255))
                # These values are stable all the time.
                img.draw_rectangle(blob.rect())
                img.draw_cross(blob.cx(), blob.cy())
                    # Note - the blob rotation is unique to 0-180 only.
                img.draw_keypoints([(blob.cx(), blob.cy(), int(math.degrees(blob.rotation())))], size=20)
                gx.append(blob.cx())
                gy.append(blob.cy())
            time.sleep(50)
        agreen=agreen+1
    ##对坐标进行排序,
    if agreen==5:
        print("gx gy sort before")
        print(gx)
        print(gy)
        gxa=(gx[0],gx[2],gx[4],gx[6],gx[8])
        gxb=(gx[1],gx[3],gx[5],gx[7],gx[9])
        gya=(gy[0],gy[2],gy[4],gy[6],gy[8])
        gyb=(gy[1],gy[3],gy[5],gy[7],gy[9])

        gxsa=sorted(gxa)
        gxsb=sorted(gxb)

        gysa=sorted(gya)
        gysb=sorted(gyb)

        print("gxs,gys---begin")
        print(gxsa)
        print(gysa)

        print(gxsb)
        print(gysb)

        print("gxs,gys----------end")
        agreen=6
    #取中间的作为最后的使用数值,有5个元素,取第三个元素,gx[2],gy[2]

    ##print(rx)
    ##print(ry)
    #扫描5次绿色,得到坐标放入gx,gy中,对gx,gy坐标进行排序,取中间数值作为有效数值
    while ablue<5:
        img = sensor.snapshot()
        blueblobs= img.find_blobs([bluethreshold], pixels_threshold=120, area_threshold=120)
        if blueblobs:
          for blob in blueblobs:
            # These values depend on the blob not being circular - otherwise they will be shaky.
            if blob.elongation() > 0.35:
                img.draw_edges(blob.min_corners(), color=(255,0,0))
                img.draw_line(blob.major_axis_line(), color=(0,255,0))
                img.draw_line(blob.minor_axis_line(), color=(0,0,255))
                # These values are stable all the time.
                img.draw_rectangle(blob.rect())
                img.draw_cross(blob.cx(), blob.cy())
                    # Note - the blob rotation is unique to 0-180 only.
                img.draw_keypoints([(blob.cx(), blob.cy(), int(math.degrees(blob.rotation())))], size=20)
                bx.append(blob.cx())
                by.append(blob.cy())
            time.sleep(50)
          ablue=ablue+1
    ##对坐标进行排序,
    if ablue==5:
        print("bx by sort before")
        print(bx)
        print(by)
        bxa=(bx[0],bx[2],bx[4],bx[6],bx[8])
        bxb=(bx[1],bx[3],bx[5],bx[7],bx[9])
        bya=(by[0],by[2],by[4],by[6],by[8])
        byb=(by[1],by[3],by[5],by[7],by[9])

        bxsa=sorted(bxa)
        bxsb=sorted(bxb)

        bysa=sorted(bya)
        bysb=sorted(byb)

        print("bxs,bys---begin")
        print(bxsa)
        print(bysa)

        print(bxsb)
        print(bysb)

        print("bxs,bys----end")
        ablue=6
    #取中间的作为最后的使用数值,有5个元素,取第三个元素,gx[2],gy[2]

    #将结果进行处理,上层的坐标,和下层的坐标分别进行比较,得到顺序
    #先将坐标分层,图片坐标是320*240,因此y方向坐标大于240/2=120的是下层,或者两个对比,y坐标大的是下层
    if rya[2]>ryb[2]:
        r_yup=ryb[2]
        r_ydown=rya[2]

        r_xup=rxb[2]
        r_xdown=rxa[2]
    else:
        r_yup=rya[2]
        r_ydown=ryb[2]

        r_xup=rxa[2]
        r_xdown=rxb[2]

    if gya[2]>gyb[2]:
        g_yup=gyb[2]
        g_ydown=gya[2]

        g_xup=gxb[2]
        g_xdown=gxa[2]


    else:
        g_yup=gya[2]
        g_ydown=gyb[2]

        g_xup=gxa[2]
        g_xdown=gxb[2]

    if bya[2]>byb[2]:
        b_yup=byb[2]
        b_ydown=bya[2]

        b_xup=bxb[2]
        b_xdown=bxa[2]
    else:
        b_yup=bya[2]
        b_ydown=byb[2]
        b_xup=bxa[2]
        b_xdown=bxb[2]

    print("A,A,A ")
    print(r_xup,r_yup,g_xup,g_yup,b_xup,b_yup)
    print("D,D,D ")
    print(r_xdown,r_ydown,g_xdown,g_ydown,b_xdown,b_ydown)

    #得到上层和下层的颜色的顺序号,方法,比较x的坐标,排顺序,找到最大值,最大的是右边,找到最小值,最小的在左边

    if r_xup >  g_xup and g_xup > b_xup:
       color_up="123"
    elif r_xup > b_xup and b_xup >g_xup:
       color_up="132"
    elif g_xup > r_xup and r_xup > b_xup:
       color_up="213"
    elif g_xup> b_xup and b_xup > r_xup:
       color_up="231"
    elif b_xup > r_xup and r_xup > g_xup:
       color_up="312"
    else:
       color_up="321"


    if r_xdown >  g_xdown and g_xdown > b_xdown:
       color_down="123"
    elif r_xdown > b_xdown and b_xdown >g_xdown:
       color_down="132"
    elif g_xdown > r_xdown and r_xdown > b_xdown:
       color_down="213"
    elif g_xdown> b_xup and b_xdown > r_xdown:
       color_down="231"
    elif b_xdown > r_xdown and r_xdown > g_xdown:
       color_down="312"
    else:
       color_down="321"
    print("**************************")
    print(color_up)
    print(color_down)
    print("**************************")












评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值