Ejemplo n.º 1
0
        private static void startImageProcessing(Bitmap A, Bitmap B, string outputfolder)
        {
            ImageProcessor imageProcessor = new ImageProcessor();
            Exporter exporter = new Exporter();
            RegistrationProcessor registrationProcessor = new RegistrationProcessor();

            //Create binary image of A with otsu threshold and save
            Bitmap image1bin = imageProcessor.createBinaryOtsu(A);
            image1bin.Save(outputfolder + "\\Abin.png", ImageFormat.Png);

            //Create binary image of B with otsu threshold and save
            Bitmap image2bin = imageProcessor.createBinaryOtsu(B);
            image2bin.Save(outputfolder + "\\Bbin.png", ImageFormat.Png);

            //Find contour of A - Only longest!
            List<Contour<Point>> contours_image1 = imageProcessor.findContoursWithOpenCV(image1bin);
            exporter.exportToCSV(contours_image1, outputfolder, "contourA.csv");
            Bitmap contour_image1 = exporter.exportToImage(contours_image1, outputfolder, "contourA.png", A.Height, A.Width);

            //Find contour of B - Only longest!
            List<Contour<Point>> contours_image2 = imageProcessor.findContoursWithOpenCV(image2bin);
            exporter.exportToCSV(contours_image2, outputfolder, "contourB.csv");
            Bitmap contour_image2 = exporter.exportToImage(contours_image2, outputfolder, "contourB.png", B.Height, B.Width);

            //Calculate transformation with help of the two contours
            try
            {
                Transformation t1 = registrationProcessor.calculateTransformation(contours_image1, contours_image2);
                //Start registration of contours
                Bitmap registrated_Contours = registrationProcessor.registrationContour(t1, contour_image1, contour_image2);
                registrated_Contours.Save(outputfolder + "\\contoursR.png", ImageFormat.Png);

                //Start registration of orginial images
                Bitmap registrated_originals = registrationProcessor.registrationBitmap(t1, A, B);
                registrated_originals.Save(outputfolder + "\\registration.png", ImageFormat.Png);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
            }
        }
        public Transformation calculateTransformation(List<Contour<Point>> contours_image1, List<Contour<Point>> contours_image2)
        {
            List<Feature> image1_features = new List<Feature>();
            List<Feature> image2_features = new List<Feature>();

            //Get the points of the FIRST (=0) contour
            Point[] points1 = contours_image1[0].ToArray();

            //Calculate the angle at every pixel for picture 1
            for (int i = 0; i < points1.Length; i++)
            {
                AngleCalculator angleCalculator = new AngleCalculator();
                double angle = angleCalculator.calculateAngle (points1, i);
                Feature f = new Feature();
                f.point = points1[i];
                f.angle_at_pixel = angle;
                image1_features.Add(f);
            }

            //Get the points of the FIRST (=0) contour
            Point[] points2 = contours_image2[0].ToArray();

            //Calculate the angle at every pixel for picture 2
            for (int i = 0; i < points2.Length; i++)
            {
                AngleCalculator angleCalculator = new AngleCalculator();
                double angle = angleCalculator.calculateAngle(points2, i);
                Feature f = new Feature();
                f.point = points2[i];
                f.angle_at_pixel = angle;
                image2_features.Add(f);
            }

            GraphAnalyzer graphanalyzer = new GraphAnalyzer();

            //Normalize the shorter contour to longer one
            List<Feature> image1_features_normalized;
            List<Feature> image2_features_normalized;
            if (image1_features.Count < image2_features.Count)
            {
                image1_features_normalized = graphanalyzer.normalize(image2_features, image1_features);
                image2_features_normalized = image2_features;
            }
            else
            {
                image2_features_normalized = graphanalyzer.normalize(image1_features, image2_features);
                image1_features_normalized = image1_features;
            }

            //Moves the contour(graph) of image1 to best matching
            image1_features_normalized = graphanalyzer.matchGraphs(image1_features_normalized, image2_features_normalized);

            //Get the best Features by using extrema [0] image1 --> [1] image2, ....
            List<Feature> bestFeatures = graphanalyzer.findBestFeaturesWithExtrema(image1_features_normalized, image2_features_normalized);
            bestFeatureStorage1.Add(bestFeatures[0]);
            bestFeatureStorage1.Add(bestFeatures[2]);
            bestFeatureStorage2.Add(bestFeatures[1]);
            bestFeatureStorage2.Add(bestFeatures[3]);

            //Calculating the transformation for the best features
            Transformation transformation = calculateTransformationValues(bestFeatures);

            //Exporting the graph
            Exporter exporter = new Exporter();
            exporter.exportFeatures(image1_features_normalized, image2_features_normalized);

            return transformation;
        }