public void Process(IplImage frame) { if (UpdateColorsEachFrame) { _cvScalarFrom = UnityCvUtils.ColorToBGRScalar(ThreshFromColor); _cvScalarTo = UnityCvUtils.ColorToBGRScalar(ThreshToColor); } var imgThresh = GetThresholdedImage(frame, _cvScalarFrom, _cvScalarTo); CvMoments moments; Cv.Moments(imgThresh, out moments, true); var moment10 = Cv.GetSpatialMoment(moments, 1, 0); var moment01 = Cv.GetSpatialMoment(moments, 0, 1); var area = Cv.GetSpatialMoment(moments, 0, 0); ColorPosition = new Vector2((float)(moment10 / area), (float)(moment01 / area)); ColorPosition.Scale(new Vector2((float)Screen.width / frame.Width, (float)Screen.height / frame.Height)); ColorPosition -= new Vector2(Screen.width, Screen.height) * .5f; Cv.ReleaseImage(imgThresh); }
public void Awake() { UnityCvBase.Init(WebCamTexture.devices[0].name, RequestedWidth, RequestedHeight, RequestedFps, ImagesDepth); TargetRenderer.sharedMaterial.mainTexture = UnityCvBase.WebCamTexture; Camera.main.orthographicSize = Screen.height / 2f; var tr = TargetRenderer.transform; tr.localScale = new Vector3(Screen.width, Screen.height); _cvScalarFrom = UnityCvUtils.ColorToBGRScalar(ThreshFromColor); _cvScalarTo = UnityCvUtils.ColorToBGRScalar(ThreshToColor); if (FollowerPrefab) { _dummyTransform = ((GameObject)Instantiate(FollowerPrefab)).transform; } if (ShowThresholdedImage) { Cv.NamedWindow("Threshold", WindowMode.FreeRatio); } WebCamTextureProxy.OnFrameReady += Process; }