Ejemplo n.º 1
0
        private void PerformClassification(List <Bitmap>[] circles, List <Bitmap>[] triangles, List <Bitmap>[] rectangles, ImageViewModel viewModel)
        {
            var signData = new Dictionary <Bitmap[], Sign>();

            for (int i = 0; i < circles[1].Count; i++)
            {
                var circle    = circles[1][i];
                var colorInfo = ColorInfo.Extract(circle);
                signData.Add(new[] { circles[0][i], circles[1][i] }, new Sign(100, colorInfo[0], colorInfo[1], colorInfo[2], colorInfo[3]));
            }
            for (int i = 0; i < triangles[1].Count; i++)
            {
                var triangle  = triangles[1][i];
                var colorInfo = ColorInfo.Extract(triangle);
                signData.Add(new[] { triangles[0][i], triangles[1][i] }, new Sign(50, colorInfo[0], colorInfo[1], colorInfo[2], colorInfo[3]));
            }
            for (int i = 0; i < rectangles[1].Count; i++)
            {
                var rectangle = rectangles[1][i];
                var colorInfo = ColorInfo.Extract(rectangle);
                signData.Add(new[] { rectangles[0][i], rectangles[1][i] }, new Sign(150, colorInfo[0], colorInfo[1], colorInfo[2], colorInfo[3]));
            }

            var containers = new[]
            {
                viewModel.WarningSigns, viewModel.ProhibitingSigns, viewModel.RegulatorySigns, viewModel.InformationSigns,
                viewModel.TemporarySigns
            };

            var    formatter   = new BinaryFormatter();
            Stream stream      = new FileStream("..\\..\\..\\perceptrons.bin", FileMode.Open, FileAccess.Read, FileShare.None);
            var    perceptrons = (List <Perceptron>)formatter.Deserialize(stream);

            stream.Close();

            var tc = new PrebuildSignsClassifier();

            tc.Teach();
            var signsViewModel = (SignsGrid.DataContext) as SignViewModel;

            signsViewModel.Signs.Clear();

            foreach (var sign in signData)
            {
                var groupIndex       = tc.FindClass(sign.Value) - 1;
                var signsImageSource = BitmapConverter.GetBitmapSource(sign.Key[1]);
                containers[groupIndex].Add(new ImageModel(signsImageSource));
                var perceptron = perceptrons[groupIndex];
                var histogram  =
                    HistogramExtracter.Process(BilinearInterpolation.Resize(sign.Key[0], ResizeWidth, ResizeHeight));
                var classificationResult = perceptron.Classify(histogram).ToList();
                var classIndex           = classificationResult.IndexOf(classificationResult.Max());
                var description          = SignDescription.Get(groupIndex, classIndex);
                signsViewModel.Signs.Add(new SignModel(signsImageSource, description));
            }
        }
Ejemplo n.º 2
0
        private async void ProcessFolder_Click(object sender, RoutedEventArgs e)
        {
            var dialog = new FolderBrowserDialog
            {
                SelectedPath = Environment.GetFolderPath(Environment.SpecialFolder.MyPictures)
            };
            var result = dialog.ShowDialog(this.GetIWin32Window());

            var storage = new Dictionary <string, Bitmap>();

            if (result == System.Windows.Forms.DialogResult.OK)
            {
                foreach (var path in Directory.EnumerateFiles(dialog.SelectedPath, "*.*", SearchOption.AllDirectories)
                         .Where(file => _imageFileExtensions.Any(x => file.EndsWith(x, StringComparison.OrdinalIgnoreCase))))
                {
                    var bitmap = BitmapConverter.GetBitmap(new BitmapImage(new Uri(path)));
                    bitmap = (from object filterObject in FiltersMenu.ItemsSource
                              select(filterObject as FilterModel) into filterModel
                              where filterModel.Enabled
                              select filterModel.Filter)
                             .Aggregate(bitmap, (current, filter) => filter.Process(current));

                    var binarizedImage = BitmapBinarizer.Process(bitmap);
                    var linesTask      = HoughTransform.GetLines(binarizedImage);
                    var circlesTask    = HoughTransform.GetCircles(binarizedImage);

                    var rectanglesBitmaps = await RectanglesExtractor.Extract(bitmap, bitmap, binarizedImage, await linesTask, true);

                    var trianglesBitmaps = await TrianglesExtractor.Extract(bitmap, bitmap, binarizedImage, await linesTask, true);

                    var circleBitmaps = await CirclesExtractor.Extract(bitmap, bitmap, await circlesTask, true);

                    bitmap =
                        rectanglesBitmaps[0].Concat(trianglesBitmaps[0])
                        .Concat(circleBitmaps[0])
                        .OrderBy(b => b.Width * b.Height).Last();
                    storage.Add(path, bitmap);
                }
            }

            foreach (var element in storage)
            {
                BilinearInterpolation.Resize(element.Value, ResizeWidth, ResizeHeight)
                .Save(element.Key + ".processed.bmp", ImageFormat.Bmp);
            }
        }
Ejemplo n.º 3
0
 public Bitmap Resize1920x1080Frame()
 => _scaling.Resize(_frame1920x1080, _scaleX, _scaleY);