RecognizeCarotidType.cs 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. using Emgu.CV;
  2. using Emgu.CV.CvEnum;
  3. using Emgu.CV.Structure;
  4. using System.Drawing;
  5. using System;
  6. using System.IO;
  7. using System.Reflection;
  8. using System.Collections.Generic;
  9. using WingAIDiagnosisService.Manage;
  10. namespace WingAIDiagnosisService.Carotid.Utilities
  11. {
  12. /// <summary>
  13. /// 识别颈动脉类型,左颈还是右颈
  14. /// </summary>
  15. public class RecognizeCarotidType : IDisposable
  16. {
  17. private const string MFDllName = "mf.dll";
  18. private string[] _fileNameLeft;
  19. private string[] _fileNameRight;
  20. private Image<Bgr, byte>[] _imagesLeft;
  21. private Image<Bgr, byte>[] _imagesRight;
  22. static RecognizeCarotidType()
  23. {
  24. var x64Folder = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Emgu.CV", "win-x64");
  25. if (Environment.OSVersion.Platform == PlatformID.Win32NT)
  26. {
  27. var mfDll = Path.Combine(x64Folder, MFDllName);
  28. if (!File.Exists(mfDll))
  29. {
  30. var systemMfDll = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.System), MFDllName);
  31. //If system32 don't exist mf environment.
  32. if (!File.Exists(systemMfDll))
  33. {
  34. var folderName = GetDirectoryFolderName(Environment.OSVersion.Version);
  35. if (!string.IsNullOrWhiteSpace(folderName))
  36. {
  37. var directoryInfo = new DirectoryInfo(Path.Combine(x64Folder, folderName));
  38. var fileInfos = directoryInfo.GetFiles();
  39. for (int i = 0; i < fileInfos.Length; ++i)
  40. {
  41. File.Copy(fileInfos[i].FullName, Path.Combine(x64Folder, fileInfos[i].Name), true);
  42. }
  43. }
  44. }
  45. }
  46. }
  47. if (Environment.Is64BitProcess)
  48. {
  49. Emgu.Util.Toolbox.SetDllDirectory(x64Folder);
  50. }
  51. else
  52. {
  53. Emgu.Util.Toolbox.SetDllDirectory(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Emgu.CV", "win-x86"));
  54. }
  55. }
  56. private static string GetDirectoryFolderName(Version version)
  57. {
  58. if (version.Major == 6)
  59. {
  60. if (version.Minor == 1)
  61. {
  62. return "6.1";
  63. }
  64. if (version.Minor == 2)
  65. {
  66. return "6.2";
  67. }
  68. }
  69. return string.Empty;
  70. }
  71. public bool Initialization()
  72. {
  73. try
  74. {
  75. var classType = GetType();
  76. var resourceNamespace = classType.Namespace?.Replace("Utilities", "Images");
  77. var assembly = classType.Assembly;
  78. if (assembly == null || string.IsNullOrWhiteSpace(resourceNamespace))
  79. {
  80. return false;
  81. }
  82. //所有资源
  83. var allResourceName = assembly.GetManifestResourceNames();
  84. _fileNameLeft = GetLeftSamplePath(allResourceName, resourceNamespace);
  85. _imagesLeft = new Image<Bgr, byte>[_fileNameLeft.Length];
  86. for (var i = 0; i < _fileNameLeft.Length; ++i)
  87. {
  88. _imagesLeft[i] = GetImageFromResource(_fileNameLeft[i], assembly);
  89. }
  90. _fileNameRight = GetRightSamplePath(allResourceName, resourceNamespace);
  91. _imagesRight = new Image<Bgr, byte>[_fileNameRight.Length];
  92. for (var i = 0; i < _fileNameRight.Length; ++i)
  93. {
  94. _imagesRight[i] = GetImageFromResource(_fileNameRight[i], assembly);
  95. }
  96. return true;
  97. }
  98. catch (Exception e)
  99. {
  100. return false;
  101. }
  102. }
  103. private string[] GetLeftSamplePath(string[] allFilePath, string rootPath)
  104. {
  105. string leftFloder = rootPath + ".left";
  106. string leftFloder1 = rootPath + ".left1";
  107. var nameList = new List<string>();
  108. for (var i = 0; i < allFilePath.Length; ++i)
  109. {
  110. if (allFilePath[i].Contains(leftFloder) || allFilePath[i].Contains(leftFloder1))
  111. {
  112. nameList.Add(allFilePath[i]);
  113. }
  114. }
  115. return nameList.ToArray();
  116. }
  117. private string[] GetRightSamplePath(string[] allFilePath, string rootPath)
  118. {
  119. string leftFloder = rootPath + ".right";
  120. string leftFloder1 = rootPath + ".right1";
  121. var nameList = new List<string>();
  122. for (var i = 0; i < allFilePath.Length; ++i)
  123. {
  124. if (allFilePath[i].Contains(leftFloder) || allFilePath[i].Contains(leftFloder1))
  125. {
  126. nameList.Add(allFilePath[i]);
  127. }
  128. }
  129. return nameList.ToArray();
  130. }
  131. private Image<Bgr, byte> GetImageFromResource(string fileName, Assembly assembly)
  132. {
  133. using Stream stream = assembly.GetManifestResourceStream(fileName);
  134. byte[] imageBuffer = new byte[stream.Length];
  135. stream.Read(imageBuffer, 0, imageBuffer.Length);
  136. //转成彩色图像
  137. using var img = new Mat();
  138. CvInvoke.Imdecode(imageBuffer, ImreadModes.Color, img);
  139. return img.ToImage<Bgr, byte>();
  140. }
  141. public CarotidScanType RecognizeType(Image<Bgr, byte> imageBgr)
  142. {
  143. using Image<Bgr, byte> image = imageBgr.Copy(new Rectangle(0, 0, imageBgr.Width / 2, imageBgr.Height / 2));
  144. double maxLeft = 0;
  145. for (var i = 0; i < _imagesLeft.Length; ++i)
  146. {
  147. var confidence = MatchImage(_imagesLeft[i], image);
  148. if (confidence > maxLeft)
  149. {
  150. maxLeft = confidence;
  151. }
  152. }
  153. double maxRight = 0;
  154. for (var i = 0; i < _imagesRight.Length; ++i)
  155. {
  156. var confidence = MatchImage(_imagesRight[i], image);
  157. if (confidence > maxRight)
  158. {
  159. maxRight = confidence;
  160. }
  161. }
  162. if (maxRight > 0.7 || maxLeft > 0.7)
  163. {
  164. if (maxRight > maxLeft)
  165. {
  166. return CarotidScanType.CarotidRight;
  167. }
  168. }
  169. return CarotidScanType.CarotidLeft;
  170. }
  171. private double MatchImage(Image<Bgr, byte> imageSmall, Image<Bgr, byte> imageLarge)
  172. {
  173. using Mat MatchResult = new Mat();//匹配结果
  174. //判断子图是否在原图中
  175. CvInvoke.MatchTemplate(imageLarge, imageSmall, MatchResult, TemplateMatchingType.CcorrNormed);
  176. Point max_loc = new Point();
  177. Point min_loc = new Point();
  178. double max = 0, min = 0;
  179. CvInvoke.MinMaxLoc(MatchResult, ref min, ref max, ref min_loc, ref max_loc);//获得极值信息
  180. return max;
  181. }
  182. public void Dispose()
  183. {
  184. if (_fileNameLeft != null && _imagesLeft != null)
  185. {
  186. for (var i = 0; i < _fileNameLeft.Length; ++i)
  187. {
  188. _fileNameLeft[i] = null;
  189. _imagesLeft[i]?.Dispose();
  190. }
  191. }
  192. if (_fileNameRight != null && _imagesRight != null)
  193. {
  194. for (var i = 0; i < _fileNameRight.Length; ++i)
  195. {
  196. _fileNameRight[i] = null;
  197. _imagesRight[i]?.Dispose();
  198. }
  199. }
  200. _fileNameLeft = null;
  201. _fileNameRight = null;
  202. _imagesLeft = null;
  203. _imagesRight = null;
  204. }
  205. }
  206. }