Ejemplo n.º 1
0
        public Mat Plot(ScreenProperties screen, EyeGazeCalibrater calib)
        {
            var pre = new Point3D[Data.Count];

            using (var plot = Plot(screen))
            {
                int i = 0;
                foreach (var item in Data)
                {
                    pre[i] = item.Value.Face.GazeInfo.Vector;
                    calib.Apply(item.Value.Face, screen);
                    i++;
                }

                using (var newPlot = Plot(screen))
                {
                    i = 0;
                    foreach (var item in Data)
                    {
                        item.Value.Face.GazeInfo.Vector = pre[i];
                        item.Value.Face.GazeInfo.UpdateScreenPoint(item.Value.Face, screen);
                        i++;
                    }

                    var fontsize = Core.Cv.GetFontSize(HersheyFonts.HersheyComplexSmall, 0.5);
                    var show     = MatTool.New(new Size(plot.Width * 2, plot.Height), MatType.CV_8UC3);
                    Core.Cv.DrawMatAlpha(show, plot, new Point(0, 0));
                    Core.Cv.DrawMatAlpha(show, newPlot, new Point(plot.Width, 0));

                    return(show);
                }
            }
        }
Ejemplo n.º 2
0
    public static void DoMatAndPng(string rootName, string path)
    {
        string        assetPath = TCommon.RemovePathPrefix(path);
        Material      m         = AssetDatabase.LoadAssetAtPath <Material>(assetPath);
        MatData       data      = MatTool.MatToJson(m);
        string        jsonData  = JsonMapper.ToJson(data);
        StringBuilder sb        = new StringBuilder();
        JsonWriter    write     = new JsonWriter(sb);

        JsonMapper.ToJson(data, write);
        int    idx      = path.LastIndexOf('\\');
        string paraPath = path.Substring(0, idx);

        Debug.Log("paraPath:" + paraPath);
        paraPath = paraPath + "\\mat.json";
        File.WriteAllText(paraPath, sb.ToString());
        //对mat_json文件标记ab
        AssetImporter importer = AssetImporter.GetAtPath(TCommon.RemovePathPrefix(paraPath));

        importer.assetBundleName    = $"{rootName}_mat";
        importer.assetBundleVariant = TCommon.ABSUFFIX;

        AssetImporter importerTex = AssetImporter.GetAtPath(data.attributes[0].texAttri.Item3.bundleDir);

        importerTex.assetBundleName    = $"{rootName}_png";
        importerTex.assetBundleVariant = TCommon.ABSUFFIX;
    }
Ejemplo n.º 3
0
        private void RenderProc()
        {
            //clear buffer
            buffer.DrawRectangle(new Rect(0, 0, buffer.Width, buffer.Height), Scalar.BgrWhite, -1);

            //project points
            Point[] img2dPoints;
            double[,] jacobian;
            Core.Cv.ProjectPoints(objpoint, rvec.ToArray(), tvec.ToArray(), camera_matrix_buffer, dist_coeffs, out img2dPoints, out jacobian);

            //render obj points
            for (int i = 0; i < img2dPoints.Length / 2; i++)
            {
                Scalar s = Scalar.BgrBlack;
                if (i < objcolor.Count)
                {
                    s = objcolor[i];
                }
                Core.Cv.DrawLine(buffer, img2dPoints[i * 2], img2dPoints[i * 2 + 1], s, 2, LineTypes.AntiAlias);
            }

            //calc rotation vec
            theta = Math.Sqrt(rvec[0] * rvec[0] + rvec[1] * rvec[1] + rvec[2] * rvec[2]);
            vec   = new double[] { rvec[0] / theta, rvec[1] / theta, rvec[2] / theta };

            string fmt = "0.0000";

            Core.Cv.DrawText(buffer, $"rvec:({rvec[0].ToString(fmt)},{rvec[1].ToString(fmt)},{rvec[2].ToString(fmt)}) | tvec:({tvec[0].ToString(fmt)},{tvec[1].ToString(fmt)},{tvec[2].ToString(fmt)})", new Point(0, 20), HersheyFonts.HersheyPlain, 1, Scalar.BgrGreen, 1, LineTypes.AntiAlias);
            Core.Cv.DrawText(buffer, $"vec:({vec[0].ToString(fmt)},{vec[1].ToString(fmt)},{vec[2].ToString(fmt)}) | theta:({theta.ToString(fmt)})", new Point(0, 35), HersheyFonts.HersheyPlain, 1, Scalar.BgrGreen, 1, LineTypes.AntiAlias);
            Core.Cv.ImgShow("window", buffer);

            //transform image
            if (!input.IsEmpty)
            {
                Profiler.Start("Transform");

                Core.Cv.Rodrigues(rvec.ToArray(), out rodriues);
                if (rod != null)
                {
                    rod.Dispose();
                }
                rod = MatTool.New(new Size(3, 3), MatType.CV_64FC1, rodriues);
                if (invertROD)
                {
                    rod = rod.Inv();
                }
                using (Mat output = MatTool.New())
                {
                    transform.Transform(input, output, rod);

                    Core.Cv.ImgShow("transform", output);
                }

                Profiler.End("Transform");
            }
        }
Ejemplo n.º 4
0
        public ThreeDimTests()
        {
            buffer = MatTool.New(new Size(500, 500), MatType.CV_8UC3);

            center               = new Point(buffer.Cols / 2, buffer.Rows / 2);
            focal_length         = buffer.Cols;
            camera_matrix_buffer = new double[, ]
            {
                { focal_length, 0, center.X },
                { 0, focal_length, center.Y },
                { 0, 0, 1 }
            };

            input = MatTool.New(new Size(500, 500), MatType.CV_8UC3);
            for (int i = 0; i < 10; i++)
            {
                double margin = i * (input.Width / 20);
                input.DrawRectangle(new Rect(margin, margin, input.Width - 2 * margin, input.Height - 2 * margin), Scalar.Random(), -1);
            }

            transform = new RotationMatrixTransform();
        }
Ejemplo n.º 5
0
        public Mat Plot(ScreenProperties screen)
        {
            var errorList  = new List <double>();
            var errorMM    = new List <double>();
            var ptList     = new List <Point>();
            var ptDistList = new List <Point>();

            var frameMargin   = new Point(12, 12);
            var frameSize     = new Size(480, 480);
            var frameBackSize = frameSize.Clone();

            frameBackSize.Width  += frameMargin.Y * 2;
            frameBackSize.Height += frameMargin.Y * 2;

            var frame = MatTool.New(frameBackSize, MatType.CV_8UC3);

            using (Mat m = MatTool.New(frameSize, MatType.CV_8UC3))
            {
                m.DrawRectangle(new Rect(0, 0, m.Width, m.Height), Scalar.BgrWhite, -1);

                foreach (var item in Data)
                {
                    var pt     = frameSize.Center;
                    var ptDist = pt.Clone();

                    var key3d = item.Key * (-1 / item.Key.Z);
                    var key   = new Point(key3d.X, key3d.Y);
                    pt.X *= key.X;
                    pt.Y *= key.Y;

                    var gazeVec = item.Value.Face.GazeInfo.Vector;
                    ptDist.X *= gazeVec.X;
                    ptDist.Y *= gazeVec.Y;
                    pt       += frameSize.Center;
                    ptDist   += frameSize.Center;
                    ptList.Add(pt);
                    ptDistList.Add(ptDist);

                    var errorDiff = key - new Point(gazeVec.X, gazeVec.Y);
                    var error     = Math.Sqrt(Math.Pow(errorDiff.X, 2) + Math.Pow(errorDiff.Y, 2));
                    errorList.Add(error);
                    var mmDist = item.Value.Face.LandmarkTransform.Z / item.Value.Face.UnitPerMM;
                    errorMM.Add(Math.Abs(screen.Origin.Z - mmDist) * error / 10);
                }

                var errorMax   = errorList.Max();
                var errorMin   = errorList.Min();
                var errorAvg   = errorList.Average();
                var errorMMAvg = errorMM.Average();

                for (int i = 0; i < ptList.Count; i++)
                {
                    var pt     = ptList[i];
                    var ptDist = ptDistList[i];
                    var error  = errorList[i];
                    var alpha  = (error - errorMin) / (errorMax - errorMin);
                    var color  = Scalar.Blend(Scalar.BgrBlue, 1 - alpha, Scalar.BgrRed, alpha);
                    m.DrawArrow(pt, ptDist, color, 1, LineTypes.AntiAlias, 0.15);
                }

                Core.Cv.DrawText(m,
                                 $"Mean error: {errorAvg.ToString("0.000")}\n" +
                                 $"Mean error(cm): {errorMMAvg.ToString("0.00")}\n" +
                                 $"Mean error(degree): {(Math.Atan(errorAvg) / Math.PI * 180).ToString("0.00")}\n" +
                                 $"Samples: {errorList.Count}",
                                 new Point(10, 25), HersheyFonts.HersheyComplexSmall, 0.5, Scalar.BgrBlack, 1);

                frame.DrawRectangle(new Rect(0, 0, frame.Width, frame.Height), new Scalar(64, 64, 64), -1);
                Core.Cv.DrawMatAlpha(frame, m, frameMargin);
            }

            return(frame);
        }
Ejemplo n.º 6
0
        public Mat RoiCropByPercent(Mat frame, double percentOfFace = 0.33)
        {
            double size = Math.Max(Parent.Width, Parent.Height) * percentOfFace;

            return(MatTool.New(frame, new Vision.Rect(Parent.X + Center.X - size * 0.5, Parent.Y + Center.Y - size * 0.5, size, size), true));
        }
Ejemplo n.º 7
0
 public Mat ROI(Mat frame)
 {
     return(MatTool.New(frame, new Rect(Parent.X + X, Parent.Y + Y, Width, Height), true));
 }