Example #1
0
 public MainWindow()
 {
     InitializeComponent();
     InitializeViews();
     this.Closed += delegate { System.Diagnostics.Process.GetCurrentProcess().Kill(); };
     LogPanel.Log("ok.");
 }
Example #2
0
        public void OpenImages()
        {
            LogPanel.Log("Opening images...");
            var dialog = new OpenFileDialog {
                Multiselect = true
            };

            if (dialog.ShowDialog() == true)
            {
                var ans = dialog.FileNames;
                if (ans != null)
                {
                    images = new List <MyImage>();
                    foreach (var f in ans)
                    {
                        LogPanel.Log(f);
                        images.Add(new MyImage(f));
                    }
                    LogPanel.Log($"{ans.Length} files selected.");
                    ImageOpened?.Invoke();
                    ShowImages();
                }
                else
                {
                    LogPanel.Log("No files selected.");
                }
            }
            else
            {
                LogPanel.Log("Canceled.");
            }
        }
Example #3
0
        public static MyImageD ToHeatImageD(this MyMatrix image, double exp)
        {
            double mx = double.MinValue, mn = double.MaxValue;

            for (int i = 0; i < image.data.GetLength(0); i++)
            {
                for (int j = 0; j < image.data.GetLength(1); j++)
                {
                    double v = image.data[i, j];
                    if (v > mx)
                    {
                        mx = v;
                    }
                    if (v < mn)
                    {
                        mn = v;
                    }
                }
            }
            LogPanel.Log($"min heat: {mn.ToString("E")}");
            LogPanel.Log($"max heat: {mx.ToString("E")}");
            double[] ans = new double[image.data.Length * 4];
            Parallel.For(0, image.data.GetLength(0), i =>
            {
                for (int j = 0; j < image.data.GetLength(1); j++)
                {
                    double v = image.data[i, j];
                    Utils.GetHeatColor(Math.Pow((v - mn) / (mx - mn), exp), out double r, out double g, out double b);
                    int k      = (i * image.data.GetLength(1) + j) * 4;
                    ans[k + 0] = b;
                    ans[k + 1] = g;
                    ans[k + 2] = r;
                    ans[k + 3] = 1;
                }
Example #4
0
 public List <MyImage> GetImages()
 {
     if (images == null)
     {
         LogPanel.Log("Warning: [SourceImagePanel] images == null");
     }
     return(images);
 }
Example #5
0
 private ImageViewer()
 {
     this.MinHeight = 200;
     this.MaxHeight = 400;
     this.Margin    = new System.Windows.Thickness(2);
     this.Content   = img;
     this.Click    += delegate
     {
         var dialog = new Microsoft.Win32.SaveFileDialog();
         if (dialog.ShowDialog() == true)
         {
             using (var stream = dialog.OpenFile())
             {
                 (img.Source as System.Windows.Media.Imaging.BitmapSource).Save(stream);
             }
             LogPanel.Log($"image saved as {dialog.FileName}");
         }
         else
         {
             LogPanel.Log("canceled.");
         }
     };
 }
Example #6
0
 private void Dispatcher_UnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e)
 {
     LogPanel.Log(e.Exception.ToString());
     e.Handled = true;
 }
Example #7
0
 public LogPanel()
 {
     InitializeViews();
     instance = this;
 }
Example #8
0
        public static List <int> VoteInliners(List <Tuple <double, double, double, double> > points, double tolerance, int tries = 100)
        {
            var make_mat = new Func <List <Tuple <double, double, double, double> >, double[, ]>(standard =>
            {
                // (ax+by+c)/(px+qy+1)=x'
                // (dx+ey+f)/(px+qy+1)=y'
                // (ax+by+c)=x'*(px+qy+1)
                // (dx+ey+f)=y'*(px+qy+1)
                // xa+yb-x'xp-x'yq+c = x'
                // xd+ye-y'xp-y'yq+f = y'
                // xa+yb+c        -x'xp-x'yq = x'
                //        +xd+ye+f-y'xp-y'yq = y'
                double[,] mat = new double[8, 8];
                double[,] val = new double[8, 1];
                System.Diagnostics.Trace.Assert(standard.Count == 4);
                for (int i = 0; i < 8;)
                {
                    // (x,y)->(a,b)
                    var std  = standard[i / 2];
                    double x = std.Item1, y = std.Item2, a = std.Item3, b = std.Item4;
                    (mat[i, 0], mat[i, 1], mat[i, 2]) = (x, y, 1);
                    (mat[i, 6], mat[i, 7])            = (-a * x, -a * y);
                    val[i, 0] = a;
                    i++;
                    (mat[i, 3], mat[i, 4], mat[i, 5]) = (x, y, 1);
                    (mat[i, 6], mat[i, 7])            = (-b * x, -b * y);
                    val[i, 0] = b;
                    i++;
                }
                double[,] inverse = MatrixInverse(mat);
                if (inverse == null)
                {
                    return(null);
                }
                for (int i = 0; i < 64; i++)
                {
                    if (Math.Abs(inverse[i / 8, i % 8]) > 1e4)
                    {
                        return(null);
                    }
                }
                {
                    double[,] test = MatrixProduct(mat, inverse);
                    for (int i = 0; i < test.GetLength(0); i++)
                    {
                        for (int j = 0; j < test.GetLength(1); j++)
                        {
                            double error = (i == j ? 1 : 0) - test[i, j];
                            if (error * error >= 1e-9)
                            {
                                LogPanel.Log($"error = {error}\n{MatrixToString(mat)}\n{MatrixToString(inverse)}");
                            }
                            System.Diagnostics.Trace.Assert(error * error < 1e-9);
                        }
                    }
                }
                double[,] result = MatrixProduct(inverse, val);
                System.Diagnostics.Trace.Assert(result.GetLength(0) == 8 && result.GetLength(1) == 1);
                result = new double[3, 3]
                {
                    { result[0, 0], result[1, 0], result[2, 0] },
                    { result[3, 0], result[4, 0], result[5, 0] },
                    { result[6, 0], result[7, 0], 1 }
                };
                if (Math.Sqrt(result[2, 0] * result[2, 0] + result[2, 1] * result[2, 1]) > 2e-1)
                {
                    return(null);                                                                             // too much perspective
                }
                foreach (var s in standard)
                {
                    double[,] test = MatrixProduct(result, new double[3, 1] {
                        { s.Item1 }, { s.Item2 }, { 1 }
                    });
                    if (Math.Abs(test[2, 0]) < 1e-9)
                    {
                        return(null);
                    }
                    double error_x = s.Item3 - test[0, 0] / test[2, 0];
                    double error_y = s.Item4 - test[1, 0] / test[2, 0];
                    if (error_x * error_x + error_y * error_y >= 1e-5)
                    {
                        LogPanel.Log(MatrixToString(result));
                        foreach (var v in standard)
                        {
                            test = MatrixProduct(result, new double[3, 1] {
                                { v.Item1 }, { v.Item2 }, { 1 }
                            });
                            LogPanel.Log($"({v.Item1}, {v.Item2}) => ({v.Item3}, {v.Item4}) ({test[0, 0] / test[2, 0]}, {test[1, 0] / test[2, 0]}, {test[2, 0]})");
                        }
                        LogPanel.Log($"error_x: {error_x}, error_y: {error_y}");
                    }
                    System.Diagnostics.Trace.Assert(error_x * error_x + error_y * error_y < 1e-5);
                }
                return(result);
            });
            var accepts = new Func <Tuple <double, double, double, double>, double[, ], bool>((p, mat) =>
            {
                var r = MatrixProduct(mat, new double[3, 1] {
                    { p.Item1 }, { p.Item2 }, { 1 }
                });
                double dx = p.Item3 - r[0, 0] / r[2, 0];
                double dy = p.Item4 - r[1, 0] / r[2, 0];
                double d  = dx * dx + dy * dy;
                return(d <= tolerance * tolerance);
            });

            if (points.Count <= 4)
            {
                return(points.Select((v, i) => i).ToList());
            }
            double[,] result_mat = null;
            int max_accept_count = int.MinValue;

            for (int i = 0; i < tries; i++)
            {
                var mat = make_mat(RandomlyPick(points, 4));
                if (mat == null)
                {
                    continue;
                }
                int accept_count = points.Count(p => accepts(p, mat));
                System.Diagnostics.Trace.Assert(accept_count >= 4);
                if (accept_count > max_accept_count)
                {
                    max_accept_count = accept_count; result_mat = mat;
                }
            }
            if (result_mat == null)
            {
                return(new List <int>(0));
            }
            List <int> ans = new List <int>();

            for (int i = 0; i < points.Count; i++)
            {
                if (accepts(points[i], result_mat))
                {
                    ans.Add(i);
                }
            }
            return(ans);
        }