PlaquePostProcessHelper.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  1. using AI.Reconstruction;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Drawing;
  5. using System.Linq;
  6. using System.Runtime.InteropServices;
  7. using AI.DiagSystem;
  8. namespace Reconstruction3DHelper
  9. {
  10. [StructLayout(LayoutKind.Sequential)]
  11. public struct PlaqueProperty
  12. {
  13. public int SerialNumber;
  14. public Rectangle ImageRect;
  15. public Point[] ArteryContour;
  16. public Point[] PlaqueContour;
  17. public double PlaqueArea;
  18. //狭窄率
  19. public float Ratio;
  20. //在原图中的斑块外界矩形
  21. public RotatedRectangle RealRect;
  22. //在原图中的斑块中心
  23. public Point RealCenterPoint;
  24. public float Credibility;
  25. }
  26. [StructLayout(LayoutKind.Sequential)]
  27. public struct RotatedRectangle
  28. {
  29. public float Angle;
  30. public float Cx;
  31. public float Cy;
  32. public float Height;
  33. public float Width;
  34. }
  35. /// <summary>
  36. /// 图像信息
  37. /// </summary>
  38. public struct StructImageInfo
  39. {
  40. private int _width;
  41. private int _height;
  42. private AI.Common.EnumColorType _colorType;
  43. private IntPtr _dataPointer;
  44. public int Width
  45. {
  46. get => _width;
  47. set => _width = value;
  48. }
  49. public int Height
  50. {
  51. get => _height;
  52. set => _height = value;
  53. }
  54. public AI.Common.EnumColorType ColorType
  55. {
  56. get => _colorType;
  57. set => _colorType = value;
  58. }
  59. public IntPtr DataPointer
  60. {
  61. get => _dataPointer;
  62. set => _dataPointer = value;
  63. }
  64. public StructImageInfo(int width, int height, AI.Common.EnumColorType colorType, IntPtr dataPointer)
  65. {
  66. _width = width;
  67. _height = height;
  68. _colorType = colorType;
  69. _dataPointer = dataPointer;
  70. }
  71. }
  72. public enum PlaqueType
  73. {
  74. SoftPlaque,
  75. HardPlaque
  76. }
  77. public enum PlaquePostionType
  78. {
  79. UpperIntima,
  80. LowerIntima
  81. }
  82. public enum PlaqueCountType
  83. {
  84. NoPlaque,
  85. OnePlaque,
  86. MultiPlaque
  87. }
  88. public class PlaqueResult
  89. {
  90. /// <summary>
  91. /// 斑块数量 无、单发、多发
  92. /// </summary>
  93. public PlaqueCountType PlaqueCountType { get; }
  94. /// <summary>
  95. /// 斑块特性 软斑、硬斑
  96. /// </summary>
  97. public PlaqueType PlaqueType { get; }
  98. /// <summary>
  99. /// 最大斑块宽度
  100. /// </summary>
  101. public float PlaqueWidth { get; }
  102. /// <summary>
  103. /// 最大斑块高度
  104. /// </summary>
  105. public float PlaqueHeight { get; }
  106. /// <summary>
  107. /// 狭窄率
  108. /// </summary>
  109. public float Stenosis { get; }
  110. /// <summary>
  111. /// 斑块位置,上内膜/下内膜
  112. /// </summary>
  113. public PlaquePostionType PlaquePostion;
  114. /// <summary>
  115. /// 检测切面的三维点
  116. /// </summary>
  117. public List<Point3DF> ClipPoints { get; }
  118. /// <summary>
  119. /// 是否识别成功
  120. /// </summary>
  121. public bool IsSuccess { get; }
  122. /// <summary>
  123. /// 斑块的轮廓
  124. /// </summary>
  125. public Point[] PlaqueContour { get; }
  126. public PlaqueResult() { }
  127. public PlaqueResult(bool isSuccess)
  128. {
  129. IsSuccess = isSuccess;
  130. }
  131. public PlaqueResult(bool isSuccess, PlaqueCountType plaqueCountType)
  132. {
  133. IsSuccess = isSuccess;
  134. PlaqueCountType = plaqueCountType;
  135. }
  136. public PlaqueResult(bool isSuccess, PlaqueCountType plaqueCountType, PlaqueType plaqueType, float plaqueWidth,
  137. float plaqueHeight, float stenosis, List<Point3DF> clipPoints, Point[] plaqueContour)
  138. {
  139. IsSuccess = isSuccess;
  140. PlaqueCountType = plaqueCountType;
  141. PlaqueType = plaqueType;
  142. PlaqueWidth = plaqueWidth;
  143. PlaqueHeight = plaqueHeight;
  144. Stenosis = stenosis;
  145. ClipPoints = clipPoints;
  146. PlaqueContour = plaqueContour;
  147. }
  148. }
  149. public class PlaquePostProcessHelper
  150. {
  151. public ModelSize _modelSize;
  152. public string _plaqueImagePath;
  153. [DllImport(@"PlaqueProcessingCpp.dll", CallingConvention = CallingConvention.Cdecl)]
  154. public static extern bool PointInPolygon(IntPtr contours, int pointCount, IntPtr point);
  155. [DllImport(@"PlaqueProcessingCpp.dll", CallingConvention = CallingConvention.Cdecl)]
  156. public static extern bool CalcMaxPlaque(IntPtr arteryContour, IntPtr plaqueContour,
  157. int arteryCount, int plaqueCount, int left, int top, ref float ratio, ref RotatedRectangle RealRect);
  158. [DllImport(@"PlaqueProcessingCpp.dll", CallingConvention = CallingConvention.Cdecl)]
  159. public static extern bool IsBrightPlaque(StructImageInfo srcImgInfo, IntPtr contours, int pointCount);
  160. /// <summary>
  161. /// 斑块自动检测
  162. /// </summary>
  163. /// <param name="image">颈动脉图像</param>
  164. /// <param name="physicalPerPixel">每个像素代表实际长度</param>
  165. /// <returns>斑块位置</returns>
  166. public PlaqueResult CalcPlaqueProperty(byte[][] model, ModelSize modelSize, float physicalPerPixel, Dictionary<int, AIDiagResultPerImg> vesselsAndPlaques)
  167. {
  168. _modelSize = modelSize;
  169. var allPlaque = GetAllPlaqueContours(vesselsAndPlaques);
  170. if (allPlaque.Count == 0)
  171. {
  172. return new PlaqueResult(false);
  173. }
  174. var maxPlaque = GetMaxPlaque(allPlaque);
  175. if (maxPlaque.RealRect.Width < 15 || maxPlaque.RealRect.Height < 15)
  176. {
  177. var detectPlaqueResult1 = new PlaqueResult(true, PlaqueCountType.NoPlaque, PlaqueType.SoftPlaque,
  178. 0, 0, 0, new List<Point3DF> { }, new Point[] { });
  179. return detectPlaqueResult1;
  180. }
  181. var decodedPointer = Marshal.UnsafeAddrOfPinnedArrayElement(model[maxPlaque.SerialNumber], 0);
  182. StructImageInfo imageInfo = new StructImageInfo(_modelSize.ModelLengthX, _modelSize.ModelLengthY, AI.Common.EnumColorType.Gray8, decodedPointer);
  183. GCHandle hObject1 = GCHandle.Alloc(maxPlaque.PlaqueContour, GCHandleType.Pinned);
  184. IntPtr plaqueContour = hObject1.AddrOfPinnedObject();
  185. var isBright = IsBrightPlaque(imageInfo, plaqueContour, maxPlaque.PlaqueContour.Length);
  186. List<Point3DF> clipPoints = new List<Point3DF> {new Point3DF(0, _modelSize.ModelLengthX, maxPlaque.SerialNumber),
  187. new Point3DF(_modelSize.ModelLengthY, 0, maxPlaque.SerialNumber),
  188. new Point3DF(0, 0, maxPlaque.SerialNumber)};
  189. //计算斑块的参数
  190. var width = maxPlaque.RealRect.Width * physicalPerPixel;
  191. var height = maxPlaque.RealRect.Height * physicalPerPixel;
  192. width = (float)Math.Round(width, 2, MidpointRounding.AwayFromZero);
  193. height = (float)Math.Round(height, 2, MidpointRounding.AwayFromZero);
  194. var plaqueResult = new PlaqueResult(true,
  195. PlaqueCountType.MultiPlaque,
  196. isBright ? PlaqueType.HardPlaque : PlaqueType.SoftPlaque,
  197. width,
  198. height,
  199. maxPlaque.Ratio,
  200. clipPoints,
  201. maxPlaque.PlaqueContour);
  202. return plaqueResult;
  203. }
  204. /// <summary>
  205. /// 判断斑块是否在血管内
  206. /// </summary>
  207. /// <param name="allPlaque"></param>
  208. /// <returns></returns>
  209. private List<PlaqueProperty> GetPlaqueInArtery(IList<PlaqueProperty> allPlaque)
  210. {
  211. var plaqueList = new List<PlaqueProperty>();
  212. foreach (PlaqueProperty plaque in allPlaque)
  213. {
  214. var count = plaque.PlaqueContour.Length;
  215. var num = 0;
  216. GCHandle hObject = GCHandle.Alloc(plaque.ArteryContour, GCHandleType.Pinned);
  217. IntPtr pObject = hObject.AddrOfPinnedObject();
  218. for (var i = 0; i < count; ++i)
  219. {
  220. GCHandle hObject2 = GCHandle.Alloc(plaque.PlaqueContour[i], GCHandleType.Pinned);
  221. IntPtr pObject2 = hObject2.AddrOfPinnedObject();
  222. if (PointInPolygon(pObject, plaque.ArteryContour.Length, pObject2))
  223. {
  224. num++;
  225. }
  226. }
  227. if (num > count * 3 / 4)
  228. {
  229. plaqueList.Add(plaque);
  230. }
  231. }
  232. return plaqueList;
  233. }
  234. /// <summary>
  235. /// 计算最大斑块的属性
  236. /// </summary>
  237. /// <param name="allPlaqueSrc"></param>
  238. /// <returns></returns>
  239. private PlaqueProperty GetMaxPlaque(IList<PlaqueProperty> allPlaqueSrc)
  240. {
  241. if (allPlaqueSrc.Count < 1)
  242. {
  243. return new PlaqueProperty();
  244. }
  245. var allPlaque = GetPlaqueInArtery(allPlaqueSrc); //判断斑块是否在血管内
  246. if (allPlaque.Count < 1)
  247. {
  248. return new PlaqueProperty();
  249. }
  250. allPlaque = allPlaque.OrderByDescending(o => o.PlaqueArea).ToList();
  251. var maxPlaque = allPlaque[0];
  252. //计算中心点
  253. var centerPoint = Get2DContoursCenterPoints(maxPlaque.PlaqueContour);
  254. //计算动脉轮廓上距离斑块中心最远的点
  255. GCHandle hObject1 = GCHandle.Alloc(maxPlaque.ArteryContour, GCHandleType.Pinned);
  256. IntPtr arteryIntPtr = hObject1.AddrOfPinnedObject();
  257. GCHandle hObject2 = GCHandle.Alloc(maxPlaque.PlaqueContour, GCHandleType.Pinned);
  258. IntPtr plaqueIntPtr = hObject2.AddrOfPinnedObject();
  259. float ratio = 0.0F;
  260. RotatedRectangle rotatedRectangle = new RotatedRectangle();
  261. var result = CalcMaxPlaque(arteryIntPtr, plaqueIntPtr, maxPlaque.ArteryContour.Length,
  262. maxPlaque.PlaqueContour.Length, maxPlaque.ImageRect.Left, maxPlaque.ImageRect.Top, ref ratio, ref rotatedRectangle);
  263. maxPlaque.RealRect = rotatedRectangle;
  264. //计算实际的中心点
  265. maxPlaque.RealCenterPoint = new Point(centerPoint.X + maxPlaque.ImageRect.Left, centerPoint.Y + maxPlaque.ImageRect.Top);
  266. maxPlaque.Ratio = ratio;
  267. return maxPlaque;
  268. }
  269. /// <summary>
  270. /// 计算轮廓的中心点
  271. /// </summary>
  272. /// <param name="contourPoints"></param>
  273. /// <returns></returns>
  274. private Point Get2DContoursCenterPoints(Point[] contourPoints)
  275. {
  276. var len = contourPoints.Length;
  277. var sumX = 0;
  278. var sumY = 0;
  279. for (var i = 0; i < len; ++i)
  280. {
  281. sumX += contourPoints[i].X;
  282. sumY += contourPoints[i].Y;
  283. }
  284. var centerX = sumX / (float)len;
  285. var centerY = sumY / (float)len;
  286. //加上离最远点最远的点
  287. return new Point((int)Math.Round(centerX, 0, MidpointRounding.AwayFromZero), (int)Math.Round(centerY, 0, MidpointRounding.AwayFromZero));
  288. }
  289. /// <summary>
  290. /// 获取AI预测结果中所有斑块的轮廓点、外接矩形等尺寸
  291. /// </summary>
  292. /// <param name="modelResults"></param>
  293. /// <returns></returns>
  294. private List<PlaqueProperty> GetAllPlaqueContours(Dictionary<int, AIDiagResultPerImg> modelResults)
  295. {
  296. List<PlaqueProperty> allPlaqueContours = new List<PlaqueProperty>();
  297. foreach (var modelResult in modelResults)
  298. {
  299. if (modelResult.Value.DiagResultsForEachOrgan[0].DetectedObjects.Length > 0)
  300. {
  301. if (modelResult.Value.DiagResultsForEachOrgan[0].DetectedObjects[0].Label == 0)
  302. {
  303. continue;
  304. }
  305. ///modelResults赋值给_allPlaqueContours
  306. var plaqueContours = new PlaqueProperty();
  307. AI.Common.Point2D[] contour1 = modelResult.Value.DiagResultsForEachOrgan[0].OrganContours[0];
  308. Point[] arteryPoints = new Point[contour1.Length];
  309. for (int i = 0; i < contour1.Length; i++)
  310. {
  311. arteryPoints[i].X = contour1[i].X;
  312. arteryPoints[i].Y = contour1[i].Y;
  313. }
  314. plaqueContours.ArteryContour = arteryPoints;
  315. AI.Common.Point2D[] contour2 = modelResult.Value.DiagResultsForEachOrgan[0].DetectedObjects[0].Contours[0];
  316. Point[] plaquePoints = new Point[contour2.Length];
  317. for (int i = 0; i < contour2.Length; i++)
  318. {
  319. plaquePoints[i].X = contour2[i].X;
  320. plaquePoints[i].Y = contour2[i].Y;
  321. }
  322. plaqueContours.PlaqueContour = plaquePoints;
  323. AI.Common.Rect rect2 = modelResult.Value.DiagResultsForEachOrgan[0].DetectedObjects[0].BoundingBox;
  324. plaqueContours.ImageRect = new Rectangle(rect2.Left, rect2.Top, rect2.Width, rect2.Height);
  325. plaqueContours.SerialNumber = modelResult.Key;
  326. plaqueContours.PlaqueArea = modelResult.Value.DiagResultsForEachOrgan[0].DetectedObjects[0].BoundingBox.Area;
  327. plaqueContours.Credibility = modelResult.Value.DiagResultsForEachOrgan[0].DetectedObjects[0].Confidence;
  328. allPlaqueContours.Add(plaqueContours);
  329. }
  330. }
  331. return allPlaqueContours;
  332. }
  333. }
  334. }