using System;
using System.IO;
using System.Text;
using System.Collections.Generic;
using System.Drawing;
//using System.Data;
using System.Data.SQLite;
using Newtonsoft.Json;
using System.Security.Cryptography;
using System.Windows;
using System.Globalization;
//using AI.Common;
using AI.Common.Interface;
using RawImageShowUtilsLib;
namespace SegmentDescribDemo
{
public enum EnumDesShapeValue
{
///
/// 椭圆形
///
Oval,
///
/// 类圆形
///
Round,
///
/// 不规则形
///
Irregular,
}
///
/// 方向描述可能的结果值
///
public enum EnumDesOrientationValue
{
///
/// 平行
///
Parallel,
///
/// 非平行
///
NonParallel,
}
///
/// 内部回声描述可能的结果
///
public enum EnumDesEchoPatternValue
{
///
/// 无回声
///
Anechoic,
///
/// 低回声
///
Hypoechoic,
///
/// 等回声
///
Isoechoic,
///
/// 高回声
///
Hyperechoic,
///
/// 混合回声
///
Complex,
///
/// 强回声
///
Strongechoic,
}
///
/// 病灶边界描述可能的结果
///
public enum EnumDesLesionBoundaryValue
{
///
/// 清晰
///
AbruptInterface,
///
/// 模糊
///
EchogenicHalo,
}
///
/// 病灶边缘描述的可能结果
///
public enum EnumDesMarginValue
{
///
/// 光整
///
Circumscribed,
///
/// 不光整
///
NonCircumscribed,
}
///
/// 病灶描述GT图像信息
///
public class LesionDesGTImgInfo
{
public string ImageId { get; set; }
///
/// 病灶分级
///
public List Label { get; set; }
///
/// 该图上所有病灶的标准描述
///
public List Lesions { get; set; }
}
///
/// 病灶信息
///
public class LesionInfo
{
///
/// 病灶轮廓
///
public List Contour { get; set; } = new List();
///
/// 病灶形状
///
public EnumDesShapeValue Shape { get; set; } = EnumDesShapeValue.Oval;
///
/// 方向
///
public EnumDesOrientationValue Orientation { get; set; } = EnumDesOrientationValue.Parallel;
///
/// 回声类型
///
public EnumDesEchoPatternValue EchoPattern { get; set; } = EnumDesEchoPatternValue.Hypoechoic;
///
/// 边界清晰不清晰
///
public EnumDesLesionBoundaryValue Boundary { get; set; } = EnumDesLesionBoundaryValue.AbruptInterface;
///
/// 边缘光整不光整
///
public EnumDesMarginValue Margin { get; set; } = EnumDesMarginValue.Circumscribed;
///
/// 横径起点
///
public Point2D HorizontalPoint1 { get; set; } = new Point2D();
///
/// 横径终点
///
public Point2D HorizontalPoint2 { get; set; } = new Point2D();
///
/// 纵径起点
///
public Point2D VerticalPoint1 { get; set; } = new Point2D();
///
/// 纵径终点
///
public Point2D VerticalPoint2 { get; set; } = new Point2D();
///
/// 内容完整
///
public bool IsCompleted
{
get
{
return Contour.Count > 0 && HorizontalPoint1 != HorizontalPoint2 &&
VerticalPoint1 != VerticalPoint2;
}
}
}
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 _allImages = new List();
private volatile int _imgIndex = 0;
public List _lesions = new List();
private volatile int _lesionIndex = 0;
private System.Windows.Point _mouseStartPos;
// 绘制中的所有轮廓点的集合
private List _drawingPoints = new List();
private volatile bool _canAutoFinish = false;
#endregion
public DataBase(string dbpath, string imgPath)
{
_dbPath = dbpath;
_imageDataFolder = imgPath;
}
public DataBase(string dbpath)
{
_dbPath = dbpath;
}
///
/// 读入数据库
///
public void ReadDataBase()
{
try
{
if (!File.Exists(_dbPath))
{
MessageBox.Show("未找到存放待标注数据的文件:" + _dbPath);
return;
}
SQLiteConnection dbCon = new SQLiteConnection("Data Source=" + _dbPath);
dbCon.Open();
SQLiteCommand dbCmd;
dbCmd = dbCon.CreateCommand();
dbCmd.CommandText = "select * from LesionDesGTDatas";
SQLiteDataReader dbReader;
dbReader = dbCmd.ExecuteReader();
if (!dbReader.HasRows)
{
dbReader.Close();
dbCmd.Dispose();
dbCon.Close();
dbCon.Dispose();
MessageBox.Show("待标注的数据为空.");
return;
}
int index = 0;
while (dbReader.Read())
{
string imgId = ((string)dbReader[0]).TrimEnd();
string strLesions = ((string)dbReader[1]).TrimEnd();
List lesions = JsonConvert.DeserializeObject>(strLesions);
string strLesionsLabels = ((string)dbReader[2]).TrimEnd();
List lesionsLabels = JsonConvert.DeserializeObject>(strLesionsLabels);
index += 1;
_allImages.Add(new LesionDesGTImgInfo { ImageId = imgId, Lesions = lesions, Label = lesionsLabels });
}
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 ;
}
catch (Exception excep)
{
MessageBox.Show("读入数据库时出错:" + 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();
}
}
}