' DENSO Robot Imaging Library Sample No.2
'
' - イメージ番号11にサーチモデルをグレースケールで格納しておく．

Sub Main
	Set RCB1 = Cao.AddController("RCB1", "CaoProv.KONDO.RCB-1", "", "Port=1,ID=0")
	Set Pos = RCB1.AddVariable("Position")

	Set Vis = Cao.AddController("cv", "CaoProv.OpenCV", "", "")
	Set rawImg = Vis.AddFile("Cam1", "ID=1")
	Set tmpImg = Vis.AddFile("Mem1", "ID=101")

	Do
		' パターンマッチングでターゲットをサーチ
		iT = rawImg.AutoThreshDiscrim(rawImg.CalcHistEx(255))   ' 判別分析法による閾値の算出
        rawImg.ThresholdEx 101, iT, 255, 1                      ' 2値化 & 白黒反転(1)
        Result = tmpImg.MatchShapes2(11, 2, 0.5, 0.01, 1)					' 形状マッチング
		
		if UBound(Result) >= 0 then
			dbg.Output Result(0)(0) & " : " & Result(0)(1) 

			' ターゲットのX-YでRCB1を動かす
			If Result(0)(0) > 180 then
				Result(0)(0) = 180
			end If
			If Result(0)(1) > 180 then
				Result(0)(1) = 180
			end If

			Pos.Value = Array(3, Result(0)(0), Result(0)(1), 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)
		end If
		
		EXT.Delay(100)
	Loop
End Sub
