Ejemplo n.º 1
0
        void drawFaceRectangles(PersonFace personFace, Mat featureImage)
        {
            Rect faceRect = personFace.GetFace();
            Rect mouthRect = personFace.GetMouth();
            Rect noseRect = personFace.GetNose();
            Rect[] eyesRects = personFace.GetEyes();

            double[] faceLineData = personFace.GetFaceLineData();
            PointGenerator faceLine = new PointGenerator(faceLineData[0], faceLineData[1]);
            Point faceTopPoint = faceLine.GetFromY(faceRect.y);
            Point faceBottomPoint = faceLine.GetFromY(faceRect.y + faceRect.height);

            //Draw face line
            Imgproc.line(featureImage, faceTopPoint, faceBottomPoint, new Scalar(255, 0, 0), 1);

            //Draw rect around the face
            Imgproc.rectangle(featureImage, faceRect, new Scalar(0, 255, 255), 2);

            //Draw circle around face
            Point faceCenter = new Point(faceRect.x + faceRect.width / 2, faceRect.y + faceRect.height / 2);
            Size faceSize = new Size(faceRect.width / 2, faceRect.height / 2);
            Imgproc.ellipse(featureImage, faceCenter,faceSize,0,0,360,new Scalar(172, 203, 227),0);
            Imgproc.ellipse(featureImage, faceCenter, faceSize, 0, 0, 360,new Scalar(0,0,0), 1);

            //Draw rect around the mouth
            Imgproc.rectangle(featureImage, mouthRect, new Scalar(0, 255, 255), 1);

            //Draw rect around the nose
            Imgproc.rectangle(featureImage, noseRect, new Scalar(0, 255, 255), 1);

            //Draw eyes rect and circles
            for (var i = 0; i < eyesRects.Length; i++)
            {
                Rect eye = eyesRects[i];
                Imgproc.rectangle(featureImage, eye, new Scalar(0, 255, 255), 1);
            }

            imageBox2.Image = featureImage;
        }
Ejemplo n.º 2
0
        void drawFaceFeatures(PersonFace personFace, Mat illustratedImg)
        {
            Rect faceRect = personFace.GetFace();
            Rect mouthRect = personFace.GetMouth();
            Rect noseRect = personFace.GetNose();
            Rect[] eyesRects = personFace.GetEyes();

            //Draw face division line
            double[] faceLineData = personFace.GetFaceLineData();
            PointGenerator faceLine = new PointGenerator(faceLineData[0], faceLineData[1]);
            Point faceTopPoint = faceLine.GetFromY(faceRect.y);
            Point faceBottomPoint = faceLine.GetFromY(faceRect.y + faceRect.height);

            //Imgproc.line(illustratedImg, faceTopPoint, faceBottomPoint, new Scalar(255, 0, 0), 1);

            //Get face feature angle
            double faceFeatureAngle = Math.Atan(faceLineData[0]);
            faceFeatureAngle = RadianToDegree(faceFeatureAngle);
            faceFeatureAngle += faceFeatureAngle > 0 ? -90 : 90;

            //Draw face lateral boundaries lines
            //Detect right and left eye
            Rect rightEye, leftEye;
            if (eyesRects[0].x > eyesRects[1].x)
            {
                rightEye = eyesRects[1];
                leftEye = eyesRects[0];
            }
            else
            {
                rightEye = eyesRects[0];
                leftEye = eyesRects[1];
            }

            //get eye line generator
            PointGenerator eyeLines = new PointGenerator(
                getRectCenter(rightEye), getRectCenter(leftEye));

            Point leftFacePoint = eyeLines.GetFromX(getRectCenter(leftEye).x + leftEye.width);
            Point rightFacePoint = eyeLines.GetFromX(getRectCenter(rightEye).x - rightEye.width);

            /* CvInvoke.Circle(image, leftFacePoint, 20,
                 new Bgr(Color.Green).MCvScalar, -1);

             CvInvoke.Circle(image, rightFacePoint, 20,
                 new Bgr(Color.Blue).MCvScalar, -1);*/

            //Get line generators for each side of the face
            double faceLineSlope = faceLineData[0];

            //Left side
            double leftFaceSideOffset = leftFacePoint.y - leftFacePoint.x * faceLineSlope;
            PointGenerator leftFaceLine = new PointGenerator(faceLineSlope, leftFaceSideOffset);

            Point startPointL = leftFaceLine.GetFromY(0);
            Point endPointL = leftFaceLine.GetFromY(illustratedImg.Height);

            //Right side
            double rightFaceSideOffset = rightFacePoint.y - rightFacePoint.x * faceLineSlope;
            PointGenerator rightFaceLine = new PointGenerator(faceLineSlope, rightFaceSideOffset);

            Point startPointR = rightFaceLine.GetFromY(0);
            Point endPointR = rightFaceLine.GetFromY(illustratedImg.Height);

            //Imgproc.line(illustratedImg, startPointL, endPointL, new Scalar(0,255,0), 5);
            //Imgproc.line(illustratedImg, startPointR, endPointR,new Scalar(255,0,0), 3);

            //Draw mouth line
            //Put center on the top for the mouth stay in the middle of the mouth square
            Point mouthCenter = new Point(mouthRect.x + mouthRect.width / 2, mouthRect.y);
            Size mouthSize = new Size(mouthRect.width / 2, mouthRect.height / 2);

            Point mCenter = getRectCenter(mouthRect);

            //Get mouth line generator
            double aFactMouth = Math.Tan(Math.Atan(faceLineSlope) + Math.PI / 2);
            double bfactMouth = mCenter.y - mCenter.x * aFactMouth;
            PointGenerator mouthLine = new PointGenerator(aFactMouth, bfactMouth);

            double leftFaceMouthCrossX = (bfactMouth - leftFaceSideOffset) /
                (faceLineSlope - aFactMouth);

            double rightFaceMouthCrossX = (bfactMouth - rightFaceSideOffset) /
                (faceLineSlope - aFactMouth);

            Point leftFaceMouthCross = mouthLine.GetFromX(leftFaceMouthCrossX);
            Point rightFaceMouthCross = mouthLine.GetFromX(rightFaceMouthCrossX);

            //Get face top line
            double afactTopFace = aFactMouth;   //use the mouth line since this uses the same slope
            double bfactTopFace = faceTopPoint.y - faceTopPoint.x * afactTopFace;
            PointGenerator faceTopLine = new PointGenerator(afactTopFace, bfactTopFace);

            double leftTopFaceCrossX = (bfactTopFace - leftFaceSideOffset) /
                 (faceLineSlope - afactTopFace);

            double rightTopFaceCrossX = (bfactTopFace - rightFaceSideOffset) /
                (faceLineSlope - afactTopFace);

            Point leftTopFaceCross = faceTopLine.GetFromX(leftTopFaceCrossX);
            Point rightTopFaceCross = faceTopLine.GetFromX(rightTopFaceCrossX);

            /*CvInvoke.Circle(illustratedImg, leftTopFaceCross, 5, new MCvScalar(), -1);
            CvInvoke.Circle(illustratedImg, rightTopFaceCross, 5, new MCvScalar(), -1);
            CvInvoke.Circle(illustratedImg, leftFaceMouthCross, 5, new MCvScalar(), -1);
            CvInvoke.Circle(illustratedImg, rightFaceMouthCross, 5, new MCvScalar(), -1);
            CvInvoke.Circle(illustratedImg, faceBottomPoint, 5, new MCvScalar(), -1);*/

            MatOfPoint facePointsMat = new MatOfPoint(leftTopFaceCross,
                rightTopFaceCross,
                rightFaceMouthCross,
                faceBottomPoint,
                leftFaceMouthCross);

            //CvInvoke.Polylines(image, facePointsVector, true, new Bgr(172, 203, 227).MCvScalar, 1);

            Imgproc.fillConvexPoly(illustratedImg, facePointsMat, new Scalar(255,255,255));

            Imgproc.ellipse(illustratedImg, mouthCenter, mouthSize, faceFeatureAngle, 0, 180, new Scalar(0,0,0), 2);

            Point p1 = faceTopLine.GetFromX(0);
            Point p2 = faceTopLine.GetFromX(illustratedImg.Width);

            //Imgproc.line(illustratedImg, p1, p2, new Scalar(0, 0, 0), 3);

            //Draw nose line
            Point noseCenter = new Point(noseRect.x + noseRect.width / 2,
                noseRect.y + noseRect.height / 2);
            Size noseSize = new Size(noseRect.width / 2, noseRect.height / 2);
            double noseAngle = Math.Atan(faceLineData[0]);
            noseAngle = RadianToDegree(noseAngle);

            Imgproc.ellipse(illustratedImg, noseCenter, noseSize, noseAngle, 0, 180, new Scalar(0, 0, 0), 2);

            //Draw eyes ellipses
            foreach (Rect eye in personFace.GetEyes())
            {
                Point eyeCenter = new Point(eye.x + eye.width / 2, eye.y + eye.height / 2);
                Size ellipseSize = new Size(eye.width / 5, eye.height / 2);

                Imgproc.ellipse(illustratedImg, eyeCenter, ellipseSize, faceFeatureAngle, 0, 360, new Scalar(0, 0, 0), -1);
            }

            Imgproc.line(illustratedImg, faceBottomPoint, new Point(illustratedImg.width() / 2, illustratedImg.height()), new Scalar(0, 0, 0));
        }