2025-11-16 19:33:01 +08:00

137 lines
5.2 KiB
C#

//using Aspose.Svg.ImageVectorization;
//using ImageMagick;
//using OpenCvSharp;
//using SkiaSharp;
//using SvgNet;
//using SvgNet.Elements;
//using SvgNet.Types;
//using System;
//using System.Collections.Generic;
//using System.Drawing;
//using System.Linq;
//using System.Text;
//using System.Threading.Tasks;
//using Point = OpenCvSharp.Point;
//using SixLabors.ImageSharp;
//using SixLabors.ImageSharp.Processing;
//namespace XPrint.Image.Tools
//{
// public class SvgTool
// {
// //public static void ImageToSvg()
// //{
// // // 读取彩色图像
// // Mat src = Cv2.ImRead("input.png");
// // Mat hsv = new Mat();
// // Cv2.CvtColor(src, hsv, ColorConversionCodes.BGR2HSV);
// // // K-means 聚类(分为原图颜色数量)
// // int clusterCount = 5; // 根据原图颜色复杂度调整
// // Mat labels = new Mat();
// // Mat centers = new Mat();
// // TermCriteria criteria = new TermCriteria(CriteriaTypes.Count, 10, 1.0);
// // Cv2.Kmeans(hsv, clusterCount, labels, criteria, 3, KMeansFlags.PpCenters, centers);
// // // 生成颜色区域掩码
// // Mat mask = new Mat();
// // for (int i = 0; i < clusterCount; i++)
// // {
// // Mat mask_i = new Mat();
// // Cv2.Compare(labels, i, mask_i, CmpType.EQ);
// // mask += mask_i;
// // }
// // // 创建 SVG 文档
// // SvgDocument svg = new SvgDocument();
// // svg.Width = src.Width;
// // svg.Height = src.Height;
// // // 遍历每个颜色区域
// // for (int i = 0; i < clusterCount; i++)
// // {
// // // 提取当前颜色区域的轮廓
// // Mat mask_i = new Mat();
// // Cv2.Compare(labels, i, mask_i, CmpType.EQ);
// // Point[][] contours;
// // HierarchyIndex[] hierarchy;
// // Cv2.FindContours(mask_i, out contours, out hierarchy, RetrievalModes.External, ContourApproximationModes.ApproxNone);
// // SvgPath? path = null;
// // // 生成路径并设置颜色
// // foreach (var contour in contours)
// // {
// // if (contour.Length < 3) continue; // 跳过无效轮廓
// // string pathData = "M ";
// // for (int j = 0; j < contour.Length; j++)
// // {
// // Point point = contour[j];
// // pathData += $"{point.X} {point.Y} ";
// // if (j < contour.Length - 1)
// // {
// // pathData += "L ";
// // }
// // }
// // pathData += "Z ";
// // path = new SvgPath(pathData);
// // }
// // if (path != null)
// // {
// // path.Fill = new SvgColourServer(new Color(centers.At<float[]>(i, 0),
// // centers.At<float[]>(i, 1),
// // centers.At<float[]>(i, 2)));
// // }
// // svg.Children.Add(path);
// // }
// // // 导出 SVG
// // svg.Write("output.svg");
// //}
// public static void ImageToSvg()
// {
// //// 加载并降噪
// //using var image = SixLabors.ImageSharp.Image.Load(@"F:\vtracer\originpic\2.jpg");
// //image.Mutate(ctx => ctx
// // .Resize(image.Width, image.Height)
// // .GaussianBlur(1) // 高斯模糊平滑噪点
// //);
// // Initialize an instance of the ImageVectorizer class
// var vectorizer = new ImageVectorizer
// {
// // Optionally set configuration
// Configuration =
// {
// // Optionally set path builder
// PathBuilder = new SplinePathBuilder()
// {
// TraceSmoother = new ImageTraceSmoother(0),
// TraceSimplifier = new ImageTraceSimplifier(0.001f),
// },
// Stencil = new StencilConfiguration
// {
// Type = StencilType.None
// },
// ColorsLimit = 25,
// //LineWidth = 1f
// }
// };
// //using(var mem = new MemoryStream())
// //{
// // await image.SaveAsPngAsync(mem);
// using (var svgDoc = vectorizer.Vectorize(@"F:\vtracer\originpic\test\123123.jpg"))
// {
// //svgDoc.RootElement.SetAttribute("shape-rendering", "crispEdges"); // 强制像素对齐[4](@ref)svgDoc.Root.SetAttribute("preserveAspectRatio", "none"); // 消除缩放变形
// svgDoc.Save("F:\\vtracer\\originpic\\123123.svg", Aspose.Svg.Saving.SVGSaveFormat.SVG);
// }
// //}
// }
// }
//}