- using Newtonsoft.Json.Linq;
- using OpenCvSharp;
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- class Program
- {
- public static void JToken2InputArray(JToken shapeData, out Mat matOfPoints, out Point[] points)
- {
- int[][] floatArray = shapeData.ToObject<int[][]>();
- int rows = floatArray.Length;
- int cols = floatArray[0].Length;
- points = new Point[floatArray.Length];
- for (int i = 0; i < floatArray.Length; i++)
- {
- if (floatArray[i].Length >= 2)
- {
- points[i] = new Point(floatArray[i][0], floatArray[i][1]);
- }
- else
- {
- Console.WriteLine($"第 {i + 1} 个数组元素不足2个,无法转换为Point");
- }
- }
- Mat mat = new Mat(rows, cols, MatType.CV_8UC1);
- for (int i = 0; i < rows; i++)
- {
- for (int j = 0; j < cols; j++)
- {
- mat.Set(i, j, floatArray[i][j]);
- }
- }
- InputArray Points = InputArray.Create(mat);
- matOfPoints = new Mat(points.Length, 1, MatType.CV_32SC2, points);
- }
- static void Main()
- {
- string imgPath = "bus.jpg";
- string labelmePath = "bus.json";
- Mat img = Cv2.ImRead(imgPath);
- int width = img.Width;
- int height = img.Height;
- Mat mask = new Mat(height, width, MatType.CV_8UC3, new Scalar(0, 0, 0));
- string jsonText = File.ReadAllText(labelmePath);
- JObject labelme = JObject.Parse(jsonText);
- Scalar bboxColor = new Scalar(255, 129, 0);
- int bboxThickness = 5;
- foreach (var shape in labelme["shapes"])
- {
- string shapeType = (string)shape["shape_type"];
- if (shapeType == "rectangle")
- {
- string label = (string)shape["label"];
- var points = shape["points"].ToObject<double[][]>();
- Point pt1 = new Point(points[0][0], points[0][1]);
- Point pt2 = new Point(points[1][0], points[1][1]);
- Cv2.Rectangle(img, pt1, pt2, bboxColor, bboxThickness);
- Cv2.PutText(img, label, new Point(pt1.X, pt1.Y - 10), HersheyFonts.HersheySimplex, 1, bboxColor, 2);
- }
- }
- var kptColorMap = new Dictionary<string, Scalar>
- {
- { "angle_30", new Scalar(255, 0, 0) },
- { "angle_60", new Scalar(0, 255, 0) },
- { "angle_90", new Scalar(0, 0, 255) }
- };
- foreach (var shape in labelme["shapes"])
- {
- string shapeType = (string)shape["shape_type"];
- if (shapeType == "point")
- {
- string label = (string)shape["label"];
- var point = shape["points"].ToObject<double[]>();
- Point pt = new Point(point[0], point[1]);
- Scalar color = kptColorMap[label];
- Cv2.Circle(img, pt, 30, color, -1);
- Cv2.PutText(img, label, new Point(pt.X + 30, pt.Y + 30), HersheyFonts.HersheySimplex, 0.8, color, 2);
- }
- }
- Scalar polyColor = new Scalar(151, 57, 224);
- foreach (var shape in labelme["shapes"])
- {
- string shapeType = (string)shape["shape_type"];
- if (shapeType == "polygon")
- {
- string label = (string)shape["label"];
- JToken pointinput = shape["points"];
- int[][] floatArray = shape["points"].ToObject<int[][]>();
- JToken2InputArray(pointinput, out Mat matOfPoints, out Point[] points);
- Cv2.Polylines(img, matOfPoints, true, polyColor, 3);
- Cv2.FillPoly(mask, matOfPoints, polyColor);
- Point center = new Point(points.Average(p => p.X), points.Average(p => p.Y));
- Cv2.PutText(img, label, new Point(center.X - 100, center.Y), HersheyFonts.HersheySimplex, 1, polyColor, 4);
- }
- }
- Cv2.AddWeighted(img, 0.7, mask, 0.3, 0, img);
- Cv2.ImWrite("visualize.jpg", img);
- Cv2.ImShow("Image", img);
- Cv2.WaitKey(0);
- Cv2.DestroyAllWindows();
- }
- }
复制代码 免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。 |