1
0

ReadDataBase.cs 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. using System;
  2. using System.IO;
  3. using System.Text;
  4. using System.Collections.Generic;
  5. using System.Drawing;
  6. //using System.Data;
  7. using System.Data.SQLite;
  8. using Newtonsoft.Json;
  9. using System.Security.Cryptography;
  10. using System.Windows;
  11. using System.Globalization;
  12. //using AI.Common;
  13. using AI.Common.Interface;
  14. using RawImageShowUtilsLib;
  15. namespace SegmentDescribDemo
  16. {
  17. public enum EnumDesShapeValue
  18. {
  19. /// <summary>
  20. /// 椭圆形
  21. /// </summary>
  22. Oval,
  23. /// <summary>
  24. /// 类圆形
  25. /// </summary>
  26. Round,
  27. /// <summary>
  28. /// 不规则形
  29. /// </summary>
  30. Irregular,
  31. }
  32. /// <summary>
  33. /// 方向描述可能的结果值
  34. /// </summary>
  35. public enum EnumDesOrientationValue
  36. {
  37. /// <summary>
  38. /// 平行
  39. /// </summary>
  40. Parallel,
  41. /// <summary>
  42. /// 非平行
  43. /// </summary>
  44. NonParallel,
  45. }
  46. /// <summary>
  47. /// 内部回声描述可能的结果
  48. /// </summary>
  49. public enum EnumDesEchoPatternValue
  50. {
  51. /// <summary>
  52. /// 无回声
  53. /// </summary>
  54. Anechoic,
  55. /// <summary>
  56. /// 低回声
  57. /// </summary>
  58. Hypoechoic,
  59. /// <summary>
  60. /// 等回声
  61. /// </summary>
  62. Isoechoic,
  63. /// <summary>
  64. /// 高回声
  65. /// </summary>
  66. Hyperechoic,
  67. /// <summary>
  68. /// 混合回声
  69. /// </summary>
  70. Complex,
  71. /// <summary>
  72. /// 强回声
  73. /// </summary>
  74. Strongechoic,
  75. }
  76. /// <summary>
  77. /// 病灶边界描述可能的结果
  78. /// </summary>
  79. public enum EnumDesLesionBoundaryValue
  80. {
  81. /// <summary>
  82. /// 清晰
  83. /// </summary>
  84. AbruptInterface,
  85. /// <summary>
  86. /// 模糊
  87. /// </summary>
  88. EchogenicHalo,
  89. }
  90. /// <summary>
  91. /// 病灶边缘描述的可能结果
  92. /// </summary>
  93. public enum EnumDesMarginValue
  94. {
  95. /// <summary>
  96. /// 光整
  97. /// </summary>
  98. Circumscribed,
  99. /// <summary>
  100. /// 不光整
  101. /// </summary>
  102. NonCircumscribed,
  103. }
  104. /// <summary>
  105. /// 病灶描述GT图像信息
  106. /// </summary>
  107. public class LesionDesGTImgInfo
  108. {
  109. public string ImageId { get; set; }
  110. /// <summary>
  111. /// 病灶分级
  112. /// </summary>
  113. public List<int> Label { get; set; }
  114. /// <summary>
  115. /// 该图上所有病灶的标准描述
  116. /// </summary>
  117. public List<LesionInfo> Lesions { get; set; }
  118. }
  119. /// <summary>
  120. /// 病灶信息
  121. /// </summary>
  122. public class LesionInfo
  123. {
  124. /// <summary>
  125. /// 病灶轮廓
  126. /// </summary>
  127. public List<MyPoint> Contour { get; set; } = new List<MyPoint>();
  128. /// <summary>
  129. /// 病灶形状
  130. /// </summary>
  131. public EnumDesShapeValue Shape { get; set; } = EnumDesShapeValue.Oval;
  132. /// <summary>
  133. /// 方向
  134. /// </summary>
  135. public EnumDesOrientationValue Orientation { get; set; } = EnumDesOrientationValue.Parallel;
  136. /// <summary>
  137. /// 回声类型
  138. /// </summary>
  139. public EnumDesEchoPatternValue EchoPattern { get; set; } = EnumDesEchoPatternValue.Hypoechoic;
  140. /// <summary>
  141. /// 边界清晰不清晰
  142. /// </summary>
  143. public EnumDesLesionBoundaryValue Boundary { get; set; } = EnumDesLesionBoundaryValue.AbruptInterface;
  144. /// <summary>
  145. /// 边缘光整不光整
  146. /// </summary>
  147. public EnumDesMarginValue Margin { get; set; } = EnumDesMarginValue.Circumscribed;
  148. /// <summary>
  149. /// 横径起点
  150. /// </summary>
  151. public Point2D HorizontalPoint1 { get; set; } = new Point2D();
  152. /// <summary>
  153. /// 横径终点
  154. /// </summary>
  155. public Point2D HorizontalPoint2 { get; set; } = new Point2D();
  156. /// <summary>
  157. /// 纵径起点
  158. /// </summary>
  159. public Point2D VerticalPoint1 { get; set; } = new Point2D();
  160. /// <summary>
  161. /// 纵径终点
  162. /// </summary>
  163. public Point2D VerticalPoint2 { get; set; } = new Point2D();
  164. /// <summary>
  165. /// 内容完整
  166. /// </summary>
  167. public bool IsCompleted
  168. {
  169. get
  170. {
  171. return Contour.Count > 0 && HorizontalPoint1 != HorizontalPoint2 &&
  172. VerticalPoint1 != VerticalPoint2;
  173. }
  174. }
  175. }
  176. class DataBase
  177. {
  178. #region 数据库相关变量
  179. private readonly string _dataFolder = System.IO.Path.Combine(System.IO.Path.GetDirectoryName(System.IO.Path.GetDirectoryName(
  180. AppDomain.CurrentDomain.BaseDirectory)), "LesionDescriptionGTDatas");
  181. public readonly string _dbPath;
  182. public readonly string _imageDataFolder;
  183. public List<LesionDesGTImgInfo> _allImages = new List<LesionDesGTImgInfo>();
  184. private volatile int _imgIndex = 0;
  185. public List<LesionInfo> _lesions = new List<LesionInfo>();
  186. private volatile int _lesionIndex = 0;
  187. private System.Windows.Point _mouseStartPos;
  188. // 绘制中的所有轮廓点的集合
  189. private List<System.Windows.Point> _drawingPoints = new List<System.Windows.Point>();
  190. private volatile bool _canAutoFinish = false;
  191. #endregion
  192. public DataBase(string dbpath, string imgPath)
  193. {
  194. _dbPath = dbpath;
  195. _imageDataFolder = imgPath;
  196. }
  197. public DataBase(string dbpath)
  198. {
  199. _dbPath = dbpath;
  200. }
  201. /// <summary>
  202. /// 读入数据库
  203. /// </summary>
  204. public void ReadDataBase()
  205. {
  206. try
  207. {
  208. if (!File.Exists(_dbPath))
  209. {
  210. MessageBox.Show("未找到存放待标注数据的文件:" + _dbPath);
  211. return;
  212. }
  213. SQLiteConnection dbCon = new SQLiteConnection("Data Source=" + _dbPath);
  214. dbCon.Open();
  215. SQLiteCommand dbCmd;
  216. dbCmd = dbCon.CreateCommand();
  217. dbCmd.CommandText = "select * from LesionDesGTDatas";
  218. SQLiteDataReader dbReader;
  219. dbReader = dbCmd.ExecuteReader();
  220. if (!dbReader.HasRows)
  221. {
  222. dbReader.Close();
  223. dbCmd.Dispose();
  224. dbCon.Close();
  225. dbCon.Dispose();
  226. MessageBox.Show("待标注的数据为空.");
  227. return;
  228. }
  229. int index = 0;
  230. while (dbReader.Read())
  231. {
  232. string imgId = ((string)dbReader[0]).TrimEnd();
  233. string strLesions = ((string)dbReader[1]).TrimEnd();
  234. List<LesionInfo> lesions = JsonConvert.DeserializeObject<List<LesionInfo>>(strLesions);
  235. string strLesionsLabels = ((string)dbReader[2]).TrimEnd();
  236. List<int> lesionsLabels = JsonConvert.DeserializeObject<List<int>>(strLesionsLabels);
  237. index += 1;
  238. _allImages.Add(new LesionDesGTImgInfo { ImageId = imgId, Lesions = lesions, Label = lesionsLabels });
  239. }
  240. dbReader.Close();
  241. dbCmd.Dispose();
  242. dbCmd = dbCon.CreateCommand();
  243. dbCmd.CommandText = "select * from LastModification";
  244. dbReader = dbCmd.ExecuteReader();
  245. if (!dbReader.HasRows)
  246. {
  247. _imgIndex = 0;
  248. }
  249. else
  250. {
  251. dbReader.Read();
  252. string imgId = ((string)dbReader[0]).TrimEnd();
  253. int imgIndex = _allImages.FindIndex(x => x.ImageId == imgId);
  254. if (imgIndex == -1)
  255. {
  256. _imgIndex = 0;
  257. }
  258. else
  259. {
  260. _imgIndex = imgIndex;
  261. }
  262. }
  263. dbReader.Close();
  264. dbCmd.Dispose();
  265. dbCon.Close();
  266. dbCon.Dispose();
  267. //return ;
  268. }
  269. catch (Exception excep)
  270. {
  271. MessageBox.Show("读入数据库时出错:" + excep);
  272. }
  273. }
  274. private string ComputeHashCode(byte[] input)
  275. {
  276. MD5 md5 = MD5.Create();
  277. byte[] hash = md5.ComputeHash(input);
  278. StringBuilder sb = new StringBuilder();
  279. foreach (var b in hash)
  280. {
  281. sb.Append(b.ToString("x2"));
  282. }
  283. string hashstr = sb.ToString();
  284. sb.Clear();
  285. return hashstr;
  286. }
  287. /// 创建数据库
  288. public void CreateDataBase()
  289. {
  290. if (File.Exists(_dbPath))
  291. {
  292. throw new Exception("数据库已存在,请检查.");
  293. }
  294. // 创建数据库文件
  295. SQLiteConnection dbCon = new SQLiteConnection("Data Source=" + _dbPath);
  296. dbCon.Open();
  297. SQLiteCommand dbCmd;
  298. dbCmd = dbCon.CreateCommand();
  299. dbCmd.CommandText = "create table if not exists GTDatas(ImageId varchar(16),Lesions varchar(16),ThyroidContour varchar(16))";
  300. dbCmd.ExecuteNonQuery();
  301. dbCmd.Dispose();
  302. dbCmd = dbCon.CreateCommand();
  303. dbCmd.CommandText = "create table if not exists LastModification(ImageId varchar(16))";
  304. dbCmd.ExecuteNonQuery();
  305. dbCmd.Dispose();
  306. dbCon.Close();
  307. dbCon.Dispose();
  308. }
  309. }
  310. }