OpenMV例程

2018.7.27光电赛用到openmv

openmv识别场地灯小例

import sensor, image, time
import cpufreq
from pyb import UART
import math
red_threshold = (19, 63, 2, 81, 0, 127)
#(22, 63, 43, 81, 0, 127)
#(22, 100, 28, 127, -128, 127)

cpufreq.set_frequency(cpufreq.CPUFREQ_216MHZ)
uart = UART(3, 115200)
uart.init(115200, bits=8, parity=None, stop=1)

sensor.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QQVGA)
sensor.set_auto_gain(False, gain_db=1)
sensor.set_auto_exposure(False,2100)
#3200 2100
sensor.set_auto_whitebal(False)
sensor.set_contrast(3)
sensor.set_saturation(3)
sensor.set_brightness(0)
sensor.skip_frames(10)
clock = time.clock()

while(True):
    clock.tick() # Track elapsed milliseconds between snapshots().
    img = sensor.snapshot() # Take a picture and return the image.
    blobs = img.find_blobs([red_threshold], area_threshold=0,merge=False)
    if blobs:
        for b in blobs:
            img.draw_rectangle(b[0:4],color = (255,255,255)) # rect
            img.draw_cross(b.cx(),b.cy(),color=(255,255,255)) # cx, cy
        aim_x = b.cx()-80#转换xy坐标
        aim_y = 60-b.cy()
        print(aim_x,aim_y)

        T_sum =0
        buf =["","","","","",""];
        buf[0] = 0xA0;
        buf[1] = ((aim_x & 0Xff00) >> 8);
        buf[2] = ((aim_x & 0X00ff));
        buf[3] = ((aim_y & 0Xff00) >> 8);
        buf[4] = ((aim_y & 0X00ff));

        for i in range(5):
            T_sum += buf[i]

        buf[5] =(T_sum & 0x00ff)

        for j in buf:
            uart.writechar(j)
else :
        T_sum =0
        buf =["","","","","",""];
        buf[0] = 0xA0;
        buf[1] = (((999) & 0Xff00) >> 8);
        buf[2] = (((999) & 0X00ff));
        buf[3] = (((999) & 0Xff00) >> 8);
        buf[4] = (((999) & 0X00ff));

        for i in range(5):
            T_sum += buf[i]

        buf[5] =(T_sum & 0x00ff)

        for j in buf:
            uart.writechar(j)
#print(clock.fps())
Donate
  • Copyright: Copyright is owned by the author. For commercial reprints, please contact the author for authorization. For non-commercial reprints, please indicate the source.

扫一扫,分享到微信

微信分享二维码
  • Copyrights © 2018-2021 Quincy
  • Visitors: | Views:

请我吃串串呗~

支付宝
微信