ToB企服应用市场:ToB评测及商务社交产业平台

标题: C#环境下实现Labelme标注数据可视化 [打印本页]

作者: 用户云卷云舒    时间: 前天 03:37
标题: C#环境下实现Labelme标注数据可视化
  1. using Newtonsoft.Json.Linq;
  2. using OpenCvSharp;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.IO;
  6. using System.Linq;
  7. class Program
  8. {
  9.     public static void JToken2InputArray(JToken shapeData, out Mat matOfPoints, out Point[] points)
  10.     {
  11.         int[][] floatArray = shapeData.ToObject<int[][]>();
  12.         int rows = floatArray.Length;
  13.         int cols = floatArray[0].Length;
  14.         points = new Point[floatArray.Length];
  15.         for (int i = 0; i < floatArray.Length; i++)
  16.         {
  17.             if (floatArray[i].Length >= 2)
  18.             {
  19.                 points[i] = new Point(floatArray[i][0], floatArray[i][1]);
  20.             }
  21.             else
  22.             {
  23.                 Console.WriteLine($"第 {i + 1} 个数组元素不足2个,无法转换为Point");
  24.             }
  25.         }
  26.         Mat mat = new Mat(rows, cols, MatType.CV_8UC1);
  27.         for (int i = 0; i < rows; i++)
  28.         {
  29.             for (int j = 0; j < cols; j++)
  30.             {
  31.                 mat.Set(i, j, floatArray[i][j]);
  32.             }
  33.         }
  34.         InputArray Points = InputArray.Create(mat);
  35.         matOfPoints = new Mat(points.Length, 1, MatType.CV_32SC2, points);
  36.     }
  37.     static void Main()
  38.     {
  39.         string imgPath = "bus.jpg";
  40.         string labelmePath = "bus.json";
  41.         Mat img = Cv2.ImRead(imgPath);
  42.         int width = img.Width;
  43.         int height = img.Height;
  44.         Mat mask = new Mat(height, width, MatType.CV_8UC3, new Scalar(0, 0, 0));
  45.         string jsonText = File.ReadAllText(labelmePath);
  46.         JObject labelme = JObject.Parse(jsonText);
  47.         Scalar bboxColor = new Scalar(255, 129, 0);
  48.         int bboxThickness = 5;
  49.         foreach (var shape in labelme["shapes"])
  50.         {
  51.             string shapeType = (string)shape["shape_type"];
  52.             if (shapeType == "rectangle")
  53.             {
  54.                 string label = (string)shape["label"];
  55.                 var points = shape["points"].ToObject<double[][]>();
  56.                 Point pt1 = new Point(points[0][0], points[0][1]);
  57.                 Point pt2 = new Point(points[1][0], points[1][1]);
  58.                 Cv2.Rectangle(img, pt1, pt2, bboxColor, bboxThickness);
  59.                 Cv2.PutText(img, label, new Point(pt1.X, pt1.Y - 10), HersheyFonts.HersheySimplex, 1, bboxColor, 2);
  60.             }
  61.         }
  62.         var kptColorMap = new Dictionary<string, Scalar>
  63.         {
  64.             { "angle_30", new Scalar(255, 0, 0) },
  65.             { "angle_60", new Scalar(0, 255, 0) },
  66.             { "angle_90", new Scalar(0, 0, 255) }
  67.         };
  68.         foreach (var shape in labelme["shapes"])
  69.         {
  70.             string shapeType = (string)shape["shape_type"];
  71.             if (shapeType == "point")
  72.             {
  73.                 string label = (string)shape["label"];
  74.                 var point = shape["points"].ToObject<double[]>();
  75.                 Point pt = new Point(point[0], point[1]);
  76.                 Scalar color = kptColorMap[label];
  77.                 Cv2.Circle(img, pt, 30, color, -1);
  78.                 Cv2.PutText(img, label, new Point(pt.X + 30, pt.Y + 30), HersheyFonts.HersheySimplex, 0.8, color, 2);
  79.             }
  80.         }
  81.         Scalar polyColor = new Scalar(151, 57, 224);
  82.         foreach (var shape in labelme["shapes"])
  83.         {
  84.             string shapeType = (string)shape["shape_type"];
  85.             if (shapeType == "polygon")
  86.             {
  87.                 string label = (string)shape["label"];
  88.                 JToken pointinput = shape["points"];
  89.                 int[][] floatArray = shape["points"].ToObject<int[][]>();
  90.                 JToken2InputArray(pointinput, out Mat matOfPoints, out Point[] points);
  91.                 Cv2.Polylines(img, matOfPoints, true, polyColor, 3);
  92.                 Cv2.FillPoly(mask, matOfPoints, polyColor);
  93.                 Point center = new Point(points.Average(p => p.X), points.Average(p => p.Y));
  94.                 Cv2.PutText(img, label, new Point(center.X - 100, center.Y), HersheyFonts.HersheySimplex, 1, polyColor, 4);
  95.             }
  96.         }
  97.         Cv2.AddWeighted(img, 0.7, mask, 0.3, 0, img);
  98.         Cv2.ImWrite("visualize.jpg", img);
  99.         Cv2.ImShow("Image", img);
  100.         Cv2.WaitKey(0);
  101.         Cv2.DestroyAllWindows();
  102.     }
  103. }
复制代码
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。




欢迎光临 ToB企服应用市场:ToB评测及商务社交产业平台 (https://dis.qidao123.com/) Powered by Discuz! X3.4