векторы изображений в SQL Spatial с использованием OpenCV - PullRequest
0 голосов
/ 04 августа 2020

Я работаю над изменением существующего приложения C#. NET, чтобы исправить некоторые проблемы. Идея состоит в том, чтобы это приложение считывало файл kml и файл png наложения растрового изображения. Затем создайте SQL Пространственные результаты и вставьте их в базу данных. Он делает все это, однако также оставляет пиксель сигнала между цветами видимости в результатах SQL Spatial. Вот основная часть кода, которая:

HashSet<Vec3b>.Enumerator colorIt = colors.GetEnumerator();

while (colorIt.MoveNext())
{
    Vec3b color = colorIt.Current;
    bool isCenterColor = colorOfTheCenter == color;
    //get color as hex (remember it is BGR)
    string colorHex = string.Format("{0:X2}{1:X2}{2:X2}", color.Item2, color.Item1, color.Item0);
    WriteOut("\nProcessing shape of color: #" + colorHex);
    WriteOut("Extracting shape");
    Cv2.InRange(inputImage,
                new Scalar(color.Item0, color.Item1, color.Item2, 0),
                new Scalar(color.Item0 + 1, color.Item1 + 1, color.Item2 + 1, 255),
                filteredColorMatMask);

       Cv2.FindContours(filteredColorMatMask,
                        out Point[][] contours,
                        out HierarchyIndex[] hierarchyIndexes,
                        mode: RetrievalModes.Tree,
                        method: ContourApproximationModes.ApproxNone);

      Cv2.DrawContours(filteredColorMatMask, 
                       contours, 
                       0, 
                       new Scalar(255, 255, 255), 
                       0, 
                       LineTypes.Link4, 
                       hierarchyIndexes);

      if (contours.Length == 0)
             throw new NotSupportedException("Couldn't find any shape in the image.");

      //center color's detected contours need to be grown by 1px
      if (isCenterColor)
      {
           Mat contoursMat = new Mat(new Size(inputImage.Width, inputImage.Height), MatType.CV_16U);
           //fill the contours
           Cv2.DrawContours(contoursMat, contours, 0, new Scalar(255, 255, 255),0, LineTypes.Link4, hierarchyIndexes);
          //draw the contour borders 2px thick (so that we have +1px everywhere)
          //Cv2.DrawContours(contoursMat, contours, 0, new Scalar(255, 0, 0), 0, LineTypes.Link4, hierarchyIndexes);
         //Cv2.ImWrite("result_conours_preview.png", contoursMat);
         //FIXME?: this fix is inaccurate when dealing with for example 2 single pixels 1 pixel away from each other or regions touch
         Cv2.FindContours(contoursMat,
                          out Point[][] contours1,
                          out HierarchyIndex[] hierarchyIndexes1,
                          mode: RetrievalModes.Tree,
                          method: ContourApproximationModes.ApproxNone);
         contoursMat.Dispose();
         if (contours1.Length == 0)
         {
             throw new ApplicationException("Application encountered a problem while processing the image: couldn't find contours in the derived image.");
         }
         contours = contours1;
         hierarchyIndexes = hierarchyIndexes1;
      }
      WriteOut("Generating WKT");
      string wktOutputmid = ContoursToWKT(boundingRectangle, dXYDeg, contours, hierarchyIndexes, "mid", true);
      WriteOut("Saving to database (as geometry)");
      File.WriteAllText("wkt_output.txt", wktOutputmid);
      SaveToDatabase(dbConnectionString, appOptions.accountId, appOptions.coverageId, DateTime.UtcNow, colorHex, isCenterColor, wktOutputmid);

Увеличено SQL Пространственные результаты

Вот исходное изображение viewshed.png

SQL Пространственные результаты

Мне нужно, чтобы эти два цвета были в SQL Пространственно, но согласовывались друг с другом, как в исходный файл png.

Спасибо, что нашли время, чтобы прочитать большую часть сообщения, и спасибо за любую помощь, которую я могу помочь в решении этой проблемы.

...