Exemplo n.º 1
0
        public double CalculateCellLength(VectorOfPoint contour)
        {
            if (null == contour)
            {
                return(double.NaN);
            }

            //TODO hierarchia kihasználása, hogy a nagyobb kontúr meg legyen, ne a doboz növelésével
            var roiRectangle = CvInvoke.BoundingRectangle(contour);
            var ogSize       = roiRectangle.Size;
            var ogPoint      = roiRectangle.Location;

            //ogSize.Width > ogSize.Height ? ogSize.Width * Math.Sqrt(2) : ogSize.Height * Math.Sqrt(2);
            var roiUpScale = Math.Sqrt(2);

            var newSize = new Size((int)(ogSize.Width * roiUpScale), (int)(ogSize.Height * roiUpScale));

            var diffWidth       = newSize.Width - ogSize.Width;
            var diffHeight      = newSize.Height - ogSize.Height;
            var newPoint        = new Point(ogPoint.X - diffWidth / 2, ogPoint.Y - diffHeight / 2);
            var newRoiRectangle = new Rectangle(newPoint, newSize);
            //var roiMat = new Mat(this.ContourImageMat, roiRectangle);//var leftHalf = new Mat(matToSlice, leftHalfRect);
            var roiMat         = new Mat(this.ProcessedImage, roiRectangle);
            var newRoiMat      = new Mat(this.ProcessedImage, newRoiRectangle);
            var roiMatOgPic    = new Mat(this.OgImage, roiRectangle);
            var newRoiMatOgPic = new Mat(this.OgImage, newRoiRectangle);

            if (roiMat.NumberOfChannels > 1)
            {
                CvInvoke.CvtColor(roiMat, roiMat, ColorConversion.Bgra2Gray);
            }
            MainWindow.ImageProcessorExaminer.AddImage(roiMat.CreateNewHardCopyFromMat(), "CalculateCellLength_roiMat");
            MainWindow.ImageProcessorExaminer.AddImage(newRoiMat.CreateNewHardCopyFromMat(), "CalculateCellLength_newRoiMat");
            MainWindow.ImageProcessorExaminer.AddImage(roiMatOgPic.CreateNewHardCopyFromMat(), "CalculateCellLength_roiMatOgPic");
            MainWindow.ImageProcessorExaminer.AddImage(newRoiMatOgPic.CreateNewHardCopyFromMat(), "CalculateCellLength_newRoiMatOgPic");
            var pixelNum = CvInvoke.CountNonZero(roiMat);

            if (pixelNum < 1)
            {
                throw new Exception("Selected ROI is blank!");
            }
            //var tempRect = CvInvoke.MinAreaRect(contour);
            //var rotatedRoiMat = newRoiMat.RotateMat(contour);
            //var rotatedRoiMatAlt = newRoiMat.RotateMat(contour, 90.0);

            var rotatedRoiMat      = newRoiMat.RotMat(contour);
            var rotatedRoiMatOgPic = newRoiMatOgPic.RotMatGreyscale(contour, 0.0);

            //var newGrayScaleContour = rotatedRoiMatOgPic.DetectBiggestCellInMat();
            //var newGrayScaleContour = rotatedRoiMatOgPic.DetectSmallestCellInMat();
            //rotatedRoiMatOgPic.GetContourIntensitySet(newGrayScaleContour);


            //var rotatedRoiMatAlt = newRoiMat.RotMat(contour, 90.0);

            MainWindow.ImageProcessorExaminer.AddImage(rotatedRoiMat.CreateNewHardCopyFromMat(), "CalculateCellLength_rotatedRoiMat");
            MainWindow.ImageProcessorExaminer.AddImage(rotatedRoiMatOgPic.CreateNewHardCopyFromMat(), "CalculateCellLength_rotatedRoiMatOgPic");
            //MainWindow.ImageProcessorExaminer.AddImage(rotatedRoiMatAlt.CreateNewHardCopyFromMat(), "CalculateCellLength_rotatedRoiMatAlt");
            //var pointPair = roiMat.GetPointsOfWidestSliceOfCell();
            //var sizeInPx = Math.Sqrt(Math.Pow(pointPair.Item2.X - pointPair.Item1.X, 2) + Math.Pow(pointPair.Item2.Y - pointPair.Item2.Y, 2));
            var sizeInPx     = rotatedRoiMat.GetWidestSliceOfCellLengthInPx();
            var sizeEndPoint = rotatedRoiMat.GetPointsOfWidestSliceOfCellSegmented();

            var newContours = rotatedRoiMat.DetectCellContoursInMat();


            var sizeInPxWithContours     = rotatedRoiMat.GetWidestSliceOfCellLengthInPxWithContours(newContours[1], newContours[0]);
            var sizeEndPointWithContours = rotatedRoiMat.GetPointsOfWidestSliceOfCellSegmentedWithContours(newContours[1], newContours[0]);


            //var newContour = rotatedRoiMat.DetectBiggestCellInMat();
            //var newContour = rotatedRoiMat.DetectBiggestCellInMat();
            var newContour = rotatedRoiMat.DetectSmallestCellInMat();


            //TEST
            //GetBiggestAreaOfCellWithSlidingWindowAndContourSegmented
            //var test = new Mat(5, 5, rotatedRoiMat.Depth, rotatedRoiMat.NumberOfChannels);
            //test.SetValue(0, 0, (byte)1);
            //test.SetValue(1, 0, (byte)1);
            //test.SetValue(2, 0, (byte)1);
            //test.SetValue(3, 0, (byte)1);
            //test.SetValue(4, 0, (byte)1);
            //test.GetBiggestAreaOfCellWithSlidingWindowAndContourSegmented(1, newContour);

            var sizeInPxWithContour     = rotatedRoiMat.GetWidestSliceOfCellLengthInPxWithContour(newContour);
            var sizeEndPointWithContour = rotatedRoiMat.GetPointsOfWidestSliceOfCellSegmentedWithContour(newContour);
            //var sizeInPxOgPic = rotatedRoiMatOgPic.GetWidestSliceOfCellLengthInPx();
            //var sizeEndPointOgPic = rotatedRoiMatOgPic.GetPointsOfWidestSliceOfCell();
            var angledBoundingRectangle = CvInvoke.MinAreaRect(contour);

            //The order is bottomLeft, topLeft, topRight, bottomRight.
            //Remélhetőleg ez tényleg így van



            // ez mindig a legalsó pontot fogja elöszőpr visszaadni aztán onnan kezd majd el az óramutat járásáával ellentétesen végig menni rajtuk


            //var ogBBoxPoints = angledBoundingRectangle.GetVertices().ReorderBoxPoints();
            //var ogBBoxPoints = angledBoundingRectangle.GetVertices();
            //var ogBBoxSize = angledBoundingRectangle.Size;

            //var bBoxPoints = CvInvoke.MinAreaRect(newContour).GetVertices().ReorderBoxPoints();
            //var refBBoxPoints = CvInvoke.MinAreaRect(newContour).GetVertices();
            //var refBBoxSize = CvInvoke.MinAreaRect(newContour).Size;

            //ogBBoxPoints = ogBBoxPoints.RotatePointsUntilLengthsAreSame(refBBoxPoints);

            //var rotationMat = newRoiMat.RotMatOnly(contour,0);

            //var locTriangleOne = new LocatorTriangle(refBBoxPoints[0], refBBoxPoints[1],sizeEndPoint.Item1);
            //locTriangleOne.CalculateNewPPosition(ogBBoxPoints[0], ogBBoxPoints[1]);
            //locTriangleOne.CalculateNewPPositionReverseAffine(rotationMat);
            //var locTriangleTwo = new LocatorTriangle(refBBoxPoints[2], refBBoxPoints[3], sizeEndPoint.Item2);
            //locTriangleTwo.CalculateNewPPosition(ogBBoxPoints[2], ogBBoxPoints[3]);
            //locTriangleTwo.CalculateNewPPositionReverseAffine(rotationMat);

            //var points = new VectorOfPointF();
            //points.Push(new PointF[]{ locTriangleOne.TransformedPointPMinus, locTriangleOne.TransformedPointPPlus , locTriangleTwo.TransformedPointPMinus, locTriangleTwo.TransformedPointPPlus });
            //points.Push(new PointF[] {  locTriangleOne.TransformedPointPPlus, locTriangleTwo.TransformedPointPPlus });
            Debug.Print(ImgName);
            Debug.Print(contour.GetContourCenterPoint().ToString());
            //First
            var firstPoints = ExtensionMethods.GetDiffractionBandHalvingPointsToDrawBack(sizeEndPoint, contour, newContour);

            this.ImageToDisplay = this.ImageToDisplay.DrawColorPixelsToMat(firstPoints.ConvertToVectorOfPoint(), new MCvScalar(0, 0, 255));
            Debug.Print("FirstMethod");
            Debug.Print(firstPoints.ToPrintableString());
            //Second
            var secondPoints = ExtensionMethods.GetDiffractionBandHalvingPointsToDrawBack(sizeEndPointWithContour, contour, newContour);

            this.ImageToDisplay = this.ImageToDisplay.DrawColorPixelsToMat(secondPoints.ConvertToVectorOfPoint(), new MCvScalar(0, 255, 0));
            Debug.Print("SecondMethod");
            Debug.Print(secondPoints.ToPrintableString());
            //Third
            var thirdPoints = ExtensionMethods.GetDiffractionBandHalvingPointsToDrawBack(sizeEndPointWithContours, contour, newContour);

            this.ImageToDisplay = this.ImageToDisplay.DrawColorPixelsToMat(thirdPoints.ConvertToVectorOfPoint(), new MCvScalar(255, 0, 0));
            Debug.Print("ThirdMethod");
            Debug.Print(thirdPoints.ToPrintableString());
            //var sizeInPxAlt = rotatedRoiMatAlt.GetWidestSliceOfCellLengthInPX();

            //GetSizeWithContoursAsVectorOfPointF

            var newPoints    = ExtensionMethods.getSizeWithContoursAndBoundingBoxes(rotatedRoiMat);
            var fourthPoints = ExtensionMethods.GetDiffractionBandHalvingPointsToDrawBack(newPoints, contour, newContour);

            this.ImageToDisplay = this.ImageToDisplay.DrawColorPixelsToMat(fourthPoints.ConvertToVectorOfPoint(), new MCvScalar(0, 255, 255));
            Debug.Print("FourthMethod");
            Debug.Print(fourthPoints.ToPrintableString());

            var temp = ExtensionMethods.GetSizeWithContoursAsVectorOfPointF(rotatedRoiMat);

            //var sizeInPx = -1;
            //var sizeInPxAlt = -1;
            //var valueToReturn = sizeInPx > sizeInPxAlt ? sizeInPx : sizeInPxAlt;
            //var sizeInPx = - 1;
            return(sizeInPx);
        }