Пример #1
0
 internal void ClassifiedPointsFromString(string v)
 {
     string[] splitted = v.Split(SaveLoad.Space1);
     foreach (string s in splitted)
     {
         string[] fp = s.Split(SaveLoad.Space2);
         if (fp.Length != 4)
         {
             break;
         }
         AddClassifiedPoints(FeaturePoint.GetOrAddFeaturePoint(int.Parse(fp[2]), int.Parse(fp[3]), fp[1]), fp[0]);
         ImageHelper.DrawOverlayClass(fp[0], int.Parse(fp[2]), int.Parse(fp[3]), this, classColor);
     }
 }
Пример #2
0
        public void AddFeaturePoint(int x, int y, string imageId)
        {
            ImageHelper.DrawOverlayTrain(imageId, x, y, 1, 1, classColor);

            if (!featurePoints.ContainsKey(imageId))
            {
                featurePoints.Add(imageId, new List <FeaturePoint>());
            }
            if (featurePoints[imageId].Exists(p => p.y == y && p.x == x))
            {
                return;
            }

            featurePoints[imageId].Add(FeaturePoint.GetOrAddFeaturePoint(x, y, imageId));
            featuresCount++;
        }
Пример #3
0
        public void AddFeaturePointRange(int x, int y, int width, int height, string imageId)
        {
            ImageHelper.DrawOverlayTrain(imageId, x, y, width, height, classColor);

            for (int i = x; i < x + width; i++)
            {
                for (int j = y; j < y + height; j++)
                {
                    if (!featurePoints.ContainsKey(imageId))
                    {
                        featurePoints.Add(imageId, new List <FeaturePoint>());
                    }
                    if (featurePoints[imageId].Exists(p => p.y == j && p.x == i))
                    {
                        continue;
                    }

                    featurePoints[imageId].Add(FeaturePoint.GetOrAddFeaturePoint(i, j, imageId));
                    featuresCount++;
                }
            }
        }
        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();
            }
        }
        public static void PostProcess(BackgroundWorker sender, ImageGrid g, string id_, int NStep, int blurRadius)
        {
            ImageTile it   = imageTiles[id_];
            DateTime  time = DateTime.Now;

            MainWindow.Log("Postprocessing Image (" + it.worldX + " " + it.worldY + ")");

            try
            {
                //First Erase the not more useful color image
                images.TryRemove(it.baseFileName, out PixelColor[,] r);


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

                for (int step = 0; step < NStep; step++)
                {
                    for (int _x = 0; _x < 256; _x++)
                    {
                        for (int _y = 0; _y < 256; _y++)
                        {
                            int centralClass = -1;
                            int maxClass     = -1;
                            int maxValue     = -1;

                            int[] counters = new int[MainWindow.classesList.Count];

                            for (int i = 0; i < counters.Length; i++)
                            {
                                if (MainWindow.classesList[i].classifiedPointsList[it.id][_y, _x] != null)
                                {
                                    centralClass = i;
                                    break;
                                }
                            }

                            int radius = MainWindow.classesList[centralClass].radius;

                            for (int x = _x - radius; x <= _x + radius; x++)
                            {
                                for (int y = _y - radius; y <= _y + radius; y++)
                                {
                                    bool found = false;

                                    int tileX = it.tileX;
                                    int tileY = it.tileY;

                                    int x_ = x;
                                    int y_ = y;

                                    if (x >= 0 && x < 256 && y > 0 && y < 256)
                                    {
                                        found = true;
                                    }
                                    else if (x < 0 && it.tileX > 0)
                                    {
                                        tileX -= 1;
                                        x_    += 256;
                                        if (y < 0 && it.tileY > 0)
                                        {
                                            tileY -= 1;
                                            y_    += 256;
                                        }
                                        else if (y >= 256 && it.tileY < g.height - 1)
                                        {
                                            tileY += 1;
                                            y_    -= 256;
                                        }
                                        else
                                        {
                                            continue;
                                        }
                                    }
                                    else if (x >= 256 && it.tileX < g.width - 1)
                                    {
                                        tileX += 1;
                                        x_    -= 256;
                                        if (y < 0 && it.tileY > 0)
                                        {
                                            tileY -= 1;
                                            y_    += 256;
                                        }
                                        else if (y >= 256 && it.tileY < g.height - 1)
                                        {
                                            tileY += 1;
                                            y_    -= 256;
                                        }
                                        else
                                        {
                                            continue;
                                        }
                                    }
                                    else if (x >= 0 && x < 256)
                                    {
                                        if (y < 0 && it.tileY > 0)
                                        {
                                            tileY -= 1;
                                            y_    += 256;
                                        }
                                        else if (y >= 255 && it.tileY < g.height - 1)
                                        {
                                            tileY += 1;
                                            y_    -= 256;
                                        }
                                        else
                                        {
                                            continue;
                                        }
                                    }
                                    else
                                    {
                                        continue;
                                    }

                                    for (int i = 0; i < counters.Length; i++)
                                    {
                                        string id;
                                        if (found)
                                        {
                                            id = it.id;
                                        }
                                        else
                                        {
                                            id = g.tiles[tileX, tileY].id;
                                        }
                                        if (MainWindow.classesList[i].classifiedPointsList.ContainsKey(id))
                                        {
                                            if (MainWindow.classesList[i].classifiedPointsList[id][y_, x_] != null)
                                            {
                                                counters[i]++;
                                                if (maxValue < counters[i])
                                                {
                                                    maxValue = counters[i];
                                                    maxClass = i;
                                                }
                                                break;
                                            }
                                        }
                                        else
                                        {
                                            MainWindow.Log("ERROR with Image (" + it.worldX + " " + it.worldY + ") while postprocessing: Image with id " + id + " is not present in classified points. Trying to continue execution without it.");
                                        }
                                    }
                                }
                            }

                            if (counters[centralClass] < MainWindow.classesList[centralClass].threshold)
                            {
                                MainWindow.classesList[centralClass].classifiedPointsList[it.id][_y, _x] = null;
                                MainWindow.classesList[maxClass].classifiedPointsList[it.id][_y, _x]     = (FeaturePoint.GetOrAddFeaturePoint(_x, _y, it.id));
                            }
                        }
                    }
                }

                MainWindow.dispatcher.Invoke(() =>
                {
                    foreach (Classes c in MainWindow.classesList)
                    {
                        for (int i = 0; i < 256; i++)
                        {
                            for (int j = 0; j < 256; j++)
                            {
                                FeaturePoint fp = c.classifiedPointsList[it.id][i, j];
                                if (fp != null)
                                {
                                    ImageHelper.DrawOverlayClass(it.id, fp.x, fp.y, c, c.classColor);
                                }
                            }
                        }
                    }

                    // BluringEdges_4(MainWindow.Instance, it.id, blurRadius);
                });

                postProcessed[it.tileX, it.tileY] = true;

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

                averagePostprocessTime = (averagePostprocessTime * postprocessgDone + t) / (postprocessgDone + 1);
                postprocessgDone++;
                ReportProgress(sender);
                postprocessQueue--;

                MainWindow.Log("Image (" + it.worldX + " " + it.worldY + ") postprocessing done in " + t + "s. Now waiting for output and dispose");

                postprocessDoneQueue.Enqueue(id_);

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

                GC.Collect();
            }
            catch (Exception e)
            {
                MainWindow.Log("ERROR when postprocessing Image (" + it.worldX + " " + it.worldY + "): " + e.Message);

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

                if (postProcessed[it.tileX, it.tileY])
                {
                    postprocessQueue++; postprocessgDone--;
                }
                postProcessed[it.tileX, it.tileY] = false;

                it.Unlock();

                ErrorPostProcessingQueue.Enqueue(id_);

                GC.Collect();
            }
        }