void ComputerVisionAlgo(IntPtr greyscale) { Utils.copyToMat(greyscale, imageMat); // Inverting Image pixel values inMat = (Mat.ones(imageMat.rows(), imageMat.cols(), CvType.CV_8UC1) * 255) - imageMat; // Creating Detector (Yellow Circle) // MatOfKeyPoint keyMat = new MatOfKeyPoint(); // SimpleBlobDetector detector = SimpleBlobDetector.create(); // Creating Detector (Red Circle) MatOfKeyPoint keyMat = new MatOfKeyPoint(); SimpleBlobDetector detector = SimpleBlobDetector.create(); inMat = imageMat; detector.read(circparam_path); // Finding circles detector.detect(inMat, keyMat); if (keyMat.size().height > 0) { blob_x = keyMat.get(0, 0)[0]; blob_y = keyMat.get(0, 0)[1]; blob_r = keyMat.get(0, 0)[2]; } // Visualizing detected circles m_ImageInfo.text = string.Format("Circle Count: {0}\n Circle[0]: {1} x {2} -- {3}", keyMat.size().height, blob_x, blob_y, blob_r); Features2d.drawKeypoints(imageMat, keyMat, outMat); }
public Magic(FrameDescription infraredFrameDescription) { mog = BackgroundSubtractorMOG.Create(); blobby = SimpleBlobDetector.Create(new SimpleBlobDetector.Params() { MinThreshold = 150, MaxThreshold = 255, FilterByColor = true, BlobColor = 255, FilterByArea = true, MinArea = 0.05f, MaxArea = 20, FilterByCircularity = true, MinCircularity = 0.5f, FilterByConvexity = true, MinConvexity = 0.5f, FilterByInertia = false //FilterByCircularity = true, //MinCircularity = 0.4f, //FilterByArea = true, //MaxArea = 10000 }); spellAI = new SpellAI(); tracePoints = new List <Point>(); traceCanvas = new Mat(new Size(infraredFrameDescription.Width, infraredFrameDescription.Height), MatType.CV_32F); gameController = new GameController(); }
private Mat find_ball() { MCvScalar orangeMin = new MCvScalar(0, 0, 212); //10 120 100 MCvScalar orangeMax = new MCvScalar(131, 255, 255); //70 255 255 Mat arr = new Mat(); Mat img = _frame; Mat hsvImg = new Mat(); CvInvoke.CvtColor(img, hsvImg, ColorConversion.Bgr2Hsv); CvInvoke.InRange(hsvImg, new ScalarArray(orangeMin), new ScalarArray(orangeMax), hsvImg); //CvInvoke.MorphologyEx(hsvImg, hsvImg, MorphOp.Close, new Mat(), new System.Drawing.Point(-1, -1), 5, BorderType.Default, new MCvScalar()); SimpleBlobDetectorParams param = new SimpleBlobDetectorParams(); param.FilterByCircularity = false; param.FilterByConvexity = false; param.FilterByInertia = false; param.FilterByColor = false; param.MinArea = 800; param.MaxArea = 5000; SimpleBlobDetector detector = new SimpleBlobDetector(param); MKeyPoint[] keypoints = detector.Detect(hsvImg); Features2DToolbox.DrawKeypoints(img, new VectorOfKeyPoint(keypoints), img, new Bgr(255, 0, 0), Features2DToolbox.KeypointDrawType.DrawRichKeypoints); foreach (var item in keypoints) { if ((int)item.Point.X > x_min && (int)item.Point.X < x_max && (int)item.Point.Y > y_min && (int)item.Point.Y < y_max) { centerX = (int)item.Point.X; centerY = (int)item.Point.Y; } else { centerX = dX; centerY = dY; total_error_x = 0; total_error_y = 0; } } if (keypoints.Length == 0) { centerX = dX; centerY = dY; total_error_x = 0; total_error_y = 0; } lbl_x.Content = "Center X: " + centerX; lbl_y.Content = "Center Y: " + centerY; return(img); }
private MKeyPoint[] detector(Image <Gray, Double> src) { Image <Gray, byte> _image = new Image <Gray, byte>(src.Bitmap); SimpleBlobDetectorParams simpleBlobDetectorParams = new SimpleBlobDetectorParams(); simpleBlobDetectorParams.MinArea = _imageTemple.Width * _imageTemple.Height / 2; //simpleBlobDetectorParams.MaxArea = 100000000; simpleBlobDetectorParams.ThresholdStep = 10; simpleBlobDetectorParams.MinThreshold = 50; simpleBlobDetectorParams.FilterByArea = true; simpleBlobDetectorParams.FilterByCircularity = false; simpleBlobDetectorParams.FilterByColor = false; simpleBlobDetectorParams.FilterByConvexity = false; simpleBlobDetectorParams.FilterByInertia = false; simpleBlobDetectorParams.MaxThreshold = 255; simpleBlobDetectorParams.MinThreshold = 50; SimpleBlobDetector simpleBlobDetector = new SimpleBlobDetector(simpleBlobDetectorParams); MKeyPoint[] keyPoint = simpleBlobDetector.Detect(_image); return(keyPoint); }
public static void Run() { string imagePath = "./images/geo.jpg"; Mat img = Cv2.ImRead(imagePath); SimpleBlobDetector.Params blobDetectorParams = new SimpleBlobDetector.Params(); blobDetectorParams.MinThreshold = 10; blobDetectorParams.MaxThreshold = 100; FlannBasedMatcher matcher = new FlannBasedMatcher(null, null); blobDetectorParams.FilterByArea = true; blobDetectorParams.MinArea = 150; blobDetectorParams.FilterByCircularity = true; blobDetectorParams.MinCircularity = 0.3f; blobDetectorParams.FilterByConvexity = true; blobDetectorParams.MinConvexity = 0.2f; blobDetectorParams.FilterByInertia = true; blobDetectorParams.MinInertiaRatio = 0.1f; var detector = SimpleBlobDetector.Create(blobDetectorParams); var keypoints = detector.Detect(img); Mat output = new Mat(); Cv2.DrawKeypoints(img, keypoints, output, new Scalar(0, 0, 255), DrawMatchesFlags.DrawRichKeypoints); Cv2.ImShow("Output", output); Cv2.WaitKey(0); }
/// <summary> /// Finds the corners/circles (depending on selected target type). /// </summary> /// <param name="image">The image to search.</param> /// <param name="annotatedImage">The image with the corners/circles annotated.</param> /// <param name="corners">Vector of the corners/circles.</param> /// <returns>True if corers/circles found, false otherwise.</returns> private bool FindCorners(Image <Bgr, byte> image, ref Image <Bgr, byte> annotatedImage, VectorOfPointF corners) { var found = false; // use simple blob detector if finding circle grid using (var det = new SimpleBlobDetector()) { // set the size of the pattern var size = new Size(_cornersPerRow, _cornersPerCol); // look for chessboard or circle grid switch (_targetType) { case CalibTargetType.ChessBoard: found = CvInvoke.FindChessboardCorners(image, size, corners); // if corners found, get sub-pixel accuracy if (found) { CvInvoke.CornerSubPix(image.Convert <Gray, byte>(), corners, new Size(11, 11), new Size(-1, -1), new MCvTermCriteria(30, 0.1)); } break; case CalibTargetType.CirclesGrid: found = CvInvoke.FindCirclesGrid(image, size, corners, _circlesGridType, det); break; } // draw the results CvInvoke.DrawChessboardCorners(annotatedImage, size, corners, found); return(found); } }
public void TestSimpleBlobDetector() { Mat box = EmguAssert.LoadMat("box.png"); SimpleBlobDetectorParams p = new SimpleBlobDetectorParams(); SimpleBlobDetector detector = new SimpleBlobDetector(p); MKeyPoint[] keypoints = detector.Detect(box); }
protected override void SandwormSolveInstance(IGH_DataAccess DA) { SetupLogging(); markerColors = new List <Color>(); DA.GetDataList(0, markerColors); DA.GetData(1, ref colorFuzz); GetSandwormOptions(DA, 2, 0, 0); SetupKinect(); Core.LogTiming(ref output, timer, "Initial setup"); // Debug Info var binaryImage = GenerateColorImage(); Core.LogTiming(ref output, timer, "Image generation"); // Debug Info if (binaryImage != null) { // Search image for the color and identify/classify var keyPoints = new List <KeyPoint>(); var detectorParameters = new SimpleBlobDetector.Params { FilterByArea = true, FilterByColor = true, // If it doesn't work; pre-filter the image MinDistBetweenBlobs = 1, MinArea = 10, MaxArea = 20 }; Core.LogTiming(ref output, timer, "Detector setup"); // Debug Info foreach (Color markerColor in markerColors) { var blobDetector = SimpleBlobDetector.Create(detectorParameters); keyPoints.AddRange(blobDetector.Detect(binaryImage)); blobDetector.Dispose(); } Core.LogTiming(ref output, timer, "Image blob detection"); // Debug Info // Translate identified points back into Grasshopper geometry markerPoints = new List <Point3d>(); foreach (KeyPoint keyPoint in keyPoints) { var x = keyPoint.Pt.X; var y = keyPoint.Pt.Y; markerPoints.Add(new Point3d(x, y, 0)); } DA.SetDataList(0, markerPoints); Core.LogTiming(ref output, timer, "Blob output"); // Debug Info } else { // TODO: add warning? } binaryImage.Dispose(); DA.SetDataList(1, output); // For logging/debugging ScheduleSolve(); }
public void TestCirclesGrid() { Size patternSize = new Size(4, 3); Image <Gray, Byte> circlesGridImage = EmguAssert.LoadImage <Gray, byte>("circlesGrid.bmp"); using (SimpleBlobDetector detector = new SimpleBlobDetector()) using (Util.VectorOfPointF centers = new Util.VectorOfPointF()) { bool found = CvInvoke.FindCirclesGrid(circlesGridImage, patternSize, centers, CvEnum.CalibCgType.SymmetricGrid | CvEnum.CalibCgType.Clustering, detector); CvInvoke.DrawChessboardCorners(circlesGridImage, patternSize, centers, found); //UI.ImageViewer.Show(circlesGridImage); } }
void CamUpdate() { CvUtil.GetWebCamMat(webCamTexture, ref mat); Cv2.CvtColor(mat, greyMat, ColorConversionCodes.RGBA2GRAY); Cv2.Threshold(greyMat, greyMat, 100, 255, ThresholdTypes.Binary); var detectorParams = new SimpleBlobDetector.Params { //MinDistBetweenBlobs = 10, // 10 pixels between blobs //MinRepeatability = 1, //MinThreshold = 100, //MaxThreshold = 255, //ThresholdStep = 5, FilterByArea = false, //FilterByArea = true, //MinArea = 0.001f, // 10 pixels squared //MaxArea = 500, FilterByCircularity = false, //FilterByCircularity = true, //MinCircularity = 0.001f, FilterByConvexity = false, //FilterByConvexity = true, //MinConvexity = 0.001f, //MaxConvexity = 10, FilterByInertia = false, //FilterByInertia = true, //MinInertiaRatio = 0.001f, FilterByColor = false //FilterByColor = true, //BlobColor = 255 // to extract light blobs }; var simpleBlobDetector = SimpleBlobDetector.Create(detectorParams); var keyPoints = simpleBlobDetector.Detect(greyMat); Cv2.DrawKeypoints( image: greyMat, keypoints: keyPoints, outImage: mat, color: Scalar.FromRgb(255, 0, 0), flags: DrawMatchesFlags.DrawRichKeypoints); CvConvert.MatToTexture2D(mat, ref tex); rawImage.texture = tex; }
/// <summary> /// UI Handler for updating the parameters during runtime /// </summary> /// <param name="minimumArea"></param> /// <param name="minimumThreshold"></param> public void UpdateBlobParameters(float minimumArea, float minimumThreshold) { blobParams = new SimpleBlobDetectorParams(); blobParams.FilterByArea = true; blobParams.FilterByColor = true; blobParams.FilterByCircularity = false; blobParams.FilterByConvexity = false; blobParams.FilterByInertia = false; blobParams.blobColor = (byte)255; blobParams.MinThreshold = minimumThreshold; blobParams.MinArea = minimumArea * minimumArea; blobDetector = new SimpleBlobDetector(blobParams); }
void Start() { m_RawImage.enabled = false; actionText = nextActionButton.GetComponentInChildren <Text>(); m_ImageInfo.gameObject.SetActive(false); enableKeypointDetection = false; enableGlobalCubePlacement = false; enableLineDrawing = false; enableColorSelection = false; nextActionButton.onClick.AddListener(onNextActionPress); Debug.Log("Pixel height is " + cam.scaledPixelHeight); Debug.Log("Pixel width is " + cam.scaledPixelWidth); rayCastManager = origin.GetComponent <ARRaycastManager>(); Debug.Log(rayCastManager); instantiatedVisualizer = Instantiate(visualizer, new Vector3(0, 0, 0), new Quaternion(0, 0, 0, 0)); lineRenderer = instantiatedVisualizer.GetComponent <LineRenderer>(); lineRenderer.positionCount = 0; // Application.targetFrameRate = 15; Params blobParams = new Params(); blobParams.set_minThreshold(0); blobParams.set_maxThreshold(255); blobParams.set_filterByArea(true); blobParams.set_minArea(25); blobParams.set_filterByCircularity(false); blobParams.set_minCircularity(.1f); blobParams.set_filterByConvexity(false); blobParams.set_minConvexity(.1f); blobParams.set_filterByInertia(true); blobParams.set_minInertiaRatio(.05f); blobber = SimpleBlobDetector.create(); //blobber = SimpleBlobDetector.create(blobParams); //blobber = SimpleBlobDetector.create(blobParams); Debug.Log((Screen.width / 2).ToString()); // string paramsPath = Utils.getFilePath("blobparams.yml"); //blobber.read(paramsPath); // downsize screen resolution Screen.SetResolution((int)Screen.width / 2, (int)Screen.height / 2, true, 30); Utils.setDebugMode(true); Application.targetFrameRate = 30; }
void Run() { try { _cameraCapture = new VideoCapture(); } catch (Exception e) { MessageBox.Show(e.Message); return; } _fgDetector = new BackgroundSubtractorMOG2(); _blobDetector = new SimpleBlobDetector(); Application.Idle += ProcessFrame; }
private void button2_Click(object sender, EventArgs e) { img = new Image <Bgr, byte>(open.FileName); Image <Gray, byte> imgGray = img.Convert <Gray, byte>(); /* blob detector */ //Gray Gavg = imgGray.GetAverage(); double minValue = 255; double maxValue = 0; Point minLoc = new Point(); Point maxLoc = new Point(); CvInvoke.MinMaxLoc(imgGray, ref minValue, ref maxValue, ref minLoc, ref maxLoc); SimpleBlobDetectorParams blobparams = new SimpleBlobDetectorParams(); blobparams.FilterByArea = true; //斑点面积的限制变量 blobparams.MinArea = 2000; // 斑点的最小面积 blobparams.MaxArea = 300000; // 斑点的最大面积 blobparams.MinThreshold = (float)minValue + 1; //二值化的起始阈值,即公式1的T1 blobparams.MaxThreshold = (float)maxValue; //二值化的终止阈值,即公式1的T2 blobparams.FilterByCircularity = true; ////斑点圆度的限制变量,默认是不限制 blobparams.MinCircularity = (float)0.5; //斑点的最小圆度 blobparams.MaxCircularity = 1; //斑点的最大圆度 blobparams.FilterByConvexity = true; //斑点凸度的限制变量 blobparams.MinConvexity = (float)0.8; //斑点的最小凸度 blobparams.MaxConvexity = 10; //斑点的最大凸度 blobparams.FilterByInertia = true; // //斑点惯性率的限制变量 blobparams.MinInertiaRatio = (float)0.4; //斑点的最小惯性率 blobparams.MaxInertiaRatio = 1; //斑点的最大惯性率 blobparams.FilterByColor = false; //斑点颜色的限制变量 blobparams.blobColor = 255; //斑点颜色的限制变量 blobparams.ThresholdStep = 135; //二值化的阈值步长,即公式1的t blobparams.MinRepeatability = new IntPtr(2); //重复的最小次数,只有属于灰度图像斑点的那些二值图像斑点数量大于该值时,该灰度图像斑点才被认为是特征点 SimpleBlobDetector detector = new SimpleBlobDetector(blobparams); MKeyPoint[] keypoints = detector.Detect(imgGray); Image <Bgr, byte> imgBgr = img.Copy(); foreach (MKeyPoint keypoint in keypoints) { imgBgr.Draw(new Rectangle((int)(keypoint.Point.X - keypoint.Size / 2), (int)(keypoint.Point.Y - keypoint.Size / 2), (int)keypoint.Size, (int)keypoint.Size), new Bgr(255, 0, 0), 1); imageBox2.Image = imgBgr; } }
// find circles/dots using blob detection private static void FindBlob(CvCapture cap, CvWindow winScr) { SimpleBlobDetector.Params blobParameters = new SimpleBlobDetector.Params(); // threshold (gray value) blobParameters.MinThreshold = blobMinThreshold; blobParameters.MaxThreshold = blobMaxThreshold; // area (pixel count) blobParameters.FilterByArea = true; blobParameters.MinArea = blobMinArea; blobParameters.MaxArea = blobMaxArea; // circularity blobParameters.FilterByCircularity = true; blobParameters.MinCircularity = blobMinCircularity; // convexity - probably not needed - maybe eleminates false positives blobParameters.FilterByConvexity = true; blobParameters.MinConvexity = blobMinConvexity; //// inertia - what does the values mean exactly //blobParameters.FilterByInertia = true; //blobParameters.MinInertiaRatio = SimpleBlobDetector blobDetector = new SimpleBlobDetector(blobParameters); gray = new IplImage(cap.QueryFrame().Size, BitDepth.U8, 1); while (CvWindow.WaitKey(10) != 27) { IplImage iplImage = PerspectiveCorretoin.GetCorrectedImage(cap.QueryFrame()); Cv.CvtColor(iplImage, gray, ColorConversion.RgbToGray); Mat mat = new Mat(gray); mat.PyrDown(new Size(mat.Width / 2, mat.Height / 2)); KeyPoint[] keypoints = blobDetector.Detect(mat); foreach (KeyPoint item in keypoints) { Cv.DrawCircle(gray, new CvPoint2D32f(item.Pt.X, item.Pt.Y), (int)(item.Size * 3), CvColor.Green); Console.WriteLine("Found blob | size = " + item.Size); } winScr.Image = gray; } }
public BlobDetector(Mat searchMat, int deviceNum, Mat cameraMatrix, Mat distCoeffs) { _searchMat = searchMat; _deviceNum = deviceNum; _cameraMatrix = cameraMatrix; _distCoeffs = distCoeffs; _detectorParams = new SimpleBlobDetectorParams(); _detectorParams.MinThreshold = 10; _detectorParams.MaxThreshold = 200; _detectorParams.FilterByInertia = false; _detectorParams.FilterByConvexity = false; _detectorParams.FilterByArea = false; _detectorParams.FilterByCircularity = false; // _detectorParams.MaxCircularity = 1f; // _detectorParams.MinCircularity = 0.4f; _detectorParams.blobColor = 255; _blobDetector = new SimpleBlobDetector(_detectorParams); }
// Detects Blobs with Detector Framework and stores Top-down view into cached_homoMat void BlobDetection() { SimpleBlobDetector detector = SimpleBlobDetector.create(); Core.flip(cached_initMat, imageMat, 0); keyMat = new MatOfKeyPoint(); detector.detect(imageMat, keyMat); Features2d.drawKeypoints(imageMat, keyMat, outMat); if (keyMat.rows() < 7) { return; } for (int i = 0; i < 7; i++) { srcPointArray[i] = new Point(keyMat.get(i, 0)[0], keyMat.get(i, 0)[1]); } SortBox(); }
private void Run() { //if true, The error log of the Native side OpenCV will be displayed on the Unity Editor Console. Utils.setDebugMode(true); 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.ToString() " + imgMat.ToString()); Mat outImgMat = new Mat(); SimpleBlobDetector blobDetector = SimpleBlobDetector.create(); Debug.Log("blobDetector.getDefaultName() " + blobDetector.getDefaultName()); blobDetector.read(blobparams_yml_filepath); 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; Utils.setDebugMode(false); }
public override void Init() { base.Init(); sBParameter = new SimpleBlobDetectorParams(); sBParameter.blobColor = (byte)actionSimpleBlobData.blobColor; sBParameter.FilterByArea = actionSimpleBlobData.filterByArea; sBParameter.FilterByCircularity = actionSimpleBlobData.filterByCircularity; sBParameter.FilterByConvexity = actionSimpleBlobData.filterByConvexity; sBParameter.FilterByInertia = actionSimpleBlobData.filterByInertia; sBParameter.MaxArea = actionSimpleBlobData.maxArea; sBParameter.MinArea = actionSimpleBlobData.minArea; sBParameter.MaxCircularity = actionSimpleBlobData.maxCircularity; sBParameter.MinCircularity = actionSimpleBlobData.minCircularity; sBParameter.MaxConvexity = actionSimpleBlobData.maxConvexity; sBParameter.MinConvexity = actionSimpleBlobData.minConvexity; sBParameter.MinInertiaRatio = actionSimpleBlobData.minInertiaRatio; sBParameter.MinDistBetweenBlobs = actionSimpleBlobData.minDistance; sBDetector = new SimpleBlobDetector(sBParameter); String filename = @".//Parameter/Model/SimpleBlob Model/model.jpg"; try { Mat mat = CvInvoke.Imread(filename, Emgu.CV.CvEnum.ImreadModes.AnyColor); _imageTemple = new Image <Gray, byte>(mat.Bitmap); } catch (Exception) { } }
private void Button_Click_Contours(object sender, RoutedEventArgs e) { //Mat hierachy = new Mat(); //VectorOfVectorOfPoint contours = new VectorOfVectorOfPoint(); //Image<Gray, byte> canny = gray.CopyBlank(); //CvInvoke.Canny(gray, canny, 10, 100); //CvInvoke.FindContours(canny, contours, hierachy, RetrType.Tree, ChainApproxMethod.ChainApproxNone); //Image<Gray, byte> draw = gray.CopyBlank(); //for (int i = 0; i < contours.Size; i++) //{ // CvInvoke.DrawContours(draw, contours, i, new MCvScalar(0, 100, 255), 3, LineType.EightConnected, hierachy); // draw.Draw(contours[i].ToArray(), new Gray(0), 5); //} //CvInvoke.Threshold(gray, gray, 100, 255, ThresholdType.Otsu | ThresholdType.Binary); //myGrayImage.Source = ToBitmapSource(canny); SimpleBlobDetectorParams blobParams = new SimpleBlobDetectorParams(); //blobParams.MinThreshold = 0; //blobParams.MaxThreshold = 100; //blobParams.FilterByArea = true; blobParams.MinArea = 10; blobParams.MaxArea = 50; SimpleBlobDetector detector = new SimpleBlobDetector(blobParams); VectorOfKeyPoint keyPoints = new VectorOfKeyPoint(); detector.DetectRaw(gray.ThresholdBinary(new Gray(30), new Gray(255)), keyPoints); //Mat im_with_keypoints = new Mat(); Bgr color = new Bgr(0, 0, 255); //Features2DToolbox.DrawKeypoints(gray, keyPoints, gray, color, Features2DToolbox.KeypointDrawType.DrawRichKeypoints); myGrayImage.Source = ToBitmapSource(gray); }
public override void Process(Image <Bgr, byte> image, out Image <Bgr, byte> annotatedImage, out List <object> data) { base.Process(image, out annotatedImage, out data); using (var dt = new SimpleBlobDetector(_params)) using (var kp = new VectorOfKeyPoint()) { // detect the blobs dt.DetectRaw(image, kp); // draw the blobs Features2DToolbox.DrawKeypoints( image, kp, annotatedImage, new Bgr(_annoColor.Color()), Features2DToolbox.KeypointDrawType.DrawRichKeypoints); // populate the data data = new List <object>(); data.AddRange(kp.ToArray().Select(k => new KeyPoint(k))); } }
private void UpdateBlobParams(object sender, RoutedEventArgs e) { blobParams = new SimpleBlobDetectorParams(); blobParams.FilterByArea = true; blobParams.FilterByColor = true; blobParams.FilterByCircularity = false; blobParams.FilterByConvexity = false; blobParams.FilterByInertia = false; blobParams.blobColor = (byte)255; blobParams.MinThreshold = float.Parse(MinTh.Text); blobParams.MinArea = float.Parse(MinArea.Text); blobParams.MinArea *= blobParams.MinArea; blobParams.MinCircularity = float.Parse(MinCirc.Text); blobParams.MinConvexity = float.Parse(MinConv.Text); blobParams.MinDistBetweenBlobs = float.Parse(MinDist.Text); blobParams.MinInertiaRatio = float.Parse(MinInertia.Text); blobDetector = new SimpleBlobDetector(blobParams); }
// Detects Blobs with Detector Framework and stores Top-down view into cached_homoMat void BlobDetection() { SimpleBlobDetector detector = SimpleBlobDetector.create(); // Core.flip(cached_initMat, imageMat, 0); cached_initMat = imageMat; keyMat = new MatOfKeyPoint(); detector.detect(imageMat, keyMat); // Features2d.drawKeypoints(imageMat, keyMat, outMat); if (keyMat.rows() < 4) { return; } for (int i = 0; i < 4; i++) { srcPointArray[i] = new Point(keyMat.get(i, 0)[0], keyMat.get(i, 0)[1]); } SortPoints(); regPointArray[0] = new Point(0.0, HOMOGRAPHY_HEIGHT); regPointArray[1] = new Point(HOMOGRAPHY_WIDTH, HOMOGRAPHY_HEIGHT); regPointArray[2] = new Point(0.0, 0.0); regPointArray[3] = new Point(HOMOGRAPHY_WIDTH, 0.0); MatOfPoint2f srcPoints = new MatOfPoint2f(srcPointArray); MatOfPoint2f regPoints = new MatOfPoint2f(regPointArray); // Creating the H Matrix Mat Homo_Mat = Calib3d.findHomography(srcPoints, regPoints); Imgproc.warpPerspective(imageMat, cached_homoMat, Homo_Mat, new Size(HOMOGRAPHY_WIDTH, HOMOGRAPHY_HEIGHT)); }
static void Main(string[] args) { String win1 = "Orange Detector"; //The name of the window CvInvoke.NamedWindow(win1); //Create the window using the specific name MCvScalar orangeMin = new MCvScalar(10, 211, 140); MCvScalar orangeMax = new MCvScalar(18, 255, 255); Mat img = new Mat("fruits.jpg", ImreadModes.AnyColor); Mat hsvImg = new Mat(); CvInvoke.CvtColor(img, hsvImg, ColorConversion.Bgr2Hsv); CvInvoke.InRange(hsvImg, new ScalarArray(orangeMin), new ScalarArray(orangeMax), hsvImg); CvInvoke.MorphologyEx(hsvImg, hsvImg, MorphOp.Close, new Mat(), new Point(-1, -1), 5, BorderType.Default, new MCvScalar()); SimpleBlobDetectorParams param = new SimpleBlobDetectorParams(); param.FilterByCircularity = false; param.FilterByConvexity = false; param.FilterByInertia = false; param.FilterByColor = false; param.MinArea = 1000; param.MaxArea = 50000; SimpleBlobDetector detector = new SimpleBlobDetector(param); MKeyPoint[] keypoints = detector.Detect(hsvImg); Features2DToolbox.DrawKeypoints(img, new VectorOfKeyPoint(keypoints), img, new Bgr(255, 0, 0), Features2DToolbox.KeypointDrawType.DrawRichKeypoints); CvInvoke.Imshow(win1, img); //Show image CvInvoke.WaitKey(0); //Wait for key press before executing next line CvInvoke.DestroyWindow(win1); }
static void Main(string[] args) { var srcImage = new Mat(@"..\..\Images\cvlbl.png"); Cv2.ImShow("Source", srcImage); Cv2.WaitKey(1); // do events var binaryImage = new Mat(srcImage.Size(), MatType.CV_8UC1); Cv2.CvtColor(srcImage, binaryImage, ColorConversionCodes.BGRA2GRAY); Cv2.Threshold(binaryImage, binaryImage, thresh: 100, maxval: 255, type: ThresholdTypes.Binary); var detectorParams = new SimpleBlobDetector.Params { //MinDistBetweenBlobs = 10, // 10 pixels between blobs //MinRepeatability = 1, //MinThreshold = 100, //MaxThreshold = 255, //ThresholdStep = 5, FilterByArea = false, //FilterByArea = true, //MinArea = 0.001f, // 10 pixels squared //MaxArea = 500, FilterByCircularity = false, //FilterByCircularity = true, //MinCircularity = 0.001f, FilterByConvexity = false, //FilterByConvexity = true, //MinConvexity = 0.001f, //MaxConvexity = 10, FilterByInertia = false, //FilterByInertia = true, //MinInertiaRatio = 0.001f, FilterByColor = false //FilterByColor = true, //BlobColor = 255 // to extract light blobs }; var simpleBlobDetector = SimpleBlobDetector.Create(detectorParams); var keyPoints = simpleBlobDetector.Detect(binaryImage); Console.WriteLine("keyPoints: {0}", keyPoints.Length); foreach (var keyPoint in keyPoints) { Console.WriteLine("X: {0}, Y: {1}", keyPoint.Pt.X, keyPoint.Pt.Y); } var imageWithKeyPoints = new Mat(); Cv2.DrawKeypoints( image: binaryImage, keypoints: keyPoints, outImage: imageWithKeyPoints, color: Scalar.FromRgb(255, 0, 0), flags: DrawMatchesFlags.DrawRichKeypoints); Cv2.ImShow("Key Points", imageWithKeyPoints); Cv2.WaitKey(1); // do events Cv2.WaitKey(0); Cv2.DestroyAllWindows(); srcImage.Dispose(); imageWithKeyPoints.Dispose(); }
public void Run() { using var src = Cv2.ImRead(FilePath.Image.Shapes); using var detectedCircles = new Mat(); using var detectedOvals = new Mat(); // Invert the image. Shapes has a black background and SimpleBlobDetector doesn't seem to work well with that. Cv2.BitwiseNot(src, src); // Parameters tuned to detect only circles var circleParams = new SimpleBlobDetector.Params { MinThreshold = 10, MaxThreshold = 230, // The area is the number of pixels in the blob. FilterByArea = true, MinArea = 500, MaxArea = 50000, // Circularity is a ratio of the area to the perimeter. Polygons with more sides are more circular. FilterByCircularity = true, MinCircularity = 0.9f, // Convexity is the ratio of the area of the blob to the area of its convex hull. FilterByConvexity = true, MinConvexity = 0.95f, // A circle's inertia ratio is 1. A line's is 0. An oval is between 0 and 1. FilterByInertia = true, MinInertiaRatio = 0.95f }; // Parameters tuned to find the ovals in the Shapes image. var ovalParams = new SimpleBlobDetector.Params { MinThreshold = 10, MaxThreshold = 230, FilterByArea = true, MinArea = 500, // The ovals are the smallest blobs in Shapes, so we limit the max area to eliminate the larger blobs. MaxArea = 10000, FilterByCircularity = true, MinCircularity = 0.58f, FilterByConvexity = true, MinConvexity = 0.96f, FilterByInertia = true, MinInertiaRatio = 0.1f }; using var circleDetector = SimpleBlobDetector.Create(circleParams); var circleKeyPoints = circleDetector.Detect(src); Cv2.DrawKeypoints(src, circleKeyPoints, detectedCircles, Scalar.HotPink, DrawMatchesFlags.DrawRichKeypoints); using var ovalDetector = SimpleBlobDetector.Create(ovalParams); var ovalKeyPoints = ovalDetector.Detect(src); Cv2.DrawKeypoints(src, ovalKeyPoints, detectedOvals, Scalar.HotPink, DrawMatchesFlags.DrawRichKeypoints); using var w1 = new Window("Detected Circles", detectedCircles); using var w2 = new Window("Detected Ovals", detectedOvals); Cv2.WaitKey(); }
static void Main(string[] args) { var srcImage = new Mat(@"..\..\Images\cvlbl.png"); Cv2.ImShow("Source", srcImage); Cv2.WaitKey(1); // do events var binaryImage = new Mat(srcImage.Size(), MatType.CV_8UC1); Cv2.CvtColor(srcImage, binaryImage, ColorConversion.BgrToGray); Cv2.Threshold(binaryImage, binaryImage, thresh: 100, maxval: 255, type: ThresholdType.Binary); var detectorParams = new SimpleBlobDetector.Params { //MinDistBetweenBlobs = 10, // 10 pixels between blobs //MinRepeatability = 1, //MinThreshold = 100, //MaxThreshold = 255, //ThresholdStep = 5, FilterByArea = false, //FilterByArea = true, //MinArea = 0.001f, // 10 pixels squared //MaxArea = 500, FilterByCircularity = false, //FilterByCircularity = true, //MinCircularity = 0.001f, FilterByConvexity = false, //FilterByConvexity = true, //MinConvexity = 0.001f, //MaxConvexity = 10, FilterByInertia = false, //FilterByInertia = true, //MinInertiaRatio = 0.001f, FilterByColor = false //FilterByColor = true, //BlobColor = 255 // to extract light blobs }; var simpleBlobDetector = new SimpleBlobDetector(detectorParams); var keyPoints = simpleBlobDetector.Detect(binaryImage); Console.WriteLine("keyPoints: {0}", keyPoints.Length); foreach (var keyPoint in keyPoints) { Console.WriteLine("X: {0}, Y: {1}", keyPoint.Pt.X, keyPoint.Pt.Y); } var imageWithKeyPoints = new Mat(); Cv2.DrawKeypoints( image: binaryImage, keypoints: keyPoints, outImage: imageWithKeyPoints, color: Scalar.FromRgb(255, 0, 0), flags: DrawMatchesFlags.DrawRichKeypoints); Cv2.ImShow("Key Points", imageWithKeyPoints); Cv2.WaitKey(1); // do events Cv2.WaitKey(0); Cv2.DestroyAllWindows(); srcImage.Dispose(); imageWithKeyPoints.Dispose(); }
Vector3 triangulate(int j, HyperMegaStuff.HyperMegaLines drawer = null) { Ray[] rays = new Ray[2]; Mat workingImage = new Mat(calibrationDevices[j].webcam.leftImage.Height, calibrationDevices[j].webcam.leftImage.Width, calibrationDevices[j].webcam.leftImage.Type(), 0); for (int i = 0; i < 2; i++) { Mat curMat = i == 0 ? calibrationDevices[j].webcam.leftImage : calibrationDevices[j].webcam.rightImage; if (calibrationDevices[j].subtractionImage[i] != null) { // Subtract the background from the curMat Cv2.Subtract(curMat, calibrationDevices[j].subtractionImage[i], workingImage); // Threshold the image to separate black and white Cv2.Threshold(workingImage, workingImage, blobThreshold, 255, ThresholdTypes.BinaryInv); // TODO MAKE THRESHOLD TUNABLE // Detect Blobs using the Mask var settings = new SimpleBlobDetector.Params(); settings.FilterByArea = false; settings.FilterByColor = false; settings.FilterByInertia = true; settings.FilterByConvexity = true; settings.FilterByCircularity = false; SimpleBlobDetector detector = SimpleBlobDetector.Create(); KeyPoint[] blobs = detector.Detect(workingImage, calibrationDevices[j].maskImage[i]); Cv2.DrawKeypoints(workingImage, blobs, workingImage, 255); int biggest = -1; float size = 0; for (int k = 0; k < blobs.Length; k++) { if (blobs[k].Size > size) { biggest = k; size = blobs[k].Size; } } // If there's only one blob in this image, assume it's the white circle if (blobs.Length > 0) { float[] pointArr = { blobs[biggest].Pt.X, blobs[biggest].Pt.Y }; Mat point = new Mat(1, 1, MatType.CV_32FC2, pointArr); Mat undistortedPoint = new Mat(1, 1, MatType.CV_32FC2, 0); Cv2.UndistortPoints(point, undistortedPoint, calibrationDevices[j].calibration.cameras[i].cameraMatrixMat, calibrationDevices[j].calibration.cameras[i].distCoeffsMat, calibrationDevices[j].calibration.cameras[i].rectificationMatrixMat); Point2f[] rectilinear = new Point2f[1]; undistortedPoint.GetArray(0, 0, rectilinear); Transform camera = i == 0 ? calibrationDevices[j].LeftCamera : calibrationDevices[j].RightCamera; rays[i] = new Ray(camera.position, camera.TransformDirection( new Vector3(-rectilinear[0].X, rectilinear[0].Y, 1f))); if (drawer != null) { drawer.color = ((j == 0) != (i == 0)) ? Color.cyan : Color.red; drawer.DrawRay(rays[i].origin, rays[i].direction); } } } } workingImage.Release(); // Only accept the triangulated point if the rays match up closely enough if (rays[0].origin != Vector3.zero && rays[1].origin != Vector3.zero) { Vector3 point1 = RayRayIntersection(rays[0], rays[1]); Vector3 point2 = RayRayIntersection(rays[1], rays[0]); if (Vector3.Distance(point1, point2) < 0.005f) { return((point1 + point2) * 0.5f); } else { return(Vector3.zero); } } else { return(Vector3.zero); } }
private Bitmap Processar(string Caminho, string NomeImagem) { if (pictureBox1.Image == null) { return(null); } CelulasMicronucleadas = 0; CelulasCariolise = 0; CelulasBinucleadas = 0; CelulasNormais = 0; CelulasIgnoradas = 0; CelulasTotais = 0; Background = 0; int PosicaoThresh2 = 0; int PosicaoNucleo2 = 0; SimpleBlobDetector param = new SimpleBlobDetector(); VectorOfKeyPoint keypoint = new VectorOfKeyPoint(); Camera = new Image <Hsv, Byte>(Caminho); CameraResult = new Image <Rgb, Byte>(Caminho); Image <Gray, Byte> ImageSave = new Image <Gray, Byte>(Camera.Cols, Camera.Rows); Image <Rgb, Byte> ImageSave2 = new Image <Rgb, Byte>(Camera.Cols, Camera.Rows); Image <Gray, Byte> Cellthresh = new Image <Gray, Byte>(Camera.Cols, Camera.Rows); Image <Gray, Byte> ThreshSet = new Image <Gray, Byte>(Camera.Cols, Camera.Rows); Image <Gray, Byte> ThreshSet2 = new Image <Gray, Byte>(Camera.Cols, Camera.Rows); Image <Rgb, Byte> separateCellRGB = new Image <Rgb, Byte>(Camera.Cols, Camera.Rows); Image <Hsv, Byte> separateCell = new Image <Hsv, Byte>(Camera.Cols, Camera.Rows); Image <Hsv, Byte> separateNuclei = new Image <Hsv, Byte>(Camera.Cols, Camera.Rows); Image <Gray, Byte> MaskContourNuclei = new Image <Gray, Byte>(Camera.Cols, Camera.Rows); Image <Gray, Byte> MaskContourCell = new Image <Gray, Byte>(Camera.Cols, Camera.Rows); Image <Hsv, Byte> maskCell = new Image <Hsv, Byte>(Camera.Cols, Camera.Rows); Image <Gray, Byte> ContourCompare = new Image <Gray, Byte>(Camera.Cols, Camera.Rows); Image <Gray, Byte> Cellthresh2 = new Image <Gray, Byte>(Camera.Cols, Camera.Rows); Image <Hsv, Byte> nothing = new Image <Hsv, Byte>(Camera.Cols, Camera.Rows); Mat EllipseKernel = new Mat(); Matrix <byte> kernel1 = new Matrix <byte>(new Byte[3, 3] { { 1, 1, 1 }, { 1, 1, 1 }, { 1, 1, 1 } }); var mask = Camera[0]; //ImageSave = mask; //ImageSave.Save("met_hsv.png"); EllipseKernel = CvInvoke.GetStructuringElement(ElementShape.Ellipse, new Size(5, 5), new Point(-1, -1)); CvInvoke.MorphologyEx(mask, mask, MorphOp.Open, EllipseKernel, new Point(-1, -1), 1, BorderType.Default, new MCvScalar()); CvInvoke.MorphologyEx(mask, mask, MorphOp.Close, EllipseKernel, new Point(-1, -1), 1, BorderType.Default, new MCvScalar()); //ImageSave = mask; //ImageSave.Save("met_openclose.png"); CvInvoke.Threshold(mask, mask, 50, 255, ThresholdType.Otsu); //ImageSave = mask; //ImageSave.Save("met_otsu.png"); Mat distanceTransform = new Mat(); CvInvoke.DistanceTransform(mask, distanceTransform, null, Emgu.CV.CvEnum.DistType.L2, 5); CvInvoke.Normalize(distanceTransform, distanceTransform, 0, 255, NormType.MinMax); var markers = distanceTransform.ToImage <Gray, byte>(); //ImageSave = markers; //ImageSave.Save("met_distancetransform.png"); markers = markers.ThresholdBinary(new Gray(100), new Gray(255)); CvInvoke.ConnectedComponents(markers, markers); var finalMarkers = markers.Convert <Gray, Int32>(); CvInvoke.Watershed(Camera, finalMarkers); Image <Gray, byte> boundaries = finalMarkers.Convert <byte>(delegate(Int32 x) { return((byte)(x == -1 ? 255 : 0)); }); boundaries._Dilate(1); //ImageSave = boundaries; //ImageSave.Save("met_watershed.png"); CvInvoke.Threshold(boundaries, boundaries, 0, 255, ThresholdType.BinaryInv); Image <Gray, Byte> CellsBoundaries = new Image <Gray, Byte>(Camera.Cols, Camera.Rows); CellsBoundaries.SetZero(); CellsBoundaries = boundaries & mask; //ImageSave = CellsBoundaries; //ImageSave.Save("met_segmentado.png"); //////////////////////////////////////// // aqui cria o contorno do watershed e separa o nucleo de cada celula encontrada VectorOfVectorOfPoint contours = new VectorOfVectorOfPoint(); Mat hierarchy = new Mat(); CvInvoke.FindContours(CellsBoundaries, contours, hierarchy, RetrType.External, ChainApproxMethod.ChainApproxNone); for (int i = 0; i < contours.Size; i++) { maskCell.SetZero(); separateCell.SetZero(); separateCellRGB.SetZero(); separateNuclei.SetZero(); MaskContourNuclei.SetZero(); MaskContourCell.SetZero(); nothing.SetZero(); double area = CvInvoke.ContourArea(contours[i]); double perimeter = CvInvoke.ArcLength(contours[i], true); //cria a mascara para separar cada célula e seus núcleos //caso o contorno for a area total da foto, ignora if (area < 400) { Background += 1; continue; } //se a celula for muito grande é uma célula a ser ignorada else if (area > 4000 || area < 1000 || perimeter > 250 || perimeter < 100) { CvInvoke.DrawContours(CameraResult, contours, i, new MCvScalar(255, 255, 255)); CelulasIgnoradas += 1; continue; } //se tudo estiver correto, realiza a análise do núcleo da célula else { CvInvoke.DrawContours(maskCell, contours, i, new MCvScalar(255, 255, 255), -1); separateCell = Camera & maskCell; ImageSave = separateCell[0].Convert <Gray, Byte>(); ImageSave.Save("met_mascaracelula.png"); var teste2 = separateCell.Convert <Gray, Byte>(); Double intensity = 0.0; double pixelintensity = 0; for (int cols = 0; cols < teste2.Cols; cols++) { for (int rows = 0; rows < teste2.Rows; rows++) { intensity += teste2.Data[rows, cols, 0]; if (teste2.Data[rows, cols, 0] > 0) { pixelintensity++; } } } intensity = intensity / pixelintensity; // verifica a intensidade de pixels na celula if (intensity < 40) { CvInvoke.DrawContours(CameraResult, contours, i, new MCvScalar(255, 0, 255)); CelulasCariolise++; break; } else if (intensity < 55) { thresh = 120; } else { thresh = 110; } int AchouNucleo = 0; int PosicaoThresh = 0; int PosicaoNucleo = 0; int Nucleos = 0; int Micronucleada = 0; int Binucleada = 0; int Normal = 0; double AreaNucleo1 = 0; double AreaNucleo2 = 0; int OutroNucleo = 0; double AreaOutroNucleo = 0; int PosicaoOutroNucleo = 0; VectorOfVectorOfPoint Nucleithresh = new VectorOfVectorOfPoint(); for (int k = thresh; k > 95; k--) { Mat NucleihierarchyThresh = new Mat(); Mat MorphStructuring = new Mat(); Cellthresh = separateCell[0]; MorphStructuring = CvInvoke.GetStructuringElement(ElementShape.Ellipse, new Size(5, 5), new Point(-1, -1)); CvInvoke.Threshold(Cellthresh, Cellthresh, k, 255, ThresholdType.Binary); CvInvoke.MorphologyEx(Cellthresh, Cellthresh, MorphOp.Dilate, MorphStructuring, new Point(-1, -1), 1, BorderType.Default, new MCvScalar()); CvInvoke.FindContours(Cellthresh, Nucleithresh, NucleihierarchyThresh, RetrType.Tree, ChainApproxMethod.ChainApproxSimple); Nucleos = 0; AchouNucleo = 0; AreaNucleo1 = 0; AreaNucleo2 = 0; OutroNucleo = 0; if (Nucleithresh.Size > 0 && Nucleithresh.Size < 3) { for (int j = 0; j < Nucleithresh.Size; j++) { ContourCompare.SetZero(); MaskContourCell.SetZero(); MaskContourNuclei.SetZero(); double AreaNucleiThresh = CvInvoke.ContourArea(Nucleithresh[j]); double PerimeterNucleiThresh = CvInvoke.ArcLength(Nucleithresh[j], true); CvInvoke.DrawContours(MaskContourCell, contours, i, new MCvScalar(255, 255, 255)); CvInvoke.DrawContours(MaskContourNuclei, Nucleithresh, j, new MCvScalar(255, 255, 255)); ContourCompare = MaskContourCell & MaskContourNuclei; int pixel = 0; for (int cols = 0; cols < ContourCompare.Cols; cols++) { for (int rows = 0; rows < ContourCompare.Rows; rows++) { pixel += ContourCompare.Data[rows, cols, 0]; } } if (AreaNucleiThresh > 100 || pixel > 0) { break; } if (AreaNucleiThresh < 100 && AreaNucleiThresh > 25 && pixel == 0 && AchouNucleo == 0 && PerimeterNucleiThresh < 31) { AchouNucleo = 1; PosicaoThresh = k; PosicaoNucleo = j; ThreshSet = Cellthresh; AreaNucleo1 = AreaNucleiThresh; } if (AreaNucleiThresh < 25 && pixel == 0 && AchouNucleo == 0 && OutroNucleo == 0) { OutroNucleo = 1; AreaOutroNucleo = AreaNucleiThresh; PosicaoOutroNucleo = j; } if (AchouNucleo == 1 && OutroNucleo == 1 && pixel == 0) { Nucleos++; if (Math.Abs(AreaNucleo1 - AreaOutroNucleo) > 20) { Micronucleada = 1; } else { Binucleada = 1; } continue; } if ((AchouNucleo == 1 && OutroNucleo == 0 && pixel == 0)) { Nucleos++; AreaNucleo2 = AreaNucleiThresh; if (Nucleos > 1) { if (Math.Abs(AreaNucleo1 - AreaNucleo2) > 20) { Micronucleada = 1; } else { Binucleada = 1; } continue; } else if (Nucleos == 1) { Normal = 1; } } } } if (AchouNucleo == 1) { break; } } // CONTABILIZA E PINTA A CÉLULA if (AchouNucleo == 1) { int check = 0; for (int j = 0; j < Nucleithresh.Size; j++) { if (Nucleos == 2 && Binucleada == 1) { if (check == 0) // conta somente uma vez! { check = 1; CelulasBinucleadas++; } CvInvoke.DrawContours(CameraResult, Nucleithresh, j, new MCvScalar(255, 0, 0)); CvInvoke.DrawContours(CameraResult, contours, i, new MCvScalar(255, 0, 0)); } else if (Nucleos == 2 && Micronucleada == 1) { if (check == 0) //checa somente uma vez! { check = 1; CelulasMicronucleadas++; } CvInvoke.DrawContours(CameraResult, Nucleithresh, j, new MCvScalar(255, 128, 0)); CvInvoke.DrawContours(CameraResult, Nucleithresh, PosicaoOutroNucleo, new MCvScalar(255, 128, 0)); CvInvoke.DrawContours(CameraResult, contours, i, new MCvScalar(255, 128, 0)); } else if (Nucleos == 1 && Normal == 1) { VectorOfVectorOfPoint NucleithreshFinal = new VectorOfVectorOfPoint(); //VERIFICA A PRESENÇA DE OUTRO NUCLEO DENTRO DA CELULA! for (int k = PosicaoThresh; k > 95; k--) { VectorOfVectorOfPoint Nucleithresh2 = new VectorOfVectorOfPoint(); int AchouNucleo2 = 0; Mat NucleihierarchyThresh2 = new Mat(); Mat MorphStructuring = new Mat(); Mat MorphStructuring2 = new Mat(); Cellthresh = separateCell[0]; Cellthresh2 = separateCell[0]; MorphStructuring = CvInvoke.GetStructuringElement(ElementShape.Ellipse, new Size(5, 5), new Point(-1, -1)); MorphStructuring2 = CvInvoke.GetStructuringElement(ElementShape.Ellipse, new Size(9, 9), new Point(-1, -1)); CvInvoke.Threshold(Cellthresh, Cellthresh, PosicaoThresh, 255, ThresholdType.Binary); //MessageBox.Show(PosicaoThresh.ToString()); CvInvoke.Threshold(Cellthresh2, Cellthresh2, k, 255, ThresholdType.Binary); CvInvoke.MorphologyEx(Cellthresh, Cellthresh, MorphOp.Dilate, MorphStructuring2, new Point(-1, -1), 1, BorderType.Default, new MCvScalar()); CvInvoke.MorphologyEx(Cellthresh2, Cellthresh2, MorphOp.Dilate, MorphStructuring, new Point(-1, -1), 1, BorderType.Default, new MCvScalar()); CvInvoke.MorphologyEx(Cellthresh2, Cellthresh2, MorphOp.Erode, MorphStructuring, new Point(-1, -1), 1, BorderType.Default, new MCvScalar()); Cellthresh2 = Cellthresh2 - Cellthresh; CvInvoke.FindContours(Cellthresh2, Nucleithresh2, NucleihierarchyThresh2, RetrType.Tree, ChainApproxMethod.ChainApproxSimple); if (Nucleithresh2.Size > 0 && Nucleithresh2.Size < 10) { for (int z = 0; z < Nucleithresh2.Size; z++) { ContourCompare.SetZero(); MaskContourCell.SetZero(); MaskContourNuclei.SetZero(); double perimeter2 = CvInvoke.ArcLength(Nucleithresh2[z], true); double AreaNucleiThresh2 = CvInvoke.ContourArea(Nucleithresh2[z]); CvInvoke.DrawContours(MaskContourCell, contours, i, new MCvScalar(255, 255, 255)); CvInvoke.DrawContours(MaskContourNuclei, Nucleithresh2, z, new MCvScalar(255, 255, 255)); ContourCompare = MaskContourCell & MaskContourNuclei; int pixel2 = 0; for (int cols = 0; cols < ContourCompare.Cols; cols++) { for (int rows = 0; rows < ContourCompare.Rows; rows++) { pixel2 += ContourCompare.Data[rows, cols, 0]; } } if (AreaNucleiThresh2 < 50 && AreaNucleiThresh2 > 30 && pixel2 == 0 && AchouNucleo2 == 0 && perimeter2 < 27) { AchouNucleo2 = 1; PosicaoThresh2 = k; PosicaoNucleo2 = z; ThreshSet2 = Cellthresh; NucleithreshFinal = Nucleithresh2; AreaNucleo2 = AreaNucleiThresh2; break; } } } if (AchouNucleo2 == 1 && Math.Abs(AreaNucleo1 - AreaNucleo2) > 20) { Nucleos++; Micronucleada = 1; Normal = 0; break; } else if (AchouNucleo2 == 1 && Math.Abs(AreaNucleo1 - AreaNucleo2) < 20) { Nucleos++; Binucleada = 1; Normal = 0; break; } } if (Normal == 1) { if (check == 0) //checa somente uma vez! { check = 1; CelulasNormais++; } CvInvoke.DrawContours(CameraResult, Nucleithresh, j, new MCvScalar(0, 255, 0)); CvInvoke.DrawContours(CameraResult, contours, i, new MCvScalar(0, 255, 0)); break; } else if (Binucleada == 1) { if (check == 0) //checa somente uma vez! { check = 1; CelulasBinucleadas++; } CvInvoke.DrawContours(CameraResult, Nucleithresh, PosicaoNucleo, new MCvScalar(255, 0, 0)); CvInvoke.DrawContours(CameraResult, NucleithreshFinal, PosicaoNucleo2, new MCvScalar(255, 0, 0)); CvInvoke.DrawContours(CameraResult, contours, i, new MCvScalar(255, 0, 0)); break; } else if (Micronucleada == 1) { if (check == 0) //checa somente uma vez! { check = 1; CelulasMicronucleadas++; } CvInvoke.DrawContours(CameraResult, Nucleithresh, PosicaoNucleo, new MCvScalar(255, 128, 0)); CvInvoke.DrawContours(CameraResult, NucleithreshFinal, PosicaoNucleo2, new MCvScalar(255, 128, 0)); CvInvoke.DrawContours(CameraResult, contours, i, new MCvScalar(255, 128, 0)); break; } } } } if (AchouNucleo == 0) //provavelmente a célula não possui núcleo -- cariólise { CvInvoke.DrawContours(CameraResult, contours, i, new MCvScalar(255, 0, 255)); CelulasCariolise++; } } } NomeImagemSaida = Path.GetFileName(Caminho); CelulasNormais2 = CelulasNormais; CelulasMicronucleadas2 = CelulasMicronucleadas; CelulasBinucleadas2 = CelulasBinucleadas; CelulasCariolise2 = CelulasCariolise; CelulasTotais2 = CelulasNormais2 + CelulasBinucleadas2 + CelulasCariolise2 + CelulasMicronucleadas2; return(CameraResult.ToBitmap()); }
private void DemoIRBlobTrack() { int IRWidth = kinectManager.IRWidth; int IRHeight = kinectManager.IRHeight; //get image and convert to threshold image Mat irImage = new Mat(IRHeight, IRWidth, MatType.CV_8UC4, kinectManager.IRRawData); //rows=height, cols=width Mat ir8Bit = new Mat(); Cv2.CvtColor(irImage, ir8Bit, ColorConversionCodes.RGBA2GRAY); Cv2.Threshold(ir8Bit, ir8Bit, thresh: 200, maxval: 255, type: ThresholdTypes.Binary); //Find blobs SimpleBlobDetector.Params detectorParams = new SimpleBlobDetector.Params { //MinDistBetweenBlobs = 10, // 10 pixels between blobs //MinRepeatability = 1, //MinThreshold = 100, //MaxThreshold = 255, //ThresholdStep = 5, FilterByArea = false, //FilterByArea = true, //MinArea = 0.001f, // 10 pixels squared //MaxArea = 500, FilterByCircularity = false, //FilterByCircularity = true, //MinCircularity = 0.001f, FilterByConvexity = false, //FilterByConvexity = true, //MinConvexity = 0.001f, //MaxConvexity = 10, FilterByInertia = false, //FilterByInertia = true, //MinInertiaRatio = 0.001f, FilterByColor = false //FilterByColor = true, //BlobColor = 255 // to extract light blobs }; SimpleBlobDetector simpleBlobDetector = SimpleBlobDetector.Create(detectorParams); KeyPoint[] blobs = simpleBlobDetector.Detect(ir8Bit); foreach (KeyPoint kp in blobs) { Vector2 blobPt = new Vector2(kp.Pt.X, kp.Pt.Y); //transform ir point to unity world space Vector2 irDimensions = new Vector2(kinectManager.IRWidth, kinectManager.IRHeight); irTrack.transform.localPosition = KinectCVUtilities.TransformTextureToUnity(irPlane, irDimensions, blobPt) + irOffset; //transform ir point to color space, then world space DepthSpacePoint depthPt = new DepthSpacePoint(); depthPt.X = blobPt.x; depthPt.Y = blobPt.y; double depth = GetAvg(kinectManager.DepthData, (int)depthPt.X, (int)depthPt.Y, kinectManager.DepthWidth, kinectManager.DepthHeight); ColorSpacePoint colorMappedPt = kinectManager.Sensor.CoordinateMapper.MapDepthPointToColorSpace(depthPt, (ushort)depth); Vector2 colorDimensions = new Vector2(kinectManager.ColorWidth, kinectManager.ColorHeight); Vector2 colorPt = new Vector2(colorMappedPt.X, colorMappedPt.Y); colorTrack.transform.localPosition = KinectCVUtilities.TransformTextureToUnity(colorPlane, colorDimensions, colorPt) + colorOffset; } //convert back to unity texture, add nice debug drawings Mat irImageKeyPoints = new Mat(); Cv2.DrawKeypoints(ir8Bit, blobs, irImageKeyPoints, color: Scalar.FromRgb(255, 0, 0), flags: DrawMatchesFlags.DrawRichKeypoints); //Convert back to RGBA32 Mat irImageOut = new Mat(IRWidth, IRHeight, MatType.CV_8UC4); Cv2.CvtColor(irImageKeyPoints, irImageOut, ColorConversionCodes.BGR2RGBA); //OpenCV is weird and has it in BGR format //load onto texture byte[] rawTextureData = KinectCVUtilities.ConvertMatToBytes(irImageOut); if (overrideIRTexture) { kinectManager.IRTexture.LoadRawTextureData(rawTextureData); kinectManager.IRTexture.Apply(); } }
public void Labeling_example() { var srcImage = new Mat("./TextSample.png"); Cv2.ImShow("Source", srcImage); Cv2.WaitKey(1); var binaryImage = new Mat(srcImage.Size(), MatType.CV_8UC1); Cv2.CvtColor(srcImage, binaryImage, ColorConversionCodes.BGRA2GRAY); Cv2.Threshold(binaryImage, binaryImage, thresh: 100, maxval: 255, type: ThresholdTypes.Binary); var detectorParams = new SimpleBlobDetector.Params { //MinDistBetweenBlobs = 10, //MinRepeatability = 1, //MinThreshold = 100, //MaxThreshold = 255, //ThresholdStep = 5, FilterByArea = false, //FilterByArea = true, //MinArea = 0.001f, //MaxArea = 500, FilterByCircularity = false, //FilterByCircularity = true, //MinCircularity = 0.001f, FilterByConvexity = false, //FilterByConvexity = true, //MinConvexity = 0.001f, //MaxConvexity = 10, FilterByInertia = false, //FilterByInertia = true, //MinInertiaRatio = 0.001f, FilterByColor = false //FilterByColor = true, //BlobColor = 255 }; var simpleBlobDetector = SimpleBlobDetector.Create(detectorParams); var keyPoints = simpleBlobDetector.Detect(binaryImage); foreach (var keyPoint in keyPoints) { Debug.WriteLine("X: {0}, Y: {1}", keyPoint.Pt.X, keyPoint.Pt.Y); } var imageWithKeyPoints = new Mat(); Cv2.DrawKeypoints( image: binaryImage, keypoints: keyPoints, outImage: imageWithKeyPoints, color: Scalar.FromRgb(255, 0, 0), flags: DrawMatchesFlags.DrawRichKeypoints); Cv2.ImShow("Key Points", imageWithKeyPoints); Cv2.DestroyAllWindows(); srcImage.Dispose(); imageWithKeyPoints.Dispose(); }