Example #1
0
        public Task <MatrixH> Adjust(Bitmap baseBitmap, Size baseOriginalSize, Bitmap targetBitmap, Size targetOriginalSize)
        {
            return(Task.Run(() =>
            {
                var basePoints = keypointDetector.Transform(baseBitmap).Select(p => new IntPoint((int)Math.Round(p.X), (int)Math.Round(p.Y))).ToArray();
                var targetPoints = keypointDetector.Transform(targetBitmap).Select(p => new IntPoint((int)Math.Round(p.X), (int)Math.Round(p.Y))).ToArray();

                var matches = new CorrelationMatching(9, 50, baseBitmap, targetBitmap).Match(basePoints, targetPoints);

                var baseMatchPoints = matches[0].Select(p => new IntPoint(p.X * baseOriginalSize.Width / baseBitmap.Width, p.Y * baseOriginalSize.Height / baseBitmap.Height)).ToArray();
                var targetMatchPoints = matches[1].Select(p => new IntPoint(p.X * targetOriginalSize.Width / targetBitmap.Width, p.Y * targetOriginalSize.Height / targetBitmap.Height)).ToArray();

                var tasks = Enumerable.Range(0, 20).AsParallel().Select(a =>
                {
                    RansacHomographyEstimator ransac = new RansacHomographyEstimator(0.001, 0.99);
                    ransac.Ransac.MaxEvaluations = 1000;
                    ransac.Ransac.MaxSamplings = 100;

                    var homography = ransac.Estimate(baseMatchPoints, targetMatchPoints);

                    if (ransac.Inliers.Length > 0)
                    {
                        var directions =
                            Accord.Math.Matrix.Get(baseMatchPoints, ransac.Inliers).Zip(
                                Accord.Math.Matrix.Get(targetMatchPoints, ransac.Inliers),
                                (point1, point2) => Math.Atan2(point2.Y - point1.Y, point2.X - point1.X) + Math.PI * 2).ToArray();

                        var avgDirection = directions.Average();
                        var variance = directions.Average(d => Math.Pow(d - avgDirection, 2));

                        return new
                        {
                            homography,
                            ransac.Inliers.Length,
                            variance
                        };
                    }
                    else
                    {
                        return null;
                    }
                }).Where(h => h != null).OrderByDescending(h => h.Length).ThenBy(h => h.variance).ToArray();

                return tasks.FirstOrDefault()?.homography;
            }));
        }
Example #2
0
        private void btnFreak_Click(object sender, EventArgs e)
        {
            // Step 1: Detect feature points using FREAK Features Detector
            FastRetinaKeypointDetector freak = new FastRetinaKeypointDetector();

            keyPoints1 = freak.Transform(img1);
            keyPoints2 = freak.Transform(img2);

            // Show the marked points in the original images
            // TODO: The following construct can be simplified
            Bitmap img1mark = new PointsMarker(keyPoints1.Select(x => (IFeaturePoint)x).ToList()).Apply(img1);
            Bitmap img2mark = new PointsMarker(keyPoints2.Select(x => (IFeaturePoint)x).ToList()).Apply(img2);

            // Concatenate the two images together in a single image (just to show on screen)
            Concatenate concatenate = new Concatenate(img1mark);

            pictureBox.Image = concatenate.Apply(img2mark);
        }
        public void ExampleTest()
        {
            Bitmap lena = Accord.Imaging.Image.Clone(Resources.lena512);

            // The freak detector can be used with any other corners detection
            // algorithm. The default corners detection method used is the FAST
            // corners detection. So, let's start creating this detector first:
            //
            var detector = new FastCornersDetector(60);

            // Now that we have a corners detector, we can pass it to the FREAK
            // feature extraction algorithm. Please note that if we leave this
            // parameter empty, FAST will be used by default.
            //
            var freak = new FastRetinaKeypointDetector(detector);

            // Now, all we have to do is to process our image:
            IList <FastRetinaKeypoint> points = freak.Transform(lena).ToList();

            // Afterwards, we should obtain 83 feature points. We can inspect
            // the feature points visually using the FeaturesMarker class as
            //
            FeaturesMarker marker = new FeaturesMarker(points, scale: 20);

            // And showing it on screen with
            // ImageBox.Show(marker.Apply(lena));

            // We can also inspect the feature vectors (descriptors) associated
            // with each feature point. In order to get a descriptor vector for
            // any given point, we can use
            //
            byte[] feature = points[42].Descriptor;

            // By default, feature vectors will have 64 bytes in length. We can also
            // display those vectors in more readable formats such as HEX or base64
            //
            string hex = points[42].ToHex();
            string b64 = points[42].ToBase64();

            // The above base64 result should be:
            //
            //  "3W8M/ev///ffbr/+v3f34vz//7X+f0609v//+++/1+jfq/e83/X5/+6ft3//b4uaPZf7ePb3n/P93/rIbZlf+g=="
            //

            Assert.AreEqual(83, points.Count);
            Assert.AreEqual(64, feature.Length);
            Assert.AreEqual("3W8M/ev///ffbr/+v3f34vz//7X+f0609v//+++/1+jfq/e83/X5/+6ft3//b4uaPZf7ePb3n/P93/rIbZlf+g==", b64);
        }
Example #4
0
        private void process1()
        {
            var bitmap1 = (Bitmap)sourcebox1.Image;
            var bitmap2 = (Bitmap)sourcebox2.Image;
            var hash1   = ImagePhash.ComputeDigest(bitmap1.ToLuminanceImage());
            var hash2   = ImagePhash.ComputeDigest(bitmap2.ToLuminanceImage());
            var score   = ImagePhash.GetCrossCorrelation(hash1, hash2);

            Console.WriteLine("score: {0}", score);

            //threshold value
            var thres = new Threshold(110);

            Grayscale filter = new Grayscale(0.2125, 0.7154, 0.0721);
            // apply the filter to the model
            Bitmap grey1 = filter.Apply(bitmap1);

            thres.ApplyInPlace(grey1);

            // Apply the filter to the observed image
            Bitmap grey2 = filter.Apply(bitmap2);

            thres.ApplyInPlace(grey2);

            int modelPoints = 0, matchingPoints = 0;

            var skewChecker     = new DocumentSkewChecker();
            var angle1          = skewChecker.GetSkewAngle(grey1);
            var rotationFilter1 = new RotateBicubic(-angle1);

            rotationFilter1.FillColor = Color.White;
            grey1 = rotationFilter1.Apply(grey1);

            var angle2          = skewChecker.GetSkewAngle(grey2);
            var rotationFilter2 = new RotateBicubic(-angle2);

            rotationFilter2.FillColor = Color.White;
            grey2 = rotationFilter2.Apply(grey2);

            //CorrelationMatching matcher = new CorrelationMatching(5, grey1, grey2);
            //var results = matcher.GetHashCode();
            var detector = new FastCornersDetector(15);
            var freak    = new FastRetinaKeypointDetector(detector);

            FastRetinaKeypoint[] features1 = freak.Transform(grey1).ToArray();
            modelPoints = features1.Count();

            Console.WriteLine("count: {0}", modelPoints);

            FastRetinaKeypoint[] features2 = freak.Transform(grey2).ToArray();

            Console.WriteLine("count: {0}", features2.Count());

            KNearestNeighborMatching matcher = new KNearestNeighborMatching(7);

            //var length = 0;

            IntPoint[][] results = matcher.Match(features1, features2);
            matchingPoints = results[0].Count(); // similarity of image1 to image2
            ////matchingPoints = results[1].Count(); // similarity of image2 to image1

            Console.WriteLine("matched points: {0}", matchingPoints);

            sourcebox1.Image = bitmap1;
            sourcebox2.Image = bitmap2;
            var marker1 = new FeaturesMarker(features1, 30);
            var marker2 = new FeaturesMarker(features2, 30);



            double similPercent = 0;

            if (matchingPoints <= 0)
            {
                similPercent = 0.0f;
            }
            similPercent = (matchingPoints * 100d) / (double)modelPoints;

            Console.WriteLine("score: {0}", similPercent);

            simil1.Text = similPercent.ToString("##.##") + "%";
            simil2.Text = (score * 100.00d).ToString("##.##") + "%";

            angle_text.Text  = angle2.ToString("##.##") + "°";
            resultbox1.Image = marker1.Apply(grey1);
            resultbox2.Image = marker2.Apply(grey2);
        }
        public void ProcessImageTest()
        {
            Bitmap lena = Accord.Imaging.Image.Clone(Resources.lena512);

            FastRetinaKeypointDetector target = new FastRetinaKeypointDetector();

            IList <FastRetinaKeypoint> actual = target.Transform(lena).ToList();

            string code;

            Assert.AreEqual(1283, actual.Count);

            int i = 0;

            Assert.AreEqual(223, actual[i].X);
            Assert.AreEqual(45, actual[i].Y);
            Assert.AreEqual(80.122163710144434, actual[i].Orientation);
            code = actual[i].ToBinary();
            Assert.AreEqual("00101100101000101100000010111000110111111010111011110000011011110111111111101001011000111111010110100110101001000010100000001100100000111110110100000010100010010000100000110000100010110000000101110001100100101100011010111101100100110101111110000100010100111101100001010101110010000001100111111010100001111000111001101000011101111011110111001101111010101101001111110101010100000100100100101100101010011010011000000000001010000110101010011000111001111011111101110101110011100001001010110010100100001000101001010011", code);
            code = actual[i].ToHex();
            Assert.AreEqual("3445031dfb750ff6fe97c6af65251430c1b74091100cd1808e4963bdc9fa21ca1baa13985fe17116eebdb357cbaf0a9234956500145619e7fdae73484d0951ca", code);
            code = actual[i].ToBase64();
            Assert.AreEqual("NEUDHft1D/b+l8avZSUUMMG3QJEQDNGAjkljvcn6IcobqhOYX+FxFu69s1fLrwqSNJVlABRWGef9rnNITQlRyg==", code);

            i = 124;
            Assert.AreEqual(141.0, actual[i].X);
            Assert.AreEqual(184.0, actual[i].Y);
            Assert.AreEqual(158.74949449286677, actual[i].Orientation);
            code = actual[i].ToBinary();
            Assert.AreEqual("11111000010001010000011000000010000100100010010101000010100000011110101010110111001110010110111110010101100001000100010100001000110010000001010000010000000001010011010110000010101110110000001000010000100000010101011010111011110000001011101110001011100001110010000001000000001011000010100010001001100001101000010100001000000001001010110100000000001010011101001010000111011010011000110010000100110000100010011001010000001010010110001000010000111000011000000000100100110000100001001000010010100100000000100001000001", code);
            code = actual[i].ToHex();
            Assert.AreEqual("1fa2604048a4428157ed9cf6a921a210132808a0ac41dd4008816add03ddd1e1040234149161a11020b500944be196312143640a944608870124434848091082", code);
            code = actual[i].ToBase64();
            Assert.AreEqual("H6JgQEikQoFX7Zz2qSGiEBMoCKCsQd1ACIFq3QPd0eEEAjQUkWGhECC1AJRL4ZYxIUNkCpRGCIcBJENISAkQgg==", code);

            i = 763;
            Assert.AreEqual(104, actual[i].X);
            Assert.AreEqual(332, actual[i].Y);
            Assert.AreEqual(-174.28940686250036, actual[i].Orientation);
            code = actual[i].ToBinary();
            Assert.AreEqual("01010001111011001100011000010110101011111010010001000101001111110111111101101101111000101101010111010110111010001110001011011100111000010000010111100000010010010101010001010001110100010101111011000001101100111001001100001000101100010111011110111100111101110100011000100101001010010011010000111110000100000000000011001000000011111011010011011001001010011101010011111101001001010111000001010100011011110111010000010101100000010110110101111100101111111000100011111001100011100000110011011011100000001010100101011101", code);
            code = actual[i].ToHex();
            Assert.AreEqual("8a376368f525a2fcfeb647ab6b17473b87a007922a8a8b7a83cdc9108dee3def62a4942c7c080013f02d9b942bbfa40e2af62ea881b63efd119f7130db0195ba", code);
            code = actual[i].ToBase64();
            Assert.AreEqual("ijdjaPUlovz+tkeraxdHO4egB5Iqiot6g83JEI3uPe9ipJQsfAgAE/Atm5Qrv6QOKvYuqIG2Pv0Rn3Ew2wGVug==", code);

            i = 1042;
            Assert.AreEqual(116, actual[i].X);
            Assert.AreEqual(410, actual[i].Y);
            Assert.AreEqual(-86.11209043916692, actual[i].Orientation, 1e-10);
            code = actual[i].ToBinary();
            Assert.AreEqual("11110111010011000010101001011011010101100111010011011101001111010110001010110000111001010111101011001011001001101101100011011011110111110000010111111000000000010001001000010110101010010001100011011110101000100101100010101100000100111011101100010110001000111000100001110100001000110001110011111011000001100000111001001000011111111011010101001101110000010101001110111101011100001100110110000100111010100010000001000000011110000110111010011000111100111011111110110101110011100000001001110010100100001000101001110011", code);
            code = actual[i].ToHex();
            Assert.AreEqual("ef3254da6a2ebbbc460da75ed3641bdbfba01f80486895187b451a35c8dd68c4112ec438df607012feadb283cabd0eb3215704021e7619cffdad73404e0951ce", code);
            code = actual[i].ToBase64();
            Assert.AreEqual("7zJU2mouu7xGDade02Qb2/ugH4BIaJUYe0UaNcjdaMQRLsQ432BwEv6tsoPKvQ6zIVcEAh52Gc/9rXNATglRzg==", code);

            i = 1282;
            Assert.AreEqual(425.0, actual[i].X);
            Assert.AreEqual(488.0, actual[i].Y);
            Assert.AreEqual(-70.722526916337571, actual[i].Orientation);
            code = actual[i].ToBinary();
            Assert.AreEqual("10111011111111011101111000011111111101110111111011111111111111111111111111111011011110111111111111111111011011101001111101101110111101111111101111111110111011101111111010110011100100111110101101111111111110111111111110111111111111111111011011101101110111111111111010110101110111000001010101111111100111011000111111110111011100111111111111111101111110101111111111110111010101000111011100111111101011011011111100011011111011111111101111111001111011111011111111110111111111110001001110111110100110101111111001010111", code);
            code = actual[i].ToHex();
            Assert.AreEqual("ddbf7bf8ef7effffffdfdeffff76f976efdf7f777fcdc9d7fedffffdff6fb7fb7fad3ba8feb9f1efceffbf5fffef2aeefcb5fdd8f7df9ff7fdefffc87d597fea", code);
        }