//Remove Background IEnumerator BackgroundSubtraction() { Imgproc.cvtColor(openCVCreateMat.rgbaMat, openCVCreateMat.rgbMat, Imgproc.COLOR_RGBA2RGB); backgroundSubstractorMOG2.apply(openCVCreateMat.rgbMat, openCVCreateMat.fgmaskMat); Core.bitwise_not(openCVCreateMat.fgmaskMat, openCVCreateMat.fgmaskMat); openCVCreateMat.rgbaMat.setTo(new Scalar(0, 0, 0, 0), openCVCreateMat.fgmaskMat); if (blurImage == true) { //Gaussian filter of the MOG2 images Imgproc.GaussianBlur(openCVCreateMat.rgbaMat, openCVCreateMat.rgbaMat, kernelSize, sigmaX, sigmaY); //Gauss filter } yield return(null); }
// Update is called once per frame void Update() { if (webCamTextureToMatHelper.IsPlaying() && webCamTextureToMatHelper.DidUpdateThisFrame()) { Mat rgbaMat = webCamTextureToMatHelper.GetMat(); Imgproc.cvtColor(rgbaMat, rgbMat, Imgproc.COLOR_RGBA2RGB); backgroundSubstractorMOG2.apply(rgbMat, fgmaskMat); Core.bitwise_not(fgmaskMat, fgmaskMat); rgbaMat.setTo(new Scalar(0, 0, 0, 0), fgmaskMat); Utils.matToTexture2D(rgbaMat, texture, webCamTextureToMatHelper.GetBufferColors()); } }
/** Background subtraction and check if mouse selected a target */ private OpenCVForUnity.Rect BgSub() { Mat fgmaskMat = new Mat(); roiRect = null; OpenCVForUnity.Rect output; //Background Subtraction backgroundSubstractorMOG2.apply(frame, fgmaskMat); //Closure *it is done to remove noise and close gaps that bgsub may leave where we don't want to Mat structuringElement = Imgproc.getStructuringElement(Imgproc.MORPH_ELLIPSE, new Size(closingSize, closingSize)); Imgproc.dilate(fgmaskMat, fgmaskMat, structuringElement); Imgproc.erode(fgmaskMat, fgmaskMat, structuringElement); //Make mask binary Mat maskBinary = new Mat(); Imgproc.threshold(fgmaskMat, maskBinary, 123, 255, Imgproc.THRESH_BINARY); //Get Contours List <MatOfPoint> contours = new List <MatOfPoint>(); OpenCVForUnity.Mat hierarchy = new OpenCVForUnity.Mat(); Imgproc.findContours(maskBinary, contours, hierarchy, Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_NONE); foreach (MatOfPoint contour in contours) { output = Imgproc.boundingRect(new MatOfPoint(contour.toArray())); Imgproc.rectangle(frame, output.tl(), output.br(), new Scalar(255, 0, 0), 2); rectanglesToPrint.Add(new ColoredRect(output, Color.white)); UnityEngine.Rect check_pos = CVtoUnityRect(output); if (Input.GetMouseButton(0) && check_pos.Contains(new Vector2(Input.mousePosition.x, Screen.height - Input.mousePosition.y))) { Debug.Log("Selected a target box"); Debug.Log(output); return(output); } } return(null); }
// /*void a() * { * UnityEngine.Rect screenRect = new UnityEngine.Rect(d, d, Screen.width - 2 * d, Screen.height - 2 * d); * * if (Input.GetMouseButtonDown(0) && (Input.mousePosition.x >= d) && (Input.mousePosition.y >= d) && (Input.mousePosition.x <= Screen.width - d) && (Input.mousePosition.y <= Screen.height - d)) * { * Debug.Log("mouse"); * Ray mouseRay = Camera.main.ScreenPointToRay(Input.mousePosition); * if (screenRect.Contains(Input.mousePosition)) giu = new Point(Input.mousePosition.x, cam.Camera.pixelRect.height - Input.mousePosition.y); * if (screenRect.Contains(Input.mousePosition)) giu2 = new Point(Input.mousePosition.x, Input.mousePosition.y); * isSelecting = true; * mousePosition1 = Input.mousePosition; * } * if (Input.GetMouseButtonUp(0) && (Input.mousePosition.x >= d) && (Input.mousePosition.y >= d) && (Input.mousePosition.x <= Screen.width - d) && (Input.mousePosition.y <= Screen.height - d)) * { * Ray mouseRay = Camera.main.ScreenPointToRay(Input.mousePosition); * if (screenRect.Contains(Input.mousePosition)) su = new Point(Input.mousePosition.x, cam.Camera.pixelRect.height - Input.mousePosition.y); * if (screenRect.Contains(Input.mousePosition)) su2 = new Point(Input.mousePosition.x, Input.mousePosition.y); * isSelecting = false; * * Debug.Log("dimensioni rettangolo: " + giu + ", " + su); * * if (su == giu) * { * ss = 0; * roiRect = BgSub(); * CamShiftOn = false; * shouldStartCamShift = true; * } * else * { * OpenCVForUnity.Rect rettangolo = Imgproc.boundingRect(new MatOfPoint(giu, su)); * roiRect = rettangolo; * skipFrame = BackFrame; //skip the part of backgroungMOG2 and BgSub() * ss += ss; * } * * } * }*/ // Update is called once per frame void Update() { //if (Input.GetMouseButton(0)) // Debug.Log(Input.mousePosition); dest = new OpenCVForUnity.Mat(); output_ar = new List <OpenCVForUnity.Rect>(); CurrentFrame(); if (shouldStartCamShift) { shouldStartCamShift = false; CamShiftOn = true; Debug.Log("camshift = on"); } bool backToStart = GetComponent <CameraControllerPTZ>().backToStart; Debug.Log("camera_in_position" + backToStart); if (!CamShiftOn && backToStart) { if (skipFrame < BackFrame) { if (skipFrame == 0) { backgroundSubstractorMOG2 = Video.createBackgroundSubtractorMOG2(); } backgroundSubstractorMOG2.apply(rgbMat, fgmaskMat); //roiRect = BgSub(); skipFrame = skipFrame + 1; } else { if (ss == 0) { roiRect = BgSub(); } //Debug.Log(roiRect); if (roiRect != null) { shouldStartCamShift = true; Debug.Log("true"); } } SaveMatToFile("rgbMat." + ss, rgbMat); Debug.Log("backsubtract"); } if (CamShiftOn) { //a(); // permette selezione del target con il mouse cliccando e trascinando per creare un rettangolo hsvMat = new Mat(frame.rows(), frame.cols(), CvType.CV_8UC3); Imgproc.cvtColor(rgbMat, hsvMat, Imgproc.COLOR_RGBA2RGB); Imgproc.cvtColor(hsvMat, hsvMat, Imgproc.COLOR_RGB2HSV); //SaveMatToFile3("hsvMat", hsvMat); RunCamShift(); iter++; if (iter > Numiter || roiRect.height > 2 * (Screen.height / 3)) { CamShiftOn = false; skipFrame = 0; } output_ar.Add(roiRect); color1 = new Color(0.1f, 0.9f, 0.1f, 0.1f); color2 = new Color(0.1f, 0.9f, 0.1f); } if (shouldStartCamShift) { Debug.Log("Starting camshift"); hsvMat = new Mat(frame.rows(), frame.cols(), CvType.CV_8UC3); /*roiHSVMat = new Mat (); * SaveMatToFile ("roihsvmatpreRGB"+ss, dest); * Imgproc.cvtColor(roiHSVMat, roiHSVMat, Imgproc.COLOR_RGBA2RGB); * Imgproc.cvtColor(roiHSVMat, roiHSVMat, Imgproc.COLOR_RGB2HSV); //COLOR_RGB2HSV , COLOR_RGB2YCrCb*/ Imgproc.cvtColor(dest, hsvMat, Imgproc.COLOR_RGBA2RGB); Imgproc.cvtColor(hsvMat, hsvMat, Imgproc.COLOR_RGB2HSV); //COLOR_RGB2HSV , COLOR_RGB2YCrCb iter = 0; //shouldStartCamShift = false; InitCamShift(); //CamShiftOn = true; //Debug.Break(); } if (!CamShiftOn) { x_coordinate = 0; y_coordinate = 0; height = 0; width = 0; } else { Debug.Log("passing coordinates to camera"); height = (float)(roiRect.height); width = (float)(roiRect.width); x_coordinate_for_camera = (float)(roiRect.x + (roiRect.width / 2)); y_coordinate_for_camera = (float)(roiRect.y + (roiRect.height / 2)); //Debug.Log("rgbMat.size()" + rgbMat.size()); //Debug.Log ("x_coordinate_for_camera" + x_coordinate_for_camera); //Debug.Log ("y_coordinate_for_camera" + y_coordinate_for_camera); //Debug.Log("height" + height); SaveMatToFile("rgbMat" + ss, rgbMat); //Debug.Log("width" + width + " , screen" + Screen.width); } //skipFrame = skipFrame + 1; }