// Use this for initialization void Start() { Texture2D imgTexture = Resources.Load("detect_blob") as Texture2D; Mat imgMat = new Mat(imgTexture.height, imgTexture.width, CvType.CV_8UC1); Utils.texture2DToMat(imgTexture, imgMat); Debug.Log("imgMat dst ToString " + imgMat.ToString()); Mat outImgMat = new Mat(); FeatureDetector blobDetector = FeatureDetector.create(FeatureDetector.SIMPLEBLOB); blobDetector.read(Utils.getFilePath("blobparams.yml")); MatOfKeyPoint keypoints = new MatOfKeyPoint(); blobDetector.detect(imgMat, keypoints); Features2d.drawKeypoints(imgMat, keypoints, outImgMat); Texture2D texture = new Texture2D(outImgMat.cols(), outImgMat.rows(), TextureFormat.RGBA32, false); Utils.matToTexture2D(outImgMat, texture); gameObject.GetComponent <Renderer> ().material.mainTexture = texture; }
void InitBlobDetector() { // Try to create the blob detector. blobDetector = FeatureDetector.create( FeatureDetector.SIMPLEBLOB); if (blobDetector == null) { Debug.LogError( "Unable to create blob detector"); Destroy(this); return; } // The blob detector parameters must be put inta a yaml file for unity string blobDetectorParams = @"%YAML:1.0 thresholdStep: 10.0 minThreshold: 50.0 maxThreshold: 220.0 minRepeatability: 2 minDistBetweenBlobs: 10.0 filterByColor: False blobColor: 0 filterByArea: True minArea: 50.0 maxArea: 5000.0 filterByCircularity: True minCircularity: 0.8 maxCircularity: 3.4028234663852886e+38 filterByInertia: False minInertiaRatio: 0.1 maxInertiaRatio: 3.4028234663852886e+38 filterByConvexity: False minConvexity: 0.95 maxConvexity: 3.4028234663852886e+38 "; // Try to write the blob detector's parameters // to a temporary file. string path = Application.persistentDataPath + "/blobDetectorParams.yaml"; File.WriteAllText(path, blobDetectorParams); if (!File.Exists(path)) { Debug.LogError( "Unable to write blob " + "detector's parameters to " + path); Destroy(this); return; } // Read the blob detector's parameters from the // temporary file. blobDetector.read(path); // Delete the temporary file. File.Delete(path); }
private void TryProcessImage(int index) { if (UseWebCam == false) { CurrentTexture = Sources[index]; } else { CurrentTexture = webCamTexture; } using (Mat imgMat = new Mat(CurrentTexture.height, CurrentTexture.width, CvType.CV_8UC1)) using (FeatureDetector blobDetector = FeatureDetector.create(FeatureDetector.SIMPLEBLOB)) using (Mat outImgMat = new Mat()) using (MatOfKeyPoint keypoints = new MatOfKeyPoint()) { if (CurrentTexture is Texture2D) { Utils.texture2DToMat(CurrentTexture as Texture2D, imgMat); } else if (CurrentTexture is WebCamTexture) { Utils.webCamTextureToMat(CurrentTexture as WebCamTexture, imgMat); } else { Utils.textureToMat(CurrentTexture, imgMat); } Debug.Log("imgMat dst ToString " + imgMat.ToString()); Imgproc.threshold(imgMat, imgMat, 0, 255, Imgproc.THRESH_BINARY | Imgproc.THRESH_OTSU); Imgproc.erode(imgMat, imgMat, erodeMat, new Point(1, 1), 5); blobDetector.read(Utils.getFilePath("blobparams.yml")); blobDetector.detect(imgMat, keypoints); Features2d.drawKeypoints(imgMat, keypoints, outImgMat); KeyPoint[] points = keypoints.toArray(); ProcessKeyPoints(points, outImgMat); Mat finalMat = outImgMat; if (texture != null && (texture.width != finalMat.cols() || texture.height != finalMat.rows())) { DestroyImmediate(texture); texture = null; } if (texture == null) { texture = new Texture2D(finalMat.cols(), finalMat.rows(), TextureFormat.RGBA32, false); } Utils.matToTexture2D(finalMat, texture); gameObject.GetComponent <Renderer>().material.mainTexture = texture; } }