// Update is called once per frame void Update() { if (webCamTextureToMatHelper.IsPlaying() && webCamTextureToMatHelper.DidUpdateThisFrame()) { Mat rgbaMat = webCamTextureToMatHelper.GetMat(); comicFilter.Process(rgbaMat, rgbaMat, false); //Imgproc.putText (rgbaMat, "W:" + rgbaMat.width () + " H:" + rgbaMat.height () + " SO:" + Screen.orientation, new Point (5, rgbaMat.rows () - 10), Imgproc.FONT_HERSHEY_SIMPLEX, 1.0, new Scalar (0), 2, Imgproc.LINE_AA, false); Utils.fastMatToTexture2D(rgbaMat, texture); } if (webCamTextureToMatHelper.IsPlaying()) { Matrix4x4 cameraToWorldMatrix = webCamTextureToMatHelper.GetCameraToWorldMatrix(); Matrix4x4 worldToCameraMatrix = cameraToWorldMatrix.inverse; quad_renderer.sharedMaterial.SetMatrix("_WorldToCameraMatrix", worldToCameraMatrix); // Position the canvas object slightly in front // of the real world web camera. Vector3 position = cameraToWorldMatrix.GetColumn(3) - cameraToWorldMatrix.GetColumn(2) * 2.2f; // Rotate the canvas object so that it faces the user. Quaternion rotation = Quaternion.LookRotation(-cameraToWorldMatrix.GetColumn(2), cameraToWorldMatrix.GetColumn(1)); gameObject.transform.position = position; gameObject.transform.rotation = rotation; } }
// Update for unity and holographic emulation void Update() { if (webCamTextureToMatHelper.IsPlaying() && webCamTextureToMatHelper.DidUpdateThisFrame()) { Mat rgbaMat = webCamTextureToMatHelper.GetMat(); //Mat rgbaMatClipROI = new Mat(rgbaMat, processingAreaRect); Imgproc.cvtColor(rgbaMat, grayMat, Imgproc.COLOR_RGBA2GRAY); // todo lo nuevo //9n Imgproc.cvtColor(dstMat, rgbaMat, Imgproc.COLOR_GRAY2RGBA); // //Imgproc.rectangle (rgbaMat, new Point (0, 0), new Point (rgbaMat.width (), rgbaMat.height ()), new Scalar (255, 0, 0, 255), 4); //Imgproc.rectangle (rgbaMat, processingAreaRect.tl(), processingAreaRect.br(), new Scalar (255, 0, 0, 255), 4); // // Utils.fastMatToTexture2D(rgbaMat, texture); // rgbaMatClipROI.Dispose (); } if (webCamTextureToMatHelper.IsPlaying()) { Matrix4x4 cameraToWorldMatrix = webCamTextureToMatHelper.GetCameraToWorldMatrix(); Matrix4x4 worldToCameraMatrix = cameraToWorldMatrix.inverse; quad_renderer.sharedMaterial.SetMatrix("_WorldToCameraMatrix", worldToCameraMatrix); // Position the canvas object slightly in front // of the real world web camera. Vector3 position = cameraToWorldMatrix.GetColumn(3) - cameraToWorldMatrix.GetColumn(2); position *= 1.2f; // Rotate the canvas object so that it faces the user. Quaternion rotation = Quaternion.LookRotation(-cameraToWorldMatrix.GetColumn(2), cameraToWorldMatrix.GetColumn(1)); gameObject.transform.position = position; gameObject.transform.rotation = rotation; } }
// Update is called once per frame void Update() { if (webCamTextureToMatHelper.IsPlaying() && webCamTextureToMatHelper.DidUpdateThisFrame()) { Mat rgbaMat = webCamTextureToMatHelper.GetMat(); Vector3 hitPoint = new Vector3(0, 0, 0); var laserPointerPosition = FindLaserPointer(rgbaMat); if (laserPointerPosition != null) { // Unprojects pixel coordinates into a camera space ray from the camera origin, expressed as a X, Y coordinates on a plane one meter from the camera. UnityEngine.Vector3 pos = new UnityEngine.Vector3((float)((laserPointerPosition.x / rgbaMat.width()) * Screen.currentResolution.width), (float)((laserPointerPosition.y / rgbaMat.height()) * Screen.currentResolution.height), 1.0f); UnityEngine.Vector3 toPos = Camera.main.ScreenToWorldPoint(pos); if ((redSphere.transform.position - toPos).magnitude >= 0.1) { redSphere.transform.position = toPos; toolTipText.text = (toPos - Camera.main.transform.position).magnitude.ToString("0.00") + " m"; } } Utils.fastMatToTexture2D(rgbaMat, texture); } if (webCamTextureToMatHelper.IsPlaying() && isVisibleImage) { Matrix4x4 cameraToWorldMatrix = webCamTextureToMatHelper.GetCameraToWorldMatrix(); Matrix4x4 worldToCameraMatrix = cameraToWorldMatrix.inverse; quad_renderer.sharedMaterial.SetMatrix("_WorldToCameraMatrix", worldToCameraMatrix); // Position the canvas object slightly in front // of the real world web camera. Vector3 position = cameraToWorldMatrix.GetColumn(3) - cameraToWorldMatrix.GetColumn(2) * 2.2f; // Rotate the canvas object so that it faces the user. Quaternion rotation = Quaternion.LookRotation(-cameraToWorldMatrix.GetColumn(2), cameraToWorldMatrix.GetColumn(1)); gameObject.transform.position = position; gameObject.transform.rotation = rotation; } }
// Update is called once per frame void Update() { if (webCamTextureToMatHelper.IsPlaying() && webCamTextureToMatHelper.DidUpdateThisFrame()) { Mat rgbaMat = webCamTextureToMatHelper.GetMat(); Mat rgbaMatClipROI = new Mat(rgbaMat, processingAreaRect); Imgproc.cvtColor(rgbaMatClipROI, grayMat, Imgproc.COLOR_RGBA2GRAY); bgMat.copyTo(dstMatClippingROI); Imgproc.GaussianBlur(grayMat, lineMat, new Size(3, 3), 0); grayMat.get(0, 0, grayPixels); for (int i = 0; i < grayPixels.Length; i++) { maskPixels [i] = 0; if (grayPixels [i] < 70) { grayPixels [i] = 0; maskPixels [i] = 1; } else if (70 <= grayPixels [i] && grayPixels [i] < 120) { grayPixels [i] = 100; } else { grayPixels [i] = 255; maskPixels [i] = 1; } } grayMat.put(0, 0, grayPixels); maskMat.put(0, 0, maskPixels); grayMat.copyTo(dstMatClippingROI, maskMat); Imgproc.Canny(lineMat, lineMat, 20, 120); lineMat.copyTo(maskMat); Core.bitwise_not(lineMat, lineMat); lineMat.copyTo(dstMatClippingROI, maskMat); //Imgproc.putText (dstMat, "W:" + dstMat.width () + " H:" + dstMat.height () + " SO:" + Screen.orientation, new Point (5, dstMat.rows () - 10), Core.FONT_HERSHEY_SIMPLEX, 1.0, new Scalar (0), 2, Imgproc.LINE_AA, false); Imgproc.cvtColor(dstMat, rgbaMat, Imgproc.COLOR_GRAY2RGBA); // //Imgproc.rectangle (rgbaMat, new Point (0, 0), new Point (rgbaMat.width (), rgbaMat.height ()), new Scalar (255, 0, 0, 255), 2); //Imgproc.rectangle (rgbaMat, processingAreaRect.tl(), processingAreaRect.br(), new Scalar (255, 0, 0, 255), 2); // // Utils.fastMatToTexture2D(rgbaMat, texture); rgbaMatClipROI.Dispose(); } if (webCamTextureToMatHelper.IsPlaying()) { Matrix4x4 cameraToWorldMatrix = webCamTextureToMatHelper.GetCameraToWorldMatrix(); Matrix4x4 worldToCameraMatrix = cameraToWorldMatrix.inverse; quad_renderer.sharedMaterial.SetMatrix("_WorldToCameraMatrix", worldToCameraMatrix); // Position the canvas object slightly in front // of the real world web camera. Vector3 position = cameraToWorldMatrix.GetColumn(3) - cameraToWorldMatrix.GetColumn(2); // Rotate the canvas object so that it faces the user. Quaternion rotation = Quaternion.LookRotation(-cameraToWorldMatrix.GetColumn(2), cameraToWorldMatrix.GetColumn(1)); gameObject.transform.position = position; gameObject.transform.rotation = rotation; } }
// Update is called once per frame, in webcam void Update() { if (webCamTextureToMatHelper.IsPlaying() && webCamTextureToMatHelper.DidUpdateThisFrame()) { Mat rgbaMat = webCamTextureToMatHelper.GetMat(); Imgproc.cvtColor(rgbaMat, grayMat, Imgproc.COLOR_RGBA2GRAY); //29n Imgproc.circle(rgbaMat, p1, 6, new Scalar(255, 0, 0, 255), 2); //Imgproc.circle(rgbaMat, p2, 6, new Scalar(0, 255, 0, 255), 2); //Vector3 point = cameraInstance.ScreenToWorldPoint(new Vector3(rgbaMat.width() * 2 / 3, rgbaMat.height() * 2 / 3, cameraInstance.nearClipPlane)); //ray = cameraInstance.ScreenPointToRay(new Vector3(point.x, point.y, 0)); //28n //para saber donde pinta, no para el tracking Imgproc.rectangle(rgbaMat, p1, p2, new Scalar(255, 255, 255, 255), 2, 1, 0); //20n if (storedTouchPoint != null) { selectedPointList.Add(storedTouchPoint); //Imgproc.circle(rgbaMat, storedTouchPoint, 6, new Scalar(0, 0, 255, 255), 2); p1 = new Point(storedTouchPoint.x - 100, storedTouchPoint.y - 100); p2 = new Point(storedTouchPoint.x + 100, storedTouchPoint.y + 100); selectedPointList.Add(p2); //Imgproc.circle(rgbaMat, p2, 6, new Scalar(0, 255, 255, 255), 2); using (MatOfPoint selectedPointMat = new MatOfPoint(selectedPointList.ToArray())) { OpenCVForUnity.Rect region = Imgproc.boundingRect(selectedPointMat); //si se necesita al fin y al cabo Debug.Log("region" + region); SelectTracker(tracker_type); //16n monotracker = TrackerKCF.create(); //8n trackerInitialized = monotracker.init(grayMat, new Rect2d(region.x, region.y, region.width, region.height)); } storedTouchPoint = null; } // aca ya no lo inicializa, update los antesriores if (trackerInitialized) { bool updated = monotracker.update(grayMat, bbox);//8n //pero nunca le pasa la bbox ni las coordenadas, unicamente al momento de tracker.init if (bbox.width != 0 && bbox.height != 0) { //Debug.Log("tracking por aca:" + bbox.x + ";" + bbox.y); Imgproc.rectangle(rgbaMat, bbox.tl(), bbox.br(), new Scalar(255, 255, 255, 255), 2, 1, 0); previousBox = new Rect2d(bbox.x, bbox.y, bbox.width, bbox.height); ///26n esa caja conviertamela a 3D, donde pegue con algo fisico ///26n Vector3 carPosition = ScreenToWorldCords(new Point(), bbox); Vector3 point = cameraInstance.ScreenToWorldPoint(new Vector3((float)bbox.x, (float)bbox.y, cameraInstance.nearClipPlane)); ray = cameraInstance.ScreenPointToRay(new Vector3((float)bbox.x, (float)bbox.y, 0)); //28n if (Physics.Raycast(ray, out hit)) { print("I'm looking at " + hit.transform.position); box3DPosition = hit.transform.position; // Debug.Log("the car is in" + carPosition); } else { print("I'm looking at nothing!"); } } else { if (previousBox != null) { Debug.Log("Se perdio en:" + previousBox.x + ";" + previousBox.y); Imgproc.rectangle(rgbaMat, previousBox.tl(), previousBox.br(), new Scalar(255, 0, 0, 255), 2, 1, 0); //8n } } Imgproc.putText(rgbaMat, "Tap again to select a new tracking region.", new Point(5, rgbaMat.rows() - 10), Core.FONT_HERSHEY_SIMPLEX, 0.8, new Scalar(255, 255, 255, 255), 2, Imgproc.LINE_AA, false); //29n } //29n else { Imgproc.putText(rgbaMat, "Tap to select the tracking region in the gaze location.", new Point(5, rgbaMat.rows() - 10), Core.FONT_HERSHEY_SIMPLEX, 0.8, new Scalar(255, 255, 255, 255), 2, Imgproc.LINE_AA, false); } //bool updated = trackers.update(rgbaMat, objects); //Debug.Log("updated " + updated); //if (!updated && objects.rows() > 0) //{ // OnResetTrackerButtonClick(); //} if (storedTouchPoint != null) { Imgproc.putText(rgbaMat, "Tap again to select a new tracking region.", new Point(5, rgbaMat.rows() - 10), Core.FONT_HERSHEY_SIMPLEX, 0.8, new Scalar(255, 255, 255, 255), 2, Imgproc.LINE_AA, false); //if (fpsMonitor != null) //{ // fpsMonitor.consoleText = "Touch the screen to select a new tracking region."; //} } else { //if (fpsMonitor != null) //{ // fpsMonitor.consoleText = "Please select the end point of the new tracking region."; //} } Utils.fastMatToTexture2D(rgbaMat, texture); } if (webCamTextureToMatHelper.IsPlaying()) { Matrix4x4 cameraToWorldMatrix = webCamTextureToMatHelper.GetCameraToWorldMatrix(); Matrix4x4 worldToCameraMatrix = cameraToWorldMatrix.inverse; quad_renderer.sharedMaterial.SetMatrix("_WorldToCameraMatrix", worldToCameraMatrix); // Position the canvas object slightly in front // of the real world web camera. Vector3 position = cameraToWorldMatrix.GetColumn(3) - cameraToWorldMatrix.GetColumn(2); position *= 1.2f; // Rotate the canvas object so that it faces the user. Quaternion rotation = Quaternion.LookRotation(-cameraToWorldMatrix.GetColumn(2), cameraToWorldMatrix.GetColumn(1)); gameObject.transform.position = position; gameObject.transform.rotation = rotation; } }
// Update is called once per frame, in webcam void Update() { if (webCamTextureToMatHelper.IsPlaying() && webCamTextureToMatHelper.DidUpdateThisFrame()) { //19n //Vector3 posWorld = GazeManager.Instance.HitPosition; //textMesh.text = posWorld + ""; //Vector2 posWorld2Screen = WorldCoordsToScreen(webCamTextureToMatHelper.GetCameraToWorldMatrix(), webCamTextureToMatHelper.GetProjectionMatrix(), posWorld /*not used because of gaze*/); //textScreenGazePos.text = posWorld2Screen + ""; Mat rgbaMat = webCamTextureToMatHelper.GetMat(); Imgproc.cvtColor(rgbaMat, grayMat, Imgproc.COLOR_RGBA2GRAY); //textMesh.text = selectedPointList.Count+""; //Here starts the OpenCV script Vector2 posWorld2Screen = WorldCoordsToScreen(webCamTextureToMatHelper.GetCameraToWorldMatrix(), webCamTextureToMatHelper.GetProjectionMatrix(), sphere.transform.position);//19N storedTouchPoint = new Point(posWorld2Screen.x, posWorld2Screen.y); textMesh.text = storedTouchPoint.x + ";" + storedTouchPoint.y; //pintemelo en la screen /* * if (selectedPointList.Count < 2) * { * foreach (var point in selectedPointList) * { * Imgproc.circle(rgbaMat, point, 6, new Scalar(0, 0, 255), 2); * } * }*/ Utils.fastMatToTexture2D(rgbaMat, texture); } if (webCamTextureToMatHelper.IsPlaying()) { Matrix4x4 cameraToWorldMatrix = webCamTextureToMatHelper.GetCameraToWorldMatrix(); Matrix4x4 worldToCameraMatrix = cameraToWorldMatrix.inverse; quad_renderer.sharedMaterial.SetMatrix("_WorldToCameraMatrix", worldToCameraMatrix); // Position the canvas object slightly in front // of the real world web camera. Vector3 position = cameraToWorldMatrix.GetColumn(3) - cameraToWorldMatrix.GetColumn(2); position *= 1.2f; // Rotate the canvas object so that it faces the user. Quaternion rotation = Quaternion.LookRotation(-cameraToWorldMatrix.GetColumn(2), cameraToWorldMatrix.GetColumn(1)); gameObject.transform.position = position; gameObject.transform.rotation = rotation; } }