private void DetectInRegion(Mat img, Rect region, List <Rect> detectedObjectsInRegions, CascadeClassifier cascade) { Rect r0 = new Rect(new Point(), img.size()); Rect r1 = new Rect(region.x, region.y, region.width, region.height); Rect.inflate(r1, (int)((r1.width * coeffTrackingWindowSize) - r1.width) / 2, (int)((r1.height * coeffTrackingWindowSize) - r1.height) / 2); r1 = Rect.intersect(r0, r1); if ((r1.width <= 0) || (r1.height <= 0)) { Debug.Log("detectInRegion: Empty intersection"); return; } int d = Math.Min(region.width, region.height); d = (int)Math.Round(d * coeffObjectSizeToTrack); using (MatOfRect tmpobjects = new MatOfRect()) using (Mat img1 = new Mat(img, r1)) //subimage for rectangle -- without data copying { cascade.detectMultiScale(img1, tmpobjects, 1.1, 2, 0 | Objdetect.CASCADE_DO_CANNY_PRUNING | Objdetect.CASCADE_SCALE_IMAGE | Objdetect.CASCADE_FIND_BIGGEST_OBJECT, new Size(d, d), new Size()); Rect[] tmpobjectsArray = tmpobjects.toArray(); int len = tmpobjectsArray.Length; for (int i = 0; i < len; i++) { Rect tmp = tmpobjectsArray[i]; Rect r = new Rect(new Point(tmp.x + r1.x, tmp.y + r1.y), tmp.size()); detectedObjectsInRegions.Add(r); } } }
private void DetectInRegion(Mat img, Rect region, List <Rect> detectedObjectsInRegions, FaceLandmarkDetector landmarkDetector) { Rect r0 = new Rect(new Point(), img.size()); Rect r1 = new Rect(region.x, region.y, region.width, region.height); Rect.inflate(r1, (int)((r1.width * coeffTrackingWindowSize) - r1.width) / 2, (int)((r1.height * coeffTrackingWindowSize) - r1.height) / 2); r1 = Rect.intersect(r0, r1); if ((r1.width <= 0) || (r1.height <= 0)) { Debug.Log("detectInRegion: Empty intersection"); return; } using (Mat img1_roi = new Mat(img, r1)) using (Mat img1 = new Mat(r1.size(), img.type())) { img1_roi.copyTo(img1); OpenCVForUnityUtils.SetImage(landmarkDetector, img1); List <UnityEngine.Rect> detectResult = landmarkDetector.Detect(); int len = detectResult.Count; for (int i = 0; i < len; i++) { UnityEngine.Rect tmp = detectResult[i]; Rect r = new Rect((int)(tmp.x + r1.x), (int)(tmp.y + r1.y), (int)tmp.width, (int)tmp.height); detectedObjectsInRegions.Add(r); } } }
void DoProcess() { if (!(owner.Value is OpenCVForUnityPlayMakerActions.Rect)) { LogError("owner is not initialized. Add Action \"newRect\"."); return; } OpenCVForUnity.CoreModule.Rect wrapped_owner = OpenCVForUnityPlayMakerActionsUtils.GetWrappedObject <OpenCVForUnityPlayMakerActions.Rect, OpenCVForUnity.CoreModule.Rect>(owner); if (!(storeResult.Value is OpenCVForUnityPlayMakerActions.Size)) { storeResult.Value = new OpenCVForUnityPlayMakerActions.Size(); } ((OpenCVForUnityPlayMakerActions.Size)storeResult.Value).wrappedObject = wrapped_owner.size(); }
/// <summary> /// Raises the save button click event. /// </summary> public void OnSaveButtonClick() { if (patternRawImage.texture != null) { Texture2D patternTexture = (Texture2D)patternRawImage.texture; Mat patternMat = new Mat(patternRect.size(), CvType.CV_8UC3); Utils.texture2DToMat(patternTexture, patternMat); Imgproc.cvtColor(patternMat, patternMat, Imgproc.COLOR_RGB2BGR); string savePath = Application.persistentDataPath; Debug.Log("savePath " + savePath); Imgcodecs.imwrite(savePath + "/patternImg.jpg", patternMat); SceneManager.LoadScene("WebCamTextureMarkerLessARExample"); } }