コード例 #1
0
        public static void Classifiy(BackgroundWorker sender, ImageGrid g, ClassifyProcessParam p, int classifier, int NStep, string path, int blurRadius)
        {
            MainWindow.Log("Loading Image (" + p.WorldX + " " + p.WorldY + ")");

            DateTime time = DateTime.Now;

            ImageTile it  = null;
            int       max = (int)Math.Pow(2, p.zoom);

            int TileX = p.TileX - 1;

            for (int x = p.WorldX - 1; x <= p.WorldX + 1; x++, TileX++)
            {
                int TileY = p.TileY - 1;
                for (int y = p.WorldY - 1; y <= p.WorldY + 1; y++, TileY++)
                {
                    if (x >= 0 && x < max && y >= 0 && y < max && TileX >= 0 && TileX < g.width && TileY >= 0 && TileY < g.height)
                    {
                        string id       = g.id + "_" + p.zoom + "_" + y + "_" + x;
                        string filename = "tmp\\sat\\" + (p.zoom) + "_" + (y) + "_" + (x) + ".png";

                        if (x == p.WorldX && y == p.WorldY)
                        {
                            MainWindow.dispatcher.Invoke(() =>
                            {
                                if (imageTiles.ContainsKey(id))
                                {
                                    it = imageTiles[id];
                                }
                                else
                                {
                                    it = new ImageTile(MainWindow.Instance, g, filename, id, p.zoom, TileX, TileY, false);
                                }
                            });
                        }
                        else
                        {
                            MainWindow.dispatcher.Invoke(() =>
                            {
                                if (!imageTiles.ContainsKey(id))
                                {
                                    ImageTile it_ = new ImageTile(MainWindow.Instance, g, filename, id, p.zoom, TileX, TileY, false);
                                    g.AddTile(it_);
                                }
                            });
                        }
                    }
                }
            }

            if (it == null)
            {
                return;
            }
            g.AddTile(it);
            it.Status(new PixelColor(0, 255, 255, 100));

            it.classified = false;
            it.Lock();

            MainWindow.Log("Classifying Image (" + p.WorldX + " " + p.WorldY + ")");

            try
            {
                MainWindow.dispatcher.Invoke(() =>
                {
                    foreach (Classes c in MainWindow.classesList)
                    {
                        g.AddOverlayClass(it, c, MainWindow.Instance);

                        if (c.classifiedPointsList.ContainsKey(it.id))
                        {
                            c.classifiedPointsList.Remove(it.id);
                        }

                        c.classifiedPointsList.Add(it.id, new FeaturePoint[256, 256]);
                    }
                });

                Parallel.For(0, 256, x =>
                {
                    for (int y = 0; y < 256; y++)
                    {
                        FeaturePoint fp = FeaturePoint.GetOrAddFeaturePoint(x, y, it.id);
                        var output      = Array.Empty <double>();

                        switch (classifier)
                        {
                        case 0:
                            dfprocess(MainWindow.Instance.DecisionForest, fp.GetFeatures(), ref output);
                            break;

                        case 1:
                            mlpprocess(MainWindow.Instance.NeuralNetwork, fp.GetFeatures(), ref output);
                            break;

                        case 2:
                            mlpeprocess(MainWindow.Instance.NeuralNetworkEnsemble, fp.GetFeatures(), ref output);
                            break;

                        default: break;
                        }

                        int predictedClass = 0;
                        for (int k = 1; k < output.Length; k++)
                        {
                            if (output[k] > output[predictedClass])
                            {
                                predictedClass = k;
                            }
                        }

                        MainWindow.GetClassByNum(predictedClass).classifiedPointsList[it.id][fp.y, fp.x] = fp;
                    }
                });

                it.Unlock();

                it.classified = true;

                float t = (float)(DateTime.Now - time).TotalSeconds;

                averageClassificationTime = (averageClassificationTime * classificationDone + t) / (classificationDone + 1);

                classificationDone++;
                ReportProgress(sender);

                classificationQueue--;

                MainWindow.Log("Image (" + p.WorldX + " " + p.WorldY + ") Classification done in " + t + "s. Now waiting for postprocessing");

                GC.Collect();

                it.Status(new PixelColor(0, 255, 0, 100));

                classificationDoneQueue.Enqueue(it.id);
            }
            catch (Exception e)
            {
                MainWindow.Log("ERROR when classifying Image (" + p.WorldX + " " + p.WorldY + "): " + e.Message);

                it.Status(new PixelColor(0, 0, 255, 100));

                if (it.classified)
                {
                    classificationQueue++; classificationDone--;
                }

                it.classified = false;
                it.Unlock();

                errorClassificationRecoveryQueue.Enqueue(p);

                GC.Collect();
            }
        }