void Update() { Texture2D imgTexture = Resources.Load("vcl_kd_WITH_LOADING_CONTROL") as Texture2D; Mat imgMat = new Mat(imgTexture.height, imgTexture.width, CvType.CV_8UC3); Utils.texture2DToMat(imgTexture, imgMat); Mat grayMat = new Mat(imgTexture.height, imgTexture.width, CvType.CV_8UC3); Imgproc.cvtColor(imgMat, grayMat, Imgproc.COLOR_RGB2GRAY); //grayscale Mat gray2Mat = new Mat(imgTexture.height, imgTexture.width, CvType.CV_8UC3); Core.bitwise_not(grayMat, gray2Mat); Mat gray3Mat = new Mat(imgTexture.height, imgTexture.width, CvType.CV_8UC3); Imgproc.threshold(gray2Mat, gray3Mat, thresh, maxval, Imgproc.THRESH_BINARY_INV | Imgproc.THRESH_OTSU); //Imgproc.threshold (gray2Mat, gray2Mat, thresh, maxval, Imgproc.THRESH_BINARY); Core.bitwise_not(gray3Mat, gray3Mat); Mat procMat = new Mat(imgTexture.height, imgTexture.width, CvType.CV_8UC3); Core.bitwise_and(gray2Mat, gray3Mat, procMat); //multiply the mask of foreground by the original image to obtain 0 background and original values in foreground Mat labels = new Mat(imgTexture.height, imgTexture.width, CvType.CV_8UC3); Mat stats = new Mat(imgTexture.height, imgTexture.width, CvType.CV_8UC3); Mat centroids = new Mat(imgTexture.height, imgTexture.width, CvType.CV_8UC3); int total = Imgproc.connectedComponentsWithStats(gray3Mat, labels, stats, centroids); for (int i = 1; i < total; ++i) { int xx = (int)centroids.get(i, 0) [0]; int yy = (int)centroids.get(i, 1) [0]; Imgproc.circle(procMat, new Point(xx, yy), 3, new Scalar(255, 255, 0), -1); int x = (int)stats.get(i, Imgproc.CC_STAT_LEFT) [0]; int y = (int)stats.get(i, Imgproc.CC_STAT_TOP) [0]; int height = (int)stats.get(i, Imgproc.CC_STAT_HEIGHT) [0]; int width = (int)stats.get(i, Imgproc.CC_STAT_WIDTH) [0]; int area = (int)stats.get(i, Imgproc.CC_STAT_AREA) [0]; if (area > 30) { OpenCVForUnity.Rect rect = new OpenCVForUnity.Rect(x, y, width, height); Imgproc.rectangle(procMat, rect.tl(), rect.br(), new Scalar(255, 255, 0), 2); Mat blotMat = imgMat.submat(rect); Scalar meanVa = Core.mean(blotMat); int meanV = (int)meanVa.val [0]; double blotV = 0; Imgproc.putText(procMat, "I" + i + "/" + (area * meanV), new Point(x, y - 50), Core.FONT_HERSHEY_PLAIN, 1, new Scalar(255, 122, 0), 2); } } Mat hierarchy = new Mat(imgTexture.height, imgTexture.width, CvType.CV_8UC3); List <MatOfPoint> blotContours = new List <MatOfPoint>(); Imgproc.findContours(gray3Mat, blotContours, hierarchy, Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE); Texture2D texture = new Texture2D(imgMat.cols(), imgMat.rows(), TextureFormat.RGBA32, false); Utils.matToTexture2D(procMat, texture); GetComponent <RawImage> ().texture = (Texture2D)texture; }
// Use this for initialization void Start() { Texture2D imgTexture = Resources.Load("matchshapes") as Texture2D; Mat srcMat = new Mat(imgTexture.height, imgTexture.width, CvType.CV_8UC1); Utils.texture2DToMat(imgTexture, srcMat); Debug.Log("srcMat.ToString() " + srcMat.ToString()); Mat dstMat = new Mat(srcMat.size(), CvType.CV_8UC3); //labeling Mat labels = new Mat(); Mat stats = new Mat(); Mat centroids = new Mat(); int total = Imgproc.connectedComponentsWithStats(srcMat, labels, stats, centroids); Debug.Log("labels.ToString() " + labels.ToString()); Debug.Log("stats.ToString() " + stats.ToString()); Debug.Log("centroids.ToString() " + centroids.ToString()); Debug.Log("total " + total); // determine drawing color List <Scalar> colors = new List <Scalar> (total); colors.Add(new Scalar(0, 0, 0)); for (int i = 1; i < total; ++i) { colors.Add(new Scalar(Random.Range(0, 255), Random.Range(0, 255), Random.Range(0, 255))); } // draw labels for (int i = 0; i < dstMat.rows(); ++i) { for (int j = 0; j < dstMat.cols(); ++j) { Scalar color = colors [(int)labels.get(i, j) [0]]; dstMat.put(i, j, color.val [0], color.val [1], color.val [2]); } } // draw rectangle for (int i = 1; i < total; ++i) { int x = (int)stats.get(i, Imgproc.CC_STAT_LEFT) [0]; int y = (int)stats.get(i, Imgproc.CC_STAT_TOP) [0]; int height = (int)stats.get(i, Imgproc.CC_STAT_HEIGHT) [0]; int width = (int)stats.get(i, Imgproc.CC_STAT_WIDTH) [0]; OpenCVForUnity.Rect rect = new OpenCVForUnity.Rect(x, y, width, height); Imgproc.rectangle(dstMat, rect.tl(), rect.br(), new Scalar(0, 255, 0), 2); } // draw centroids for (int i = 1; i < total; ++i) { int x = (int)centroids.get(i, 0) [0]; int y = (int)centroids.get(i, 1) [0]; Imgproc.circle(dstMat, new Point(x, y), 3, new Scalar(255, 0, 0), -1); } // draw index of label for (int i = 1; i < total; ++i) { int x = (int)stats.get(i, Imgproc.CC_STAT_LEFT) [0]; int y = (int)stats.get(i, Imgproc.CC_STAT_TOP) [0]; Imgproc.putText(dstMat, "" + i, new Point(x + 5, y + 15), Core.FONT_HERSHEY_COMPLEX, 0.5, new Scalar(255, 255, 0), 2); } Texture2D texture = new Texture2D(dstMat.cols(), dstMat.rows(), TextureFormat.RGBA32, false); Utils.matToTexture2D(dstMat, texture); gameObject.GetComponent <Renderer> ().material.mainTexture = texture; }