色塊追蹤與線條追蹤

加入KOI 2庫

from future import *
from koi2 import KOI2

初始化KOI 2

koi = KOI2(tx, rx)

初次化KOI 2連接。

參數:

tx: TX 針腳,Robotbit EDU使用P2

rx: RX 針腳,Robotbit EDU使用P12

選擇線條追蹤模式

koi.setModel(16)

選擇線條追蹤模式

選擇目標顏色

koi.colorSwitch(color)

參數:

color: 顏色

color代表

0

紅色

1

2

Blue

3

Yellow

9

自定義(需要校正)

校正自定義顏色

koi.colorCalibration()

校正自定義顏色

獲取色塊數據

koi.xywh[0]

獲取色塊數據

參數:

data: 數據類型

data代表

0

X coordinates

1

Y coordinates

2

Width of blob

3

Height of blob

範例程式: 色塊追蹤

from future import *
from koi2 import KOI2



koi = KOI2('P2', 'P12')
koi.setModel(16)
screen.sync = 0
while True:
  koi.read_from_uart()
  if sensor.btnValue('a'):
    koi.colorSwitch(0)
  if sensor.btnValue('b'):
    koi.colorCalibration()
    koi.colorSwitch(9)
  screen.fill((0, 0, 0))
  screen.text(koi.xywh[0],5,10,1,(255, 255, 255))
  screen.text(koi.xywh[1],5,30,1,(255, 255, 255))
  screen.text(koi.xywh[2],5,50,1,(255, 255, 255))
  screen.text(koi.xywh[3],5,70,1,(255, 255, 255))
  screen.refresh()
509B
Color Blob Tracking.py

選擇線條追蹤模式

koi.setModel(32)

選擇線條追蹤模式

選擇目標顏色

koi.colorSwitch(color)

參數:

color: 顏色

color代表

0

紅色

1

2

Blue

3

Yellow

9

自定義(需要校正)

校正自定義顏色

koi.colorCalibration()

校正自定義顏色

獲取線條數據

koi.xy12[data]

獲取線條數據

參數:

data: The type of data to return.

data代表

0

X1 coordinates

1

Y1 coordinates

2

X2 coordinates

3

Y2

範例程式: 線條追蹤

from future import *
from koi2 import KOI2



koi = KOI2('P2', 'P12')
koi.setModel(32)
screen.sync = 0
while True:
  koi.read_from_uart()
  if sensor.btnValue('a'):
    koi.lineSwitch(3)
  if sensor.btnValue('b'):
    koi.colorCalibration()
    koi.lineSwitch(4)
  screen.fill((0, 0, 0))
  screen.text(koi.xy12[0],5,10,1,(255, 255, 255))
  screen.text(koi.xy12[1],5,30,1,(255, 255, 255))
  screen.text(koi.xy12[2],5,50,1,(255, 255, 255))
  screen.text(koi.xy12[3],5,70,1,(255, 255, 255))
  screen.refresh()
471B
Item Tracking.py

Last updated