Exemple #1
0
        private void Test_Accuracy_Click(object sender, RoutedEventArgs e)
        {
            var Scanned_files = Directory.GetFiles("C:/Users/CPT Danko/Desktop/ObjectDScan");

            foreach (var file in Scanned_files)
            {
                var image = new Image <Gray, byte>(file);
                var name  = Path.GetFileName(file);
                int label = int.Parse(name.Substring(name.IndexOf("_") - 1, 1));
                var index = TestD.FindIndex(x => x.Class == label);

                System.Drawing.PointF[] centers;
                byte[] NoZeroPxls;
                List <Image <Gray, byte> > NewImage = new List <Image <Gray, byte> >();
                System.Drawing.Point[]     contour;

                (NoZeroPxls, centers, contour) = ProceessImage(image);

                var LabeledArray = FrameObj.Calculate_Kmeans(centers, NoZeroPxls, image.Size);
                var Regions      = FrameObj.CalculateRegions(LabeledArray);

                Image <Bgr, byte> colored = new Image <Bgr, byte>(512, 424);



                if (index > -1)
                {
                    TestD[index].Images.Add(image);
                    TestD[index].RegionsValues.Add(Regions.ToArray());
                    TestD[index].ImageContours.Add(contour);
                    TestD[index].NonZeroPixels.Add(NoZeroPxls.Length);
                }
                else
                {
                    ImageDataset img = new ImageDataset();
                    img.Images        = new List <Image <Gray, byte> >();
                    img.RegionsValues = new List <float[]>();
                    img.ImageContours = new List <System.Drawing.Point[]>();
                    img.NonZeroPixels = new List <int>();
                    img.Images.Add(image);
                    img.Class = label;
                    img.RegionsValues.Add(Regions.ToArray());
                    img.ImageContours.Add(contour);
                    img.NonZeroPixels.Add(NoZeroPxls.Length);
                    TestD.Add(img);
                }
            }
            ;

            Train_KNN_Full();
            Test_KNN();


            if (confMat.IsActive == false)
            {
                confMat.Show();
            }
            counter++;
            DetectedClass.Content = counter;
        }
Exemple #2
0
        private void Load_Data()
        {
            var images = Directory.GetFiles("C:/Users/CPT Danko/Desktop/images_1");


            if (File.Exists("C:/Users/CPT Danko/Pictures/ObjectValues.txt"))
            {
                File.Delete("C:/Users/CPT Danko/Pictures/ObjectValues.txt");
            }


            for (var image = 0; image < images.Length; image++)
            {
                var SelectImg = new Image <Gray, byte>(images[image]);
                var ImgName   = Path.GetFileName(images[image]);
                var label     = int.Parse(ImgName.Substring(ImgName.IndexOf("_") - 2, 2));
                var index     = Datasets.FindIndex(x => x.Class == label);



                System.Drawing.PointF[] centers;
                byte[] NoZeroPxls;
                List <Image <Gray, byte> > NewImage = new List <Image <Gray, byte> >();
                System.Drawing.Point[]     contour;

                (NoZeroPxls, centers, contour) = ProceessImage(SelectImg);

                var LabeledArray = FrameObj.Calculate_Kmeans(centers, NoZeroPxls, SelectImg.Size);
                var Regions      = FrameObj.CalculateRegions(LabeledArray);

                Image <Bgr, byte> colored = new Image <Bgr, byte>(512, 424);


                if (label == 6)
                {
                    foreach (var pixel in LabeledArray)
                    {
                        switch (pixel.Z)
                        {
                        case 0:
                            colored[(int)(pixel.Y), (int)(pixel.X)] = new Bgr(0, 255, 0);
                            break;

                        case 1:
                            colored[(int)(pixel.Y), (int)(pixel.X)] = new Bgr(0, 0, 255);
                            break;

                        case 2:
                            colored[(int)(pixel.Y), (int)(pixel.X)] = new Bgr(255, 0, 0);
                            break;

                        case 3:
                            colored[(int)(pixel.Y), (int)(pixel.X)] = new Bgr(0, 247, 255);
                            break;
                        }
                    }

                    CvInvoke.Polylines(colored, Array.ConvertAll(centers, System.Drawing.Point.Round), true, new MCvScalar(255, 255, 255), 1);
                    //BitmapSource FrameBitmap = BitmapSourceConvert.ToBitmapSource(colored);
                    //LoadCapture.Source = FrameBitmap;
                    //colored.Save("C:/Users/CPT Danko/Desktop/Fotky_bakalárka/06.png");
                }


                if (index > -1)
                {
                    Datasets[index].Images.Add(SelectImg);
                    Datasets[index].RegionsValues.Add(Regions.ToArray());
                    Datasets[index].ImageContours.Add(contour);
                    Datasets[index].NonZeroPixels.Add(NoZeroPxls.Length);
                }
                else
                {
                    ImageDataset img = new ImageDataset();
                    img.Images        = new List <Image <Gray, byte> >();
                    img.RegionsValues = new List <float[]>();
                    img.ImageContours = new List <System.Drawing.Point[]>();
                    img.NonZeroPixels = new List <int>();
                    img.Images.Add(SelectImg);
                    img.Class = label;
                    img.RegionsValues.Add(Regions.ToArray());
                    img.ImageContours.Add(contour);
                    img.NonZeroPixels.Add(NoZeroPxls.Length);
                    Datasets.Add(img);
                }



                string ImageTrainData = label + "*" + "*" + ImgName;
                foreach (var region in Regions)
                {
                    ImageTrainData += region.ToString() + "*";
                }



                File.AppendAllText("C:/Users/CPT Danko/Pictures/ObjectValues.txt", ImageTrainData + Environment.NewLine);
            }



            IsDataLoaded = true;
            frameCount   = 0;
        }
Exemple #3
0
        private void DoWork(object sender, DoWorkEventArgs e)
        {
            for (var image = 0; image < images.Length; image++)
            {
                var SelectImg = new Image <Gray, byte>(images[image]);
                var ImgName   = Path.GetFileName(images[image]);
                var label     = int.Parse(ImgName.Substring(ImgName.IndexOf("_") - 2, 2));
                var index     = Datasets.FindIndex(x => x.Class == label);



                System.Drawing.PointF[] centers;
                byte[] NoZeroPxls;
                List <Image <Gray, byte> > NewImage = new List <Image <Gray, byte> >();
                System.Drawing.Point[]     contour;

                (NoZeroPxls, centers, contour) = ProceessImage(SelectImg);

                var LabeledArray = FrameObj.Calculate_Kmeans(centers, NoZeroPxls, SelectImg.Size);
                var Regions      = FrameObj.CalculateRegions(LabeledArray);

                Image <Bgr, byte> colored = new Image <Bgr, byte>(512, 424);



                if (index > -1)
                {
                    Datasets[index].Images.Add(SelectImg);
                    Datasets[index].RegionsValues.Add(Regions.ToArray());
                    Datasets[index].ImageContours.Add(contour);
                    Datasets[index].NonZeroPixels.Add(NoZeroPxls.Length);
                }
                else
                {
                    ImageDataset img = new ImageDataset();
                    img.Images        = new List <Image <Gray, byte> >();
                    img.RegionsValues = new List <float[]>();
                    img.ImageContours = new List <System.Drawing.Point[]>();
                    img.NonZeroPixels = new List <int>();
                    img.Images.Add(SelectImg);
                    img.Class = label;
                    img.RegionsValues.Add(Regions.ToArray());
                    img.ImageContours.Add(contour);
                    img.NonZeroPixels.Add(NoZeroPxls.Length);
                    Datasets.Add(img);
                    Class_Select.Dispatcher.Invoke(() =>
                    {
                        Class_Select.Items.Add(label);
                    }
                                                   );
                    IndexList.Dispatcher.Invoke(() =>
                    {
                        IndexList.Items.Add(label);
                    });
                }



                bckgroundworker1.ReportProgress(image);
            }



            IsDataLoaded = true;
            frameCount   = 0;
        }