Esempio n. 1
0
        public void Apply(string inputPath, double uniquenessThresh, int tm, int hessianThresh)
        {
            Stopwatch watch      = Stopwatch.StartNew();
            ArrayList allMatches = new ArrayList();
            Tuple <Image <Bgr, byte>, float[]> drawResult;

            float[] recStat;
            // Handling file names.



            string     topic = "";
            TextWriter log   = File.AppendText(inputPath + topic + "/log.txt");

            // Read images.
            Image <Bgr, Byte> element = new Image <Bgr, Byte>(string.Format("{0}{1}/in/element.png", inputPath, topic));
            Image <Bgr, Byte> test    = new Image <Bgr, Byte>(string.Format("{0}{1}/in/test.png", inputPath, topic));
            Bitmap            window  = test.ToBitmap();

            // Convert to gray-level images and save.
            Image <Gray, Byte> gElement = element.Convert <Gray, Byte>();
            Image <Gray, Byte> gTest    = test.Convert <Gray, Byte>();

            gElement.Save(string.Format("{0}{1}/in/g-element.png", inputPath, topic));
            gTest.Save(string.Format("{0}{1}/in/g-test.png", inputPath, topic));

            // Get image dimensions.
            int wfactor = 2;
            // The size of the element image.
            int ex = element.Width;
            int ey = element.Height;
            // The size of the test image.
            int tx = test.Width;
            int ty = test.Height;
            // The distance that the sliding window shifts.
            int xshift = tx / ex / wfactor * 2 - 1;
            int yshift = ty / ey / wfactor * 2 - 1;

            log.WriteLine(string.Format("Element Image: ({0}*{1})\nTest Image:({2}*{3})\n", ex, ey, tx, ty));

            for (int j = 0; j < yshift; j++)
            {
                for (int i = 0; i < xshift; i++)
                {
                    int xstart  = i * ex * wfactor / 2;
                    int ystart  = j * ey * wfactor / 2;
                    int counter = i + j * xshift;

                    Rectangle         r     = new Rectangle(xstart, ystart, ex * wfactor, ey * wfactor);
                    Image <Bgr, Byte> pTest = new Image <Bgr, Byte>(window.Clone(r, window.PixelFormat));
                    pTest.Save(string.Format("{0}{1}/in/part.jpg", inputPath, topic));

                    drawResult = DrawMatches.Draw(gElement, pTest.Convert <Gray, Byte>(), test, xstart, ystart, inputPath, topic, counter, log, uniquenessThresh, tm, hessianThresh);
                    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////



                    log.WriteLine(string.Format("\n\nSub-image #{0}:\n\tLoop #({1}, {2})\n\tSW1 location: ({3}, {4})", counter, i, j, xstart, ystart));
                    test    = drawResult.Item1;
                    recStat = drawResult.Item2;
                    if (recStat[2] > 0)
                    {
                        allMatches.Add(recStat);
                        log.WriteLine(string.Format("\n\tSW2 location: ({0}, {1})\n\tHistogram score: {2}]", recStat[0], recStat[1], recStat[2]));
                    }
                }
            }

            log.WriteLine("The count before consolidation: " + allMatches.Count);

            HashSet <float[]> hash0 = DrawMatches.Consolidate(allMatches, gElement.Width - 1, gElement.Height - 1, log);
            ArrayList         al    = new ArrayList();

            foreach (float[] i in hash0)
            {
                al.Add(i);
            }


            HashSet <float[]> hash = DrawMatches.Consolidate(al, gElement.Width - 1, gElement.Height - 1, log);

            log.WriteLine("The count after consolidation: " + hash.Count);
            //Blue
            TextWriter coordinatesOnMapBlue = File.AppendText(inputPath + topic + "/coordinatesOnMapBlue.txt");

            foreach (float[] i in hash)
            {
                test.Draw(new Rectangle(new Point((int)i[0], (int)i[1]), gElement.Size), new Bgr(Color.Blue), 5);
                coordinatesOnMapBlue.WriteLine("x= " + (int)i[0] + ", y=" + (int)i[1] + "");
            }
            coordinatesOnMapBlue.Close();
            test.Save(string.Format("{0}{1}/out.jpg", inputPath, topic));

            watch.Stop();
            log.WriteLine(watch.Elapsed);

            log.Close();
        }