async void StartSimulation() { int kase_self = System.Threading.Interlocked.Increment(ref kase); await Task.Run(() => { List <IPointsProvider> points_providers = source_image_panel.GetImages().Select(i => new ImageD_Providers.ImageD_Cache(i.ToImageD()) as IImageD_Provider).Select(i => new PointsProviders.MSOP_DescriptorVector(new PointsProviders.HarrisCornerDetector(i), new MatrixProviders.GrayScale(i)) as IPointsProvider).ToList(); List <IImageD_Provider> image_providers = source_image_panel.GetImages().Select((i, idx) => new ImageD_Providers.PlotPoints(new ImageD_Providers.ImageD_Cache(i.ToImageD()), points_providers[idx]) as IImageD_Provider).ToList(); var provider = new PlaneImages(image_providers, 1000, 600); image_container.Dispatcher.Invoke(() => image_container.Content = new ImageViewer(provider, false)); LogPanel.Log("searching features..."); provider.GetImageD(); LogPanel.Log("matching..."); var points = points_providers.Select(pp => pp.GetPoints().Select(ps => (ps as ImagePoint <PointsProviders.MSOP_DescriptorVector.Descriptor>)).ToList()).ToList(); for (int i = 0; i < points.Count; i++) { for (int j = i + 1; j < points.Count; j++) { List <ImagePoint <PointsProviders.MSOP_DescriptorVector.Descriptor> > p1s = points[i], p2s = points[j]; List <Tuple <double, double, double, double, double> > candidates = new List <Tuple <double, double, double, double, double> >(); foreach (var p1 in p1s) { if (p1.content.try_match(p2s, out ImagePoint p2)) { candidates.Add(Tuple.Create(p1.content.difference((p2 as ImagePoint <PointsProviders.MSOP_DescriptorVector.Descriptor>).content), p1.x, p1.y, p2.x, p2.y)); } } candidates.Sort((p1, p2) => p1.Item1.CompareTo(p2.Item1)); for (int k = 0; k < candidates.Count; k++) { var c = candidates[k]; PlaneImage.AddSpring(provider.images[i], provider.images[j], c.Item2, c.Item3, c.Item4, c.Item5); } } } LogPanel.Log("ok"); while (kase_self == kase) { provider.update_speed(0.1); provider.update_position(); provider.ResetSelf(); provider.GetImageD(); } }); }
async void StartSimulation() { int kase_self = System.Threading.Interlocked.Increment(ref kase); await Task.Run(() => { var images = source_image_panel.GetImages(); List <IPointsProvider> points_providers = images.Select(i => new ImageD_Providers.ImageD_Cache(i.ToImageD()) as IImageD_Provider).Select(i => new PointsProviders.MSOP_DescriptorVector(new PointsProviders.HarrisCornerDetector(i), new MatrixProviders.GrayScale(i)) as IPointsProvider).ToList(); List <IImageD_Provider> image_providers = images.Select((i, idx) => new ImageD_Providers.PlotPoints(new ImageD_Providers.ImageD_Cache(i.ToImageD()), points_providers[idx]) as IImageD_Provider).ToList(); var global_viewer = new Func <PlaneImages>(() => { return(new PlaneImages(image_providers, 5000, 600)); })(); int n = global_viewer.images.Count; image_container.Dispatcher.Invoke(() => image_container.Content = new ImageViewer(global_viewer, false)); LogPanel.Log("searching features..."); Parallel.For(0, n, i => image_providers[i].GetImageD()); global_viewer.GetImageD(); LogPanel.Log("matching..."); var points = points_providers.Select(pp => pp.GetPoints().Select(ps => (ps as ImagePoint <PointsProviders.MSOP_DescriptorVector.Descriptor>)).ToList()).ToList(); var get_displacement = new Func <int, int, Tuple <double, double, int> >((i, j) => { List <ImagePoint <PointsProviders.MSOP_DescriptorVector.Descriptor> > p1s = points[i], p2s = points[j]; List <Tuple <double, double, double, double> > candidates = new List <Tuple <double, double, double, double> >(); Parallel.For(0, p1s.Count, _ => { var p1 = p1s[_]; if (p1.content.try_match(p2s, out ImagePoint p2)) { IImageD_Provider me = image_providers[i], other = image_providers[j]; lock (candidates) candidates.Add(new Tuple <double, double, double, double>( p1.x - 0.5 * me.GetImageD().width, p1.y - 0.5 * me.GetImageD().height, p2.x - 0.5 * other.GetImageD().width, p2.y - 0.5 * other.GetImageD().height)); //candidates.Add(Tuple.Create(p1.content.difference((p2 as ImagePoint<PointsProviders.MSOP_DescriptorVector.Descriptor>).content), p1.x, p1.y, p2.x, p2.y)); } }); var ans = Utils.VoteInliners(candidates, 10); return(new Tuple <double, double, int>( ans.Average(_ => { var v = candidates[_]; return v.Item3 - v.Item1; }), ans.Average(_ => { var v = candidates[_]; return v.Item4 - v.Item2; }), ans.Count)); }); { bool[] vis = new bool[n]; SortedSet <Tuple <int, int, Tuple <double, double, int> > > edge_list = new SortedSet <Tuple <int, int, Tuple <double, double, int> > >(new comparer1()); var add_edges = new Action <int>(u => { Parallel.For(0, n, i => { if (!vis[i]) { var edge = new Tuple <int, int, Tuple <double, double, int> >(u, i, get_displacement(u, i)); lock (edge_list) edge_list.Add(edge); } }); }); vis[0] = true; global_viewer.images[0].set_position(0, 0); add_edges(0); while (edge_list.Count > 0) { var edge = edge_list.First(); edge_list.Remove(edge); if (vis[edge.Item2]) { continue; } LogPanel.Log($"edge: {edge.Item1} → {edge.Item2}"); vis[edge.Item2] = true; var u = global_viewer.images[edge.Item1]; global_viewer.images[edge.Item2].set_position(u.center_x - edge.Item3.Item1, u.center_y - edge.Item3.Item2); add_edges(edge.Item2); } } global_viewer.move_to_center(); global_viewer.ResetSelf(); global_viewer.GetImageD(); LogPanel.Log("ok"); }); }
async void StartSimulation() { //for (int seed = 0; seed < 100; seed++) //{ // int n = Utils.Rand(10, 100); // double[,] m = Utils.MatrixRandom(n, n,-10,10, seed); // double[,] i = Utils.MatrixInverse(m); // double[,] I = Utils.MatrixIdentity(n); // double[,] p = Utils.MatrixProduct(m, i); // System.Diagnostics.Trace.Assert(Utils.MatrixAreEqual(p, I, 1.0E-8)); //} int kase_self = System.Threading.Interlocked.Increment(ref kase); var image_providers = source_image_panel.GetImages().Select(i => new ImageD_Providers.ImageD_Cache(i.ToImageD()) as IImageD_Provider).ToList(); while (image_providers.Any(p => { var i = p.GetImageD(); return(i.width * i.height > 1000000); })) { LogPanel.Log($"scaling down..."); Parallel.For(0, image_providers.Count, i => { var p = image_providers[i]; image_providers[i] = new ImageD_Providers.ImageD_Cache(new ImageD_Providers.Scale(new ImageD_Providers.GaussianBlur(p, 0.5), 0.5).GetImageD()); }); } var global_viewer = new CorrectiveCylinderImages(image_providers, pixel_width, pixel_height); image_container.Content = new ScrollViewer { HorizontalScrollBarVisibility = ScrollBarVisibility.Auto, VerticalScrollBarVisibility = ScrollBarVisibility.Disabled, Content = new ImageViewer(global_viewer, false) }; await Task.Run(() => { global_viewer.InitializeOnPlane(); // stage1: false false false // stage2: true false false // stage3: false false false (roll back to stage1) // stage4: true true false // stage5: true false false (roll back to stage2) // stage6: false false false (roll back to stage1) // stage7: ... (int, int)prev_freedom = (-1, -1); int freedom = 1, cached_freedom = 1; for (DateTime time = DateTime.MinValue; ;) { //freedom = cached_freedom = 10; if (prev_freedom != (freedom, cached_freedom)) { prev_freedom = (freedom, cached_freedom); LogPanel.Log($"refine #{global_viewer.refine_count+1}: freedom: {freedom} ← {cached_freedom} / {CorrectiveCylinderImages.maximum_freedom}"); } bool verbose = false; if ((DateTime.Now - time).TotalSeconds > 30) { verbose = true; } bool result = global_viewer.Refine(freedom + (Utils.RandDouble() < 0.01 ? (Utils.RandDouble() < 0.5 ? 1 : 2) : 0), verbose); if (!result) { if (freedom > 1) { freedom--; } else { freedom = cached_freedom = cached_freedom + 1; } } else if (freedom < cached_freedom) { cached_freedom = freedom; } if (freedom > CorrectiveCylinderImages.maximum_freedom) { //System.Diagnostics.Trace.Assert(freedom == 7); LogPanel.Log("done. generating image..."); System.Diagnostics.Trace.Assert(!global_viewer.Refine(CorrectiveCylinderImages.maximum_freedom, true)); for (int i = 0; i < global_viewer.cylinder_images.Count; i++) { LogPanel.Log($"params of image[{i}]:"); LogPanel.Log(global_viewer.cylinder_images[i].transform.ToString()); } LogPanel.Log("ok."); return; } //if (verbose) //{ // for (int i = 1; i <= CorrectiveCylinderImages.maximum_freedom; i++) // { // if (!global_viewer.Test(i, false)) LogPanel.Log($"problem entry: {i}"); // else LogPanel.Log($"entry {i} seems ok."); // } //} if (verbose) { time = DateTime.Now; } } }); }