1
0

ImageUtility.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Drawing;
  7. using System.Windows.Media.Imaging;
  8. using System.Drawing.Imaging;
  9. using System.Runtime.InteropServices;
  10. namespace SegmentDescribDemo
  11. {
  12. /// <summary>
  13. /// 图像处理工具类
  14. /// </summary>
  15. [StructLayout(LayoutKind.Sequential)]
  16. public struct MyPoint
  17. {
  18. public int x;
  19. public int y;
  20. }
  21. /// <summary>
  22. /// 病灶轮廓的纵、横轴的坐标点
  23. /// </summary>
  24. public struct AxisPoint
  25. {
  26. public MyPoint horizontalPt1; // 病灶轮廓的 横轴(水平方向) 端点坐标
  27. public MyPoint horizontalPt2;
  28. public MyPoint verticalPt1; // 病灶轮廓的 纵轴 端点坐标
  29. public MyPoint verticalPt2;
  30. }
  31. public struct LesionDescri
  32. {
  33. public int shape; /// 形状描述。0未标注,1椭圆,2类圆,3不规则
  34. public int orientation; /// 生长方向。0未标注,1平行,2非平行
  35. public int echo; /// 回声类型。0未标注,1无回声,2低回声,3等回声,4高回声,5混合回声
  36. public int boundary; /// 边界清晰度。0未标注,1清晰,2不清晰
  37. public int margin; /// 边缘光整度。0未标注,1光整,不光整
  38. public int calcification; /// 钙化。0未标注,1无钙化,2粗大钙化,3内部微钙化,外部微钙化
  39. public AxisPoint axisPt;
  40. }
  41. public struct LesionDescriNow
  42. {
  43. public int shape; /// 形状描述。0未标注,1椭圆,2类圆,3不规则
  44. public int orientation; /// 生长方向。0未标注,1平行,2非平行
  45. public int echo; /// 回声类型。0未标注,1无回声,2低回声,3等回声,4高回声,5混合回声
  46. public int boundary; /// 边界清晰度。0未标注,1清晰,2不清晰
  47. public int margin; /// 边缘光整度。0未标注,1光整,不光整
  48. public int calcification; /// 钙化。0未标注,1无钙化,2粗大钙化,3内部微钙化,外部微钙化
  49. public int posteriorAcousticFeature; /// 后部声学特征 :
  50. }
  51. public static class ImageUtility
  52. {
  53. [DllImport(@"BreastLesionSeg.dll", CallingConvention = CallingConvention.Cdecl)]
  54. public static unsafe extern int ContourDetectBLS(byte[] img_data, int channels, int img_width, int img_height,
  55. roiCoor roi_vec, [In, Out] MyPoint[] contour_points);
  56. [DllImport(@"ActiveContourWithoutEdge_IO.dll", CallingConvention = CallingConvention.Cdecl)]
  57. public static unsafe extern int ContourDetectACWE(byte[] img_data, int channels, int img_width, int img_height,
  58. roiCoor roi_vec, [In, Out] MyPoint[] contour_points);
  59. [DllImport(@"ActiveContourDRLSE_IO.dll", EntryPoint = "ContourDetectDRLSE", CallingConvention = CallingConvention.Cdecl)]
  60. public static unsafe extern int ContourDetectDRLSE(byte[] img_data, int channels, int img_width, int img_height,
  61. roiCoor roi_vec, [In, Out] MyPoint[] contour_points);
  62. [DllImport(@"ActiveContourLocalBinaryFitting.dll", EntryPoint = "ContourDetectLBF", CallingConvention = CallingConvention.Cdecl)]
  63. public static unsafe extern int ContourDetectLBF(byte[] img_data, int channels, int img_width, int img_height,
  64. roiCoor roi_vec, [In, Out] MyPoint[] contour_points);
  65. [DllImport(@"LevelSetEvolutionWithoutReinitialization.dll", EntryPoint = "ContourDetectLSEWR", CallingConvention = CallingConvention.Cdecl)]
  66. public static unsafe extern int ContourDetectLSEWR(byte[] img_data, int channels, int img_width, int img_height,
  67. roiCoor roi_vec, [In, Out] MyPoint[] contour_points);
  68. [DllImport(@"FCM_IO.dll", CallingConvention = CallingConvention.Cdecl)]
  69. public static unsafe extern int ContourDetectFCM(byte[] img_data, int channels, float contrast_value, int img_width, int img_height,
  70. roiCoor roi_vec, [In, Out] MyPoint[] contour_points);
  71. [DllImport(@"LesionDescription.dll", CallingConvention = CallingConvention.Cdecl)]
  72. public static extern void LoadModel(char[] shapePath);
  73. [DllImport(@"LesionDescription.dll", CallingConvention = CallingConvention.Cdecl)]
  74. public static extern void BreastDescription(byte[] img_data, int channels, int img_w, int img_h, roiCoor roi_vec, int num_pts,
  75. MyPoint[] contour_points, AxisPoint axisPt, ref LesionDescriNow lesionDescr);
  76. [DllImport(@"LesionDescription.dll", CallingConvention = CallingConvention.Cdecl)]
  77. public static extern void FreeModel();
  78. public static BitmapImage BitmapToBitmapImage(Bitmap img)
  79. {
  80. BitmapImage bmpimg = new BitmapImage();
  81. using (System.IO.MemoryStream ms = new System.IO.MemoryStream())
  82. {
  83. img.Save(ms, ImageFormat.Png);
  84. bmpimg.BeginInit();
  85. bmpimg.StreamSource = ms;
  86. bmpimg.CacheOption = BitmapCacheOption.OnLoad;
  87. bmpimg.EndInit();
  88. bmpimg.Freeze();
  89. ms.Dispose();
  90. }
  91. return bmpimg;
  92. }
  93. public unsafe static PointF[] ContourDetect(SegDataInput data, EnumSegMethod segMethod)
  94. {
  95. if ((data.InputImage.PixelFormat != PixelFormat.Format24bppRgb) &&
  96. (data.InputImage.PixelFormat != PixelFormat.Format8bppIndexed) &&
  97. (data.InputImage.PixelFormat != PixelFormat.Format32bppRgb))
  98. {
  99. return new PointF[0];
  100. }
  101. if (data.InputRoi.Width * data.InputRoi.Height <= 0)
  102. {
  103. return new PointF[0];
  104. }
  105. BitmapData bmData;
  106. int imgWidth = data.InputImage.Width;
  107. int imgHeight = data.InputImage.Height;
  108. int channels = (data.InputImage.PixelFormat == PixelFormat.Format24bppRgb) ? 3 : 1;
  109. Rectangle rect = new Rectangle(0, 0, imgWidth, imgHeight);
  110. bmData = data.InputImage.LockBits(rect, ImageLockMode.ReadOnly, data.InputImage.PixelFormat);
  111. byte[] imgData = new byte[imgWidth * imgHeight * channels];
  112. var ptr0 = (byte*)bmData.Scan0.ToPointer();
  113. int stride = bmData.Stride;
  114. for (int y = 0; y < imgHeight; y++)
  115. {
  116. for (int x = 0; x < imgWidth; x++)
  117. {
  118. for (int c = 0; c < channels; c++) // 通道数
  119. {
  120. imgData[y * imgWidth * channels + x * channels + c] = ptr0[y * stride + x * channels + c];
  121. }
  122. }
  123. }
  124. int max_num = data.InputRoi.Height * data.InputRoi.Width;
  125. MyPoint[] contourPts_tmp = new MyPoint[max_num];
  126. roiCoor roi;
  127. roi.x = data.InputRoi.X;
  128. roi.y = data.InputRoi.Y;
  129. roi.width = data.InputRoi.Width;
  130. roi.height = data.InputRoi.Height;
  131. int ptsNum = 0;
  132. if (segMethod == EnumSegMethod.BLS)
  133. {
  134. ptsNum = ContourDetectBLS(imgData, channels, imgWidth, imgHeight, roi, contourPts_tmp);
  135. }
  136. if (segMethod == EnumSegMethod.FCM)
  137. {
  138. ptsNum = ContourDetectFCM(imgData, channels, 120, imgWidth, imgHeight, roi, contourPts_tmp);
  139. }
  140. if (segMethod == EnumSegMethod.ACWE)
  141. {
  142. ptsNum = ContourDetectACWE(imgData, channels, imgWidth, imgHeight, roi, contourPts_tmp);
  143. }
  144. if (segMethod == EnumSegMethod.DRLSE)
  145. {
  146. ptsNum = ContourDetectDRLSE(imgData, channels, imgWidth, imgHeight, roi, contourPts_tmp);
  147. }
  148. if (segMethod == EnumSegMethod.LBF)
  149. {
  150. ptsNum = ContourDetectLBF(imgData, channels, imgWidth, imgHeight, roi, contourPts_tmp);
  151. }
  152. if (segMethod == EnumSegMethod.LSEWR)
  153. {
  154. ptsNum = ContourDetectLSEWR(imgData, channels, imgWidth, imgHeight, roi, contourPts_tmp);
  155. }
  156. data.InputImage.UnlockBits(bmData);
  157. if (ptsNum <= 0)
  158. {
  159. return new PointF[0];
  160. }
  161. Point[] contourPts = new Point[ptsNum];
  162. for (int i = 0; i < ptsNum; i++)
  163. {
  164. contourPts[i].X = contourPts_tmp[i].x;
  165. contourPts[i].Y = contourPts_tmp[i].y;
  166. }
  167. PointF[] contour_points = new PointF[ptsNum];
  168. for (int i = 0; i < ptsNum; i++)
  169. {
  170. contour_points[i] = new PointF(contourPts[i].X, contourPts[i].Y);
  171. }
  172. return contour_points;
  173. }
  174. public unsafe static LesionDescriptionResult BreastDescription(DesDataInput data, AxisPoint axisPt)
  175. {
  176. System.Drawing.PointF[] contours = data.Contour;
  177. LesionDescriptionResult result = null;
  178. // 病灶轮廓描述
  179. int pts_num = contours.GetLength(0);
  180. int imgWidth = data.InputImage.Width;
  181. int imgHeight = data.InputImage.Height;
  182. MyPoint[] contourPts_tmp = new MyPoint[pts_num];
  183. for (int i = 0; i < pts_num; i++)
  184. {
  185. contourPts_tmp[i].x = (int)contours[i].X;
  186. contourPts_tmp[i].y = (int)contours[i].Y;
  187. }
  188. int channels = (data.InputImage.PixelFormat == System.Drawing.Imaging.PixelFormat.Format24bppRgb) ? 3 : 1;
  189. BitmapData bmData;
  190. System.Drawing.Rectangle rect = new System.Drawing.Rectangle(0, 0, imgWidth, imgHeight);
  191. bmData = data.InputImage.LockBits(rect, ImageLockMode.ReadOnly, data.InputImage.PixelFormat);
  192. byte[] img_data = new byte[imgWidth * imgHeight * channels];
  193. var ptr0 = (byte*)bmData.Scan0.ToPointer();
  194. int stride = bmData.Stride;
  195. for (int y = 0; y < imgHeight; y++)
  196. {
  197. for (int x = 0; x < imgWidth; x++)
  198. {
  199. for (int c = 0; c < channels; c++)
  200. {
  201. img_data[y * imgWidth * channels + x * channels + c] = ptr0[y * stride + x * channels + c];
  202. }
  203. }
  204. }
  205. roiCoor roi;
  206. roi.x = data.InputRoi.X;
  207. roi.y = data.InputRoi.Y;
  208. roi.width = data.InputRoi.Width;
  209. roi.height = data.InputRoi.Height;
  210. LesionDescriNow lesionDescr = new LesionDescriNow();
  211. lesionDescr.boundary = 0;
  212. lesionDescr.margin = 0;
  213. lesionDescr.echo = 0;
  214. lesionDescr.calcification = 0;
  215. lesionDescr.orientation = 0;
  216. AxisPoint axisPtNew = new AxisPoint();
  217. axisPtNew.horizontalPt1 = new MyPoint { x = 0, y = 0 };
  218. axisPtNew.horizontalPt2 = new MyPoint { x = 0, y = 0 };
  219. axisPtNew.verticalPt1 = new MyPoint { x = 0, y = 0 };
  220. axisPtNew.verticalPt2 = new MyPoint { x = 0, y = 0 };
  221. if( true/*axisPt*/ )
  222. {
  223. axisPtNew.horizontalPt1 = axisPt.horizontalPt1;
  224. axisPtNew.horizontalPt2 = axisPt.horizontalPt2;
  225. axisPtNew.verticalPt1 = axisPt.verticalPt1;
  226. axisPtNew.verticalPt2 = axisPt.verticalPt2;
  227. }
  228. int startTime2 = Environment.TickCount;
  229. BreastDescription(img_data, channels, imgWidth, imgHeight, roi,
  230. pts_num, contourPts_tmp, axisPtNew, ref lesionDescr);
  231. int endTime2 = Environment.TickCount;
  232. Console.WriteLine("TestTime:" + (endTime2 - startTime2) + "ms");
  233. result = new LesionDescriptionResult(lesionDescr.shape,
  234. lesionDescr.orientation,
  235. lesionDescr.echo,
  236. lesionDescr.boundary,
  237. lesionDescr.margin,
  238. lesionDescr.calcification,
  239. lesionDescr.posteriorAcousticFeature
  240. );
  241. return result;
  242. }
  243. public static void Init()
  244. {
  245. string shapePath = "../TrainModel/shape66.xml";
  246. int startTime1 = Environment.TickCount;
  247. LoadModel(shapePath.ToCharArray());
  248. int endTime1 = Environment.TickCount;
  249. Console.WriteLine("LoadTime:" + (endTime1 - startTime1) + "ms");
  250. }
  251. public static void Dispose()
  252. {
  253. // DisposeModel
  254. //FreeModel();
  255. }
  256. }
  257. /// <summary>
  258. /// 分割选用的方法
  259. /// </summary>
  260. public enum EnumSegMethod
  261. {
  262. FCM,
  263. ACWE,
  264. DRLSE,
  265. LBF,
  266. LSEWR,
  267. BLS, //最新的分割算法
  268. }
  269. /// <summary>
  270. /// 分割的输入数据
  271. /// </summary>
  272. public class SegDataInput
  273. {
  274. /// <summary>
  275. /// 输入图像
  276. /// </summary>
  277. public Bitmap InputImage { get; set; } = null;
  278. /// <summary>
  279. /// 感兴趣区域
  280. /// </summary>
  281. public Rectangle InputRoi { get; set; } = Rectangle.Empty;
  282. /// <summary>
  283. /// 构造函数
  284. /// </summary>
  285. /// <param name="img"></param>
  286. /// <param name="rect"></param>
  287. public SegDataInput(Bitmap img, Rectangle rect)
  288. {
  289. InputImage = (Bitmap)img.Clone();
  290. InputRoi = rect;
  291. }
  292. public void Dispose()
  293. {
  294. InputImage?.Dispose();
  295. }
  296. }
  297. /// <summary>
  298. /// 描述的输入数据
  299. /// </summary>
  300. public class DesDataInput
  301. {
  302. /// <summary>
  303. /// 输入图像
  304. /// </summary>
  305. public Bitmap InputImage { get; set; } = null;
  306. /// <summary>
  307. /// 感兴趣区域
  308. /// </summary>
  309. public Rectangle InputRoi { get; set; } = Rectangle.Empty;
  310. /// <summary>
  311. /// 病灶轮廓
  312. /// </summary>
  313. public PointF[] Contour { get; set; } = null;
  314. public DesDataInput(Bitmap img, Rectangle rect, PointF[] contour)
  315. {
  316. InputImage = (Bitmap)img.Clone();
  317. InputRoi = rect;
  318. Contour = contour;
  319. }
  320. public void Dispose()
  321. {
  322. InputImage?.Dispose();
  323. }
  324. }
  325. /// <summary>
  326. /// 病灶描述结果
  327. /// </summary>
  328. public class LesionDescriptionResult
  329. {
  330. public LesionDescriptionResult(int shape, int orientation, int echo,
  331. int boundary, int margin, int calcification, int posteriorAcousticFeature)
  332. {
  333. Shape = shape;
  334. Orientation = orientation;
  335. Echo = echo;
  336. Boundary = boundary;
  337. Margin = margin;
  338. Calcification = calcification;
  339. PosteriorAcousticFeature = posteriorAcousticFeature;
  340. }
  341. public bool Succeed { get; private set; }
  342. public int Shape { get; private set; }
  343. public int Orientation { get; private set; }
  344. public int Echo { get; private set; }
  345. public int Boundary { get; private set; }
  346. public int Margin { get; private set; }
  347. public int Calcification { get; private set; }
  348. public int PosteriorAcousticFeature { get; private set; } /// 后部声学特征 :
  349. }
  350. }