/// <summary> /// Detects and outlines faces in the WebcamTexture and stored as a Mat. /// Optionally equalizes and denoises WebcamTexture. /// </summary> public void UpdateFaceTexture() { if (webcamController.DidUpdateThisFrame()) { Mat rgbaMat = webcamController.WebcamMat; Imgproc.cvtColor(rgbaMat, grayscale, Imgproc.COLOR_RGB2GRAY); Imgproc.equalizeHist(grayscale, grayscale); if (cascade != null) { cascade.detectMultiScale(grayscale, faces, 1.1, 2, 2, new Size(grayscale.cols() * 0.2, grayscale.rows() * 0.2), new Size()); } faceLocations = faces.toArray(); if (DenoiseTexture) { Photo.fastNlMeansDenoising(grayscale, grayscale); } for (int i = 0; i < faceLocations.Length; i++) { if (EqualizeTexture) { Imgproc.rectangle(grayscale, new Point(faceLocations[i].x, faceLocations[i].y), new Point(faceLocations[i].x + faceLocations[i].width, faceLocations[i].y + faceLocations[i].height), new Scalar(255, 255, 255, 255), 5); } else { Imgproc.rectangle(rgbaMat, new Point(faceLocations[i].x, faceLocations[i].y), new Point(faceLocations[i].x + faceLocations[i].width, faceLocations[i].y + faceLocations[i].height), new Scalar(255, 255, 255, 255), 5); } } if (EqualizeTexture) { Utils.matToTexture2D(grayscale, faceTexture, webcamController.Colors); } else { Utils.matToTexture2D(rgbaMat, faceTexture, webcamController.Colors); } } }