Example #1
0
        public static ModifiedRotatedRect Cut(ModifiedRotatedRect source, Point pc)
        {
            double distanceUpDown    = Geometry.Distance(source.Pul, source.Pll);
            double distanceLeftRight = Geometry.Distance(source.Pul, source.Pur);

            Point ptL1 = new Point();
            Point ptL2 = new Point();

            Point ptR1 = new Point();
            Point ptR2 = new Point();

            if (distanceUpDown >= distanceLeftRight)
            {
                ptL1 = source.Pul;
                ptL2 = source.Pll;

                ptR1 = source.Pur;
                ptR2 = source.Plr;
            }
            else
            {
                ptL1 = source.Pul;
                ptL2 = source.Pur;

                ptR1 = source.Pll;
                ptR2 = source.Plr;
            }


            Point pllNew = GetIntersectingPoint(ptL1, ptL2, pc);
            Point plrNew = GetIntersectingPoint(ptR1, ptR2, pc);

            List <Point> lv = new List <Point>();

            lv.Add(source.Pul);
            lv.Add(source.Pur);
            lv.Add(pllNew);
            lv.Add(plrNew);

            return(new ModifiedRotatedRect(lv));
        }
Example #2
0
        //public Rectangle MinRectangle(List<Point> lp)
        //{
        //    // find right most  point
        //    //int xRightMost = 0;
        //    //int yUpperMost = 0;

        //    //foreach (Point pt in lp)
        //    //{
        //    //    if (pt.X > xRightMost) xRightMost = pt.X;
        //    //    if (pt.Y > yUpperMost) yUpperMost = pt.Y;
        //    //}

        //    //int xLoc = xRightMost;
        //    //int yLoc = yUpperMost;

        //    //foreach (Point pt in lp)
        //    //{
        //    //    if(pt.X < xLoc) pt.X
        //    //}

        //    // left most
        //}

        public static ModifiedRotatedRect CutArmToHand(ModifiedRotatedRect rotRect)
        {
            double dist1 = Geometry.Distance(rotRect.Pll, rotRect.Pul);

            Point pt1 = rotRect.Pul;
            Point pt2 = rotRect.Pur;

            int xNewL = (rotRect.Pll.X + rotRect.Pul.X) / 2;
            int yNewL = (rotRect.Pll.Y + rotRect.Pul.Y) / 2;

            int   xNewR = (rotRect.Plr.X + rotRect.Pur.X) / 2;
            int   yNewR = (rotRect.Plr.Y + rotRect.Pur.Y) / 2;
            Point pt3   = new Point(xNewL, yNewL);
            Point pt4   = new Point(xNewR, yNewR);

            List <Point> ptL = new List <Point>();

            ptL.Add(pt1);
            ptL.Add(pt2);
            ptL.Add(pt3);
            ptL.Add(pt4);

            return(new ModifiedRotatedRect(ptL));
        }
Example #3
0
        private Rectangle LocateROI()
        {
            int           prediction        = -1;
            VectorOfPoint contourOfInterest = new VectorOfPoint();

            int index = 0;

            index             = ImgProc.LargestContourIndex(_contour);
            contourOfInterest = _contour[index];

            MCvMoments moment = CvInvoke.Moments(contourOfInterest);

            double[] huMoment = moment.GetHuMoment();

            prediction = _svm.Compute(huMoment);

            //foreach (VectorOfPoint vp in _listOfContours.GetRange(0, 5))
            //{
            //    MCvMoments moment = CvInvoke.Moments(vp);
            //    double[] huMoment = moment.GetHuMoment();

            //    prediction = _svm.Compute(huMoment);

            //    if (prediction == CLASSIFICATION_ARM || prediction == CLASSIFICATION_HAND)
            //    {
            //        contourOfInterest = vp;
            //        break;
            //    }
            //}

            if (prediction == CLASSIFICATION_REJECT)
            {
                return(Rectangle.Empty);
            }
            else if (prediction == CLASSIFICATION_HAND)
            {
                //Rectangle rectRotRect = rectRot.MinAreaRect();
                //Rectangle init = CvInvoke.MinAreaRect(contoursEval1[largestContourIndexEval1]).MinAreaRect();
                //Point final = new Point(rectRotRect.X + init.X, rectRotRect.Y + init.Y);

                //return new Rectangle(final, init.Size);


                return(CvInvoke.MinAreaRect(contourOfInterest).MinAreaRect());
            }
            else if (prediction == CLASSIFICATION_ARM)
            {
                Mat         convexityDefect = new Mat();
                VectorOfInt hull            = new VectorOfInt();
                CvInvoke.ConvexHull(contourOfInterest, hull, false, false);
                CvInvoke.ConvexityDefects(contourOfInterest, hull, convexityDefect);
                RotatedRect         rectRot    = CvInvoke.MinAreaRect(contourOfInterest);
                ModifiedRotatedRect rotRectMod = new ModifiedRotatedRect(rectRot);
                int yDel = 0;

                double ptLftToRight = Geometry.Distance(rotRectMod.Pul, rotRectMod.Pur);
                double ptUpToDown   = Geometry.Distance(rotRectMod.Pul, rotRectMod.Pll);

                if (!convexityDefect.IsEmpty)
                {
                    Matrix <int> convex = new Matrix <int>(convexityDefect.Rows, convexityDefect.Cols, convexityDefect.NumberOfChannels);

                    convexityDefect.CopyTo(convex);

                    List <Point> contourTmp = new List <Point>();

                    for (int i = 0; i < contourOfInterest.Size; i++)
                    {
                        contourTmp.Add(contourOfInterest[i]);
                    }


                    List <ConvexDefects> convexDefectList = new List <ConvexDefects>();

                    for (int i = 0; i < convex.Rows; i++)
                    {
                        // do not touch
                        int startIdx = convex.Data[i, 0];
                        int endIdx   = convex.Data[i, 1];
                        int pointIdx = convex.Data[i, 2];

                        Point startPt  = contourOfInterest[startIdx];
                        Point endPt    = contourOfInterest[endIdx];
                        Point defectPt = contourOfInterest[pointIdx];

                        // do not touch
                        convexDefectList.Add(new ConvexDefects(startPt, endPt, defectPt));
                    }


                    if (ptLftToRight <= ptUpToDown)
                    {
                        Point pc1Tmp = convexDefectList[0].DefectPt;
                        Point pc2Tmp = convexDefectList[1].DefectPt;

                        Point pc = pc1Tmp.Y > pc2Tmp.Y ? pc1Tmp : pc2Tmp;


                        Point ptUpLeft   = rotRectMod.Pul;
                        Point ptUpRight  = rotRectMod.Pur;
                        Point ptLowLeft  = rotRectMod.Pll;
                        Point ptLowRight = rotRectMod.Plr;

                        ModifiedRotatedRect rotRectEval1 = ModifiedRotatedRect.Cut(ptUpLeft, ptUpRight, ptLowLeft, ptLowRight, pc);
                        ModifiedRotatedRect rotRectEval2 = ModifiedRotatedRect.Cut(ptUpLeft, ptUpRight, ptLowLeft, ptLowRight, pc, true);

                        Size      sizeFrame    = ImageInput.Size;
                        Rectangle rectROIEval1 = rotRectEval1.ToRect(sizeFrame);
                        Rectangle rectROIEval2 = rotRectEval2.ToRect(sizeFrame);

                        Mat cloneMat1 = ImageInput.Clone().Mat;

                        Mat matToBeEval1 = new Mat(cloneMat1, rectROIEval1);

                        VectorOfVectorOfPoint contoursEval1 = new VectorOfVectorOfPoint();

                        Mat matHierachyEval1 = new Mat();
                        CvInvoke.FindContours(matToBeEval1, contoursEval1, matHierachyEval1, Emgu.CV.CvEnum.RetrType.External,
                                              Emgu.CV.CvEnum.ChainApproxMethod.ChainApproxTc89L1);

                        int largestContourIndexEval1 = ImgProc.LargestContourIndex(contoursEval1);

                        MCvMoments momentEval1 = CvInvoke.Moments(contoursEval1[largestContourIndexEval1]);

                        double[] huMomentsEval1 = momentEval1.GetHuMoment();

                        double[] featureVectorSearch = ScaleValues(huMomentsEval1, 5000.0);

                        int predictionEval1 = _svm.Compute(featureVectorSearch, MulticlassComputeMethod.Elimination);

                        //double[] featureVectorHand = ScaleValues(huMomentsEval1.
                        //    .GetRange(0, _svmMachineHand.Inputs).ToArray(), 1000.0);

                        if (predictionEval1 == CLASSIFICATION_HAND)
                        {
                            Rectangle rectRotRect = rectRot.MinAreaRect();
                            Rectangle init        = CvInvoke.MinAreaRect(contoursEval1[largestContourIndexEval1]).MinAreaRect();
                            Point     final       = new Point(rectRotRect.X + init.X, rectRotRect.Y + init.Y);

                            return(new Rectangle(final, init.Size));
                        }

                        else
                        {
                            return(Rectangle.Empty);
                        }
                    }
                    else
                    {
                        return(Rectangle.Empty);
                    }
                }
                else
                {
                    return(Rectangle.Empty);
                }
            }
            else
            {
                return(Rectangle.Empty);
            }
        }