Beispiel #1
0
 public override int CompareImg(IVisNode cn)
 {
     int tx = 0;
     int ty = 0;
     int outv = CompareImg(cn.Data, ref tx, ref ty, xcoordsloc, ycoordsloc);
     cn.StartX = tx;
     cn.StartY = ty;
     return outv;
 }
Beispiel #2
0
 public virtual int CompareImg(IVisNode cn)
 {
     int tx = 0;
     int ty = 0;
     int outv = CompareImg(cn.Data, ref tx, ref ty);
     cn.StartX = tx;
     cn.StartY = ty;
     return outv;
 }
Beispiel #3
0
 public override int CompareImg(IVisNode cn)
 {
     int tx = 0;
     int ty = 0;
     int outv = CompareImg(cn.Data, ref tx, ref ty, cn.CachedXCoords, cn.CachedYCoords);
     cn.StartX = tx;
     cn.StartY = ty;
     return outv;
 }
Beispiel #4
0
        public static void Main(string[] args)
        {
            int[,] refimg = null;
            int[,] img1 = null;
            bool show_help = false;
            bool useheuristic = false;
            bool decompose = false;
            bool imgcmp = false;
            string imgcomparer = "sceneparse.FullPixelDiffImageComparer";
            IVisNode[] genos = null;
            int numiter = int.MaxValue;
            var opset = new NDesk.Options.OptionSet() {
                {"r|ref=", "the {REF} image file", (string v) => {
                        refimg = LoadImage(v);
                        useheuristic = true;
                    }},
                {"i|img=", "the {IMAGE} file to load", (string v) => {
                        img1 = LoadImage(v);
                    }},
                {"g|gen=", "comma,separted {LIST} of objects", (string v) => {
                        var objnames = v.Split(',');
                        genos = new IVisNode[objnames.Length];
                        for (int i = 0; i < objnames.Length; ++i) {
                            var nv = objnames[i].DeepCopy();
                            if (!nv.Contains(".")) nv = "sceneparse."+nv;
                            genos[i] = (IVisNode)Activator.CreateInstance(Type.GetType(nv));
                        }
                    }},
                {"l|load=", "object {TYPE} to load", (string v) => {
                        var nv = v.DeepCopy();
                        if (!nv.Contains(".")) nv = "sceneparse."+nv;
                        var diri = new DirectoryInfo(v);
                        var fil = diri.GetFiles("*.xml");
                        genos = new IVisNode[fil.Length];
                        for (int i = 0; i < fil.Length; ++i) {
                            genos[i] = DeSerializeFromFile(fil[i].FullName);
                        }
                    }},
                {"d|decompose=", "comma,separted {LIST} of objects", (string v) => {
                        var objnames = v.Split(',');
                        genos = new IVisNode[objnames.Length];
                        for (int i = 0; i < objnames.Length; ++i) {
                            var nv = objnames[i].DeepCopy();
                            if (!nv.Contains(".")) nv = "sceneparse."+nv;
                            genos[i] = (IVisNode)Activator.CreateInstance(Type.GetType(nv));
                        }
                        numiter = 100;
                        decompose = true;
                        useheuristic = true;
                    }},
                {"t|itr=", "number of {ITERATIONS} to go", (string v) => {
                        numiter = int.Parse(v);
                    }},
                {"m|comparer=", "the {COMPARER} to use", (string v) => {
                        imgcomparer = v.DeepCopy();
                        if (!imgcomparer.Contains("ImageComparer")) imgcomparer += "ImageComparer";
                        if (!imgcomparer.Contains(".")) imgcomparer = "sceneparse."+imgcomparer;
                    }},
                {"topgm=", "png {FILE} to convert", (string v) => {
                        LoadImage(v).ToPGM(v.ReplaceExtension("pgm"));
                    }},
                {"topbm=", "png {FILE} to convert", (string v) => {
                        LoadImage(v).ToPBM(v.ReplaceExtension("pbm"));
                    }},
                {"topng=", "pnm {FILE} to convert", (string v) => {
                        LoadImage(v).ToPNG(v.ReplaceExtension("png"));
                    }},
                {"u|uheu", "use heuristic", (string v) => {
                        if (v != null) {
                            useheuristic = true;
                        }
                    }},
                {"c|compare", "compare images", (string v) => {
                        if (v != null) {
                            imgcmp = true;
                        }
                    }},
                {"h|help", "show this message and exit", (string v) => {
                        show_help = (v != null);
                    }},
            };
            List<string> extrargs;
            try {
                extrargs = opset.Parse (args);
            }
            catch (NDesk.Options.OptionException e) {
                Console.Write (Constants.myname+": ");
                Console.WriteLine (e.Message);
                Console.WriteLine ("Try `"+Constants.myname+" --help' for more information.");
                return;
            }
            if (show_help) {
                ShowHelp(opset);
                return;
            }
            if (imgcmp) {
                if (refimg == null) {
                    Console.WriteLine("need ref img");
                } else if (img1 == null) {
                    Console.WriteLine("need img");
                } else {
                    int xout = 0;
                    int yout = 0;
                    IImageComparer imgc = (IImageComparer)Activator.CreateInstance(Type.GetType(imgcomparer), new object[] {refimg});
                    //IImageComparer imgc = new PixelPropImageComparer(refimg);
                    //int heuv = 0;
                    int heuv = imgc.CompareImg(img1, ref xout, ref yout);
                    //int heuv = SlidingImgComp2(refimg, img1, ref xout, ref yout);
                    Console.WriteLine(heuv+" at "+xout+","+yout);
                }
            }
            if (decompose) {
                int imgn = 0;
                int[,] fullimg = new int[refimg.Width(),refimg.Height()];
                int[,] supstruct = new int[refimg.Width(),refimg.Height()];
                int[,] rendertarg = new int[refimg.Width(),refimg.Height()];
                int subimgn = 0;
                var search = new SearchAstar<IVisNode>((IVisNode cn) => {
                    Console.WriteLine(cn.Describe());
                    Console.WriteLine();
                    if (rendertarg != null) {
                        cn.Data.CopyMatrix(rendertarg, cn.StartX, cn.StartY);
                        rendertarg.ToPBM("out"+subimgn+"-"+imgn);
                        rendertarg.SetRegion(0, cn.StartX, cn.StartX+cn.Data.Width()-1, cn.StartY, cn.StartY+cn.Data.Height()-1);
                    } else {
                        cn.Data.ToPBM("out"+subimgn+"-"+imgn);
                    }
                    cn.SerializeToFile("out"+subimgn+"-"+imgn);
                    ++imgn;
                });
                int BestHeu = int.MaxValue;
                IVisNode BestNode = null;
                IImageComparer imgc = (IImageComparer)Activator.CreateInstance(Type.GetType(imgcomparer), new object[] {refimg});
                search.FlushNodeCache = imgc.FlushNodeCache;
                search.FullFlushNodeCache = imgc.FullFlushNodeCache;
                search.NodeHeuristic = (IVisNode cn) => {
                    //return 0; // disable heuristic

                    return imgc.CompareImg(cn);
                };
                search.NodeTermination = (IVisNode cn) => {
                    if (cn.Heuv < BestHeu) {
                        BestHeu = cn.Heuv;
                        BestNode = cn;
                        search.Lifetime = numiter;
                    }
                    if (cn.Heuv <= 0) {
                        return true;
                    }
                    Console.WriteLine("current heuv is"+cn.Heuv);
                    return false;
                };
                while (true) {
                    search.Lifetime = numiter;
                    search.AddNewRange(genos);
                    search.Run();
                    Console.WriteLine("object type is"+BestNode.Name);
                    Console.WriteLine("object description is"+BestNode.Describe());
                    Console.WriteLine("heuristic value is "+BestNode.Heuv);
                    if (rendertarg != null) {
                        BestNode.Data.CopyMatrix(rendertarg, BestNode.StartX, BestNode.StartY);
                        rendertarg.ToPBM("outresult"+subimgn);
                        //rendertarg.SetRegion(0, BestNode.StartX, BestNode.StartX+BestNode.Data.Width()-1, BestNode.StartY, BestNode.StartY+BestNode.Data.Height()-1);
                    } else {
                        BestNode.Data.ToPBM("outresult"+subimgn);
                    }
                    BestNode.SerializeToFile("outresult"+subimgn);
                    supstruct[BestNode.StartX+BestNode.Width/2, BestNode.StartY+BestNode.Height/2] = 255;
                    supstruct.ToPBM("outresult-sup");
                    fullimg = fullimg.AddMatrix(rendertarg);
                    fullimg.ToPBM("outresult-full");
                    imgc = (IImageComparer)Activator.CreateInstance(Type.GetType(imgcomparer), new object[] {refimg, fullimg});
                    if (!genos.Contains(BestNode))
                        genos = genos.AddResize(BestNode);
                    //rendertarg.SetAll(0);
                    rendertarg.SetRegion(0, BestNode.StartX, BestNode.StartX+BestNode.Data.Width()-1, BestNode.StartY, BestNode.StartY+BestNode.Data.Height()-1);
                    search.Reset();
                    ++subimgn;
                    if (search.BestHeu <= 0)
                        break;
                }
            }
            else if (genos != null) {
                int imgn = 0;
                int[,] rendertarg = null;
                if (refimg != null) {
                    rendertarg = new int[refimg.Width(),refimg.Height()];
                }
                var search = new SearchAstar<IVisNode>((IVisNode cn) => {
                    Console.WriteLine(cn.Describe());
                    Console.WriteLine();
                    if (rendertarg != null) {
                        cn.Data.CopyMatrix(rendertarg, cn.StartX, cn.StartY);
                        rendertarg.ToPBM("out"+imgn);
                        rendertarg.SetRegion(0, cn.StartX, cn.StartX+cn.Data.Width()-1, cn.StartY, cn.StartY+cn.Data.Height()-1);
                    } else {
                        cn.Data.ToPBM("out"+imgn);
                    }
                    cn.SerializeToFile("out"+imgn);
                    ++imgn;
                });
                int BestHeu = int.MaxValue;
                IVisNode BestNode = null;
                if (useheuristic) {
                    IImageComparer imgc = (IImageComparer)Activator.CreateInstance(Type.GetType(imgcomparer), new object[] {refimg});
                    search.FlushNodeCache = imgc.FlushNodeCache;
                    search.FullFlushNodeCache = imgc.FullFlushNodeCache;
                    search.NodeHeuristic = (IVisNode cn) => {
                        //return 0; // disable heuristic

                        return imgc.CompareImg(cn);
                    };
                    search.NodeTermination = (IVisNode cn) => {
                        if (cn.Heuv < BestHeu) {
                            BestHeu = cn.Heuv;
                            BestNode = cn;
                        }
                        if (cn.Heuv <= 0) {
                            return true;
                        }
                        Console.WriteLine("current heuv is"+cn.Heuv);
                        return false;
                    };
                }
                search.Lifetime = numiter;
                search.AddNewRange(genos);
                search.Run();
                if (useheuristic) {
                    Console.WriteLine("object type is"+BestNode.Name);
                    Console.WriteLine("object description is"+BestNode.Describe());
                    Console.WriteLine("heuristic value is "+BestNode.Heuv);
                    if (rendertarg != null) {
                        BestNode.Data.CopyMatrix(rendertarg, BestNode.StartX, BestNode.StartY);
                        rendertarg.ToPBM("outresult");
                        rendertarg.SetRegion(0, BestNode.StartX, BestNode.StartX+BestNode.Data.Width()-1, BestNode.StartY, BestNode.StartY+BestNode.Data.Height()-1);
                    } else {
                        BestNode.Data.ToPBM("outresult");
                    }
                    BestNode.SerializeToFile("outresult");
                }
            }
        }