123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223 |
- using System;
- using System.IO;
- using System.Text;
- using System.Collections.Generic;
- using System.Drawing;
- using System.Data.SQLite;
- using Newtonsoft.Json;
- using System.Security.Cryptography;
- using System.Windows;
- namespace ThyroidDescriptionUI
- {
- /// Canvas上绘制的目标有几种类型
- public enum EnumCanvasItemType
- {
- Drawing,
- Choosed,
- Existed,
- }
- /// 线的种类
- public enum EnumCanvasLineType
- {
- Horizontal,
- Vertical,
- }
- /// 病灶信息
- public class LesionInfo
- {
- // 病灶轮廓
- public List<MyPoint> LesionContour { get; set; } = new List<MyPoint>();
- // 回声
- public EchoPatternEnum EchoPattern { get; set; } = 0;
- // 病灶形状
- public ShapeEnum Shape { get; set; } = 0;
- // 边缘
- public MarginEnum Margin { get; set; } = 0;
- // 局灶性强回声
- public EchogenicFociEnum EchogenicFoci { get; set; } = 0;
- // 横径起点
- public MyPoint HorizontalPoint1 { get; set; } = null;
- /// 横径终点
- public MyPoint HorizontalPoint2 { get; set; } = null;
- /// 纵径起点
- public MyPoint VerticalPoint1 { get; set; } = null;
- /// 纵径终点
- public MyPoint VerticalPoint2 { get; set; } = null;
- /// 内容完整
- public bool IsCompleted
- {
- get
- {
- return LesionContour.Count > 0 /*&& ThyroidContour.Count > 0*/ && HorizontalPoint1 != null
- && HorizontalPoint2 != null && VerticalPoint1 != null && VerticalPoint2 != null;
- }
- }
- }
- /// 病灶描述GT图像信息
- public class GTImgInfo
- {
- public string ImageId { get; set; }
- /// 该图上所有病灶的标准描述
- public List<LesionInfo> Lesions { get; set; }
- /// 甲状腺轮廓
- public List<MyPoint> ThyroidContour { get; set; }
- }
- class DataBase
- {
- #region 数据库相关变量
- private readonly string _dataFolder = System.IO.Path.Combine(System.IO.Path.GetDirectoryName(System.IO.Path.GetDirectoryName(
- AppDomain.CurrentDomain.BaseDirectory)), "LesionDescriptionGTDatas");
- public readonly string _dbPath;
- public readonly string _imageDataFolder;
- public List<GTImgInfo> _allImages = new List<GTImgInfo>();
- private volatile int _imgIndex = 0;
- public List<LesionInfo> _lesions = new List<LesionInfo>();
- private volatile int _lesionIndex = 0;
- private System.Windows.Point _mouseStartPos;
- // 绘制中的所有轮廓点的集合
- private List<System.Windows.Point> _drawingPoints = new List<System.Windows.Point>();
- private volatile bool _canAutoFinish = false;
- #endregion
- public DataBase(string dbpath, string imgPath)
- {
- _dbPath = dbpath;
- _imageDataFolder = imgPath;
- }
- public DataBase(string dbpath)
- {
- _dbPath = dbpath;
- }
- /// 读入数据库
- public bool ReadDataBase()
- {
- try
- {
- if (!File.Exists(_dbPath))
- {
- //throw new Exception("未找到存放待标注数据的文件:" + _dbPath);
- return false;
- }
- SQLiteConnection dbCon = new SQLiteConnection("Data Source=" + _dbPath);
- dbCon.Open();
- SQLiteCommand dbCmd;
- dbCmd = dbCon.CreateCommand();
- dbCmd.CommandText = "select * from GTDatas";
- SQLiteDataReader dbReader;
- dbReader = dbCmd.ExecuteReader();
- if (!dbReader.HasRows)
- {
- dbReader.Close();
- dbCmd.Dispose();
- dbCon.Close();
- dbCon.Dispose();
- throw new Exception("待标注的数据为空.");
- return false;
- }
- while (dbReader.Read())
- {
- string imgId = ((string)dbReader[0]).TrimEnd();
- string strLesions = ((string)dbReader[1]).TrimEnd();
- List<LesionInfo> lesions = JsonConvert.DeserializeObject<List<LesionInfo>>(strLesions);
- string strThyroidCont = ((string)dbReader[2]).TrimEnd();
- List<MyPoint> thyroidContour = JsonConvert.DeserializeObject<List<MyPoint>>(strThyroidCont);
- _allImages.Add(new GTImgInfo { ImageId = imgId, Lesions = lesions, ThyroidContour = thyroidContour });
- }
- dbReader.Close();
- dbCmd.Dispose();
- dbCmd = dbCon.CreateCommand();
- dbCmd.CommandText = "select * from LastModification";
- dbReader = dbCmd.ExecuteReader();
- if (!dbReader.HasRows)
- {
- _imgIndex = 0;
- }
- else
- {
- dbReader.Read();
- string imgId = ((string)dbReader[0]).TrimEnd();
- int imgIndex = _allImages.FindIndex(x => x.ImageId == imgId); //最后一个修改的图像索引
- if (imgIndex == -1)
- {
- _imgIndex = 0;
- }
- else
- {
- _imgIndex = imgIndex;
- }
- }
- dbReader.Close();
- dbCmd.Dispose();
- dbCon.Close();
- dbCon.Dispose();
- return true;
- }
- catch (Exception excep)
- {
- throw new Exception("读入数据库时出错:" + excep);
- }
- }
- private string ComputeHashCode(byte[] input)
- {
- MD5 md5 = MD5.Create();
- byte[] hash = md5.ComputeHash(input);
- StringBuilder sb = new StringBuilder();
- foreach (var b in hash)
- {
- sb.Append(b.ToString("x2"));
- }
- string hashstr = sb.ToString();
- sb.Clear();
- return hashstr;
- }
- /// 创建数据库
- public void CreateDataBase()
- {
- if (File.Exists(_dbPath))
- {
- throw new Exception("数据库已存在,请检查.");
- }
- // 创建数据库文件
- SQLiteConnection dbCon = new SQLiteConnection("Data Source=" + _dbPath);
- dbCon.Open();
- SQLiteCommand dbCmd;
- dbCmd = dbCon.CreateCommand();
- dbCmd.CommandText = "create table if not exists GTDatas(ImageId varchar(16),Lesions varchar(16),ThyroidContour varchar(16))";
- dbCmd.ExecuteNonQuery();
- dbCmd.Dispose();
- dbCmd = dbCon.CreateCommand();
- dbCmd.CommandText = "create table if not exists LastModification(ImageId varchar(16))";
- dbCmd.ExecuteNonQuery();
- dbCmd.Dispose();
- dbCon.Close();
- dbCon.Dispose();
- }
- }
- }
|