123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959 |
- using System;
- using System.Windows;
- using System.Threading;
- using System.Threading.Tasks;
- using System.IO;
- using System.Collections.Generic;
- using Microsoft.Win32;
- using System.Drawing;
- using Accord.Video;
- using Accord.Video.FFMPEG;
- using Accord.Video.DirectShow;
- using UsHepatoRenalRatioDetectLib;
- using AI.Common;
- using AI.Common.Log;
- using Rect = AI.Common.Rect;
- using ImageShowUtilsLib;
- using System.Windows.Media.Imaging;
- using System.Windows.Input;
- using System.Windows.Media;
- using System.Xml.Linq;
- using System.ComponentModel;
- using Rectangle = System.Drawing.Rectangle;
- #test
- namespace HepatoRenalEchoContrastDemo
- {
- public enum TestType
- {
- None,
- ImageTest,
- VideoTest,
- CardTest
- }
- /// <summary>
- /// MainWindow.xaml 的交互逻辑
- /// </summary>
- public partial class MainWindow : Window
- {
- #region private
- private volatile bool _showOrganSeg = true;
- private volatile bool _showRois = false;
- private volatile bool _showEchoContrastValue = true;
- private IUsHepatoRenalRatioDetect _echoContrastDetect;
- private ManualResetEvent _playEvent = new ManualResetEvent(false);
- private VideoFileReader _videoReader;
- private readonly object _videoReaderLocker = new object();
- private VideoCaptureDevice _videoCapture;
- private volatile int _frameIntervalTime = 0;
- private volatile bool _readingVideo = true; //false
- private volatile bool _pausing = false;
- private volatile int _frameCount = 0;
- private volatile int _frameIndex = 0;
- private Thread _videoPlayThread = null;
- private volatile int _lastDetectTickCount;
- private volatile int _fpsStartTickCount;
- private volatile int _displayCount = 0;
- private volatile bool _continuouslyDetecting = false;
- private ManualResetEvent _videoPlayEvent = new ManualResetEvent(false);
- /// ///////////////////////////////////////////////
- private bool _isCropImag;
- private volatile TestType _testType = TestType.None;
- private bool _isManulSelROI; // 是否选择手动画框
- // 鼠标动作
- //private volatile EnumMouseOperType _mouseOper = EnumMouseOperType.None;
- private volatile bool _mouseLeftBtnPressed = false;
- // 鼠标绘制的起始位置(画线时需要)
- private System.Windows.Point _mouseStartPos;
- // 绘制中的所有轮廓点的集合
- private List<System.Windows.Point> _drawingPoints = new List<System.Windows.Point>();
- private Point2D _mousePosInOrigImg = new Point2D(0, 0);
- private Point2D _startPosInOrigImg = new Point2D(0, 0);
- private List<Point2D> _contourInOrigImg = new List<Point2D>();
- private List<Rect> roisManual = new List<Rect>();
- private Bitmap _imageBuffer;
- private volatile bool _isEnableDetect = false; // 开启测试
- #endregion
- #region 用户界面响应
- public MainWindow()
- {
- InitializeComponent();
- CheckBoxShowOrganSeg.IsChecked = _showOrganSeg;
- CheckBoxShowRois.IsChecked = _showRois;
- CheckBoxShowEchoContrastValue.IsChecked = _showEchoContrastValue;
- //刷新采集卡
- RefreshComboBoxVideoCards();
- // todo
- MyImageCanvas.SetShowParams(_showOrganSeg, _showRois, _showEchoContrastValue);
- }
- /// <summary>
- /// 用户关闭窗口
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void OnWindowClosing(object sender, System.ComponentModel.CancelEventArgs e)
- {
- StopPlay();
- _isEnableDetect = false;
- if (_echoContrastDetect != null)
- {
- _echoContrastDetect.StopProcess();
- _echoContrastDetect.NotifyError -= OnErrorOccur;
- _echoContrastDetect.NotifyLog -= OnLogWrite;
- _echoContrastDetect.NotifyProcessFinish -= OnPreprocessedDatasUpdate;
- _echoContrastDetect.Dispose();
- _echoContrastDetect = null;
- }
- if (_imageBuffer != null)
- {
- _imageBuffer.Dispose();
- _imageBuffer = null;
- }
- }
- /// <summary>
- /// 显示器官分割
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void OnCheckBoxShowOrganSegChecked(object sender, RoutedEventArgs e)
- {
- _showOrganSeg = true;
- MyImageCanvas.SetShowParams(_showOrganSeg, _showRois, _showEchoContrastValue);
- }
- /// <summary>
- /// 不显示器官分割
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void OnCheckBoxShowOrganSegUnchecked(object sender, RoutedEventArgs e)
- {
- _showOrganSeg = false;
- MyImageCanvas.SetShowParams(_showOrganSeg, _showRois, _showEchoContrastValue);
- }
- /// <summary>
- /// 显示roi
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void OnCheckBoxShowRoisChecked(object sender, RoutedEventArgs e)
- {
- _showRois = true;
- MyImageCanvas.SetShowParams(_showOrganSeg, _showRois, _showEchoContrastValue);
- }
- /// <summary>
- /// 不显示roi
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void OnCheckBoxShowRoisUnchecked(object sender, RoutedEventArgs e)
- {
- _showRois = false;
- MyImageCanvas.SetShowParams(_showOrganSeg, _showRois, _showEchoContrastValue);
- }
- /// <summary>
- /// 显示肝肾比
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void OnCheckBoxShowEchoContrastValueChecked(object sender, RoutedEventArgs e)
- {
- _showEchoContrastValue = true;
- MyImageCanvas.SetShowParams(_showOrganSeg, _showRois, _showEchoContrastValue);
- }
- /// <summary>
- /// 不显示肝肾比
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void OnCheckBoxShowEchoContrastValueUnchecked(object sender, RoutedEventArgs e)
- {
- _showEchoContrastValue = false;
- MyImageCanvas.SetShowParams(_showOrganSeg, _showRois, _showEchoContrastValue);
- }
- private void OnCheckBoxIsCropChecked(object sender, RoutedEventArgs e)
- {
- _isCropImag = true;
- }
- private void OnCheckBoxIsCropUnchecked(object sender, RoutedEventArgs e)
- {
- _isCropImag = false;
- }
- /// <summary>
- /// 开启手动选框
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void OnCheckBoxIsManulSelectROIChecked(object sender, RoutedEventArgs e)
- {
- _isManulSelROI = true;
- BtnCleraRois.IsEnabled = true;
- BtnCalHRR.IsEnabled= true;
- BtnClearHRR.IsEnabled= true;
- BtnStartAutoDetect.IsEnabled = false;
- BtnStopAutoDetect.IsEnabled = false;
- }
- /// <summary>
- /// 停止手动选框
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void OnCheckBoxIsManulSelectROIUnchecked(object sender, RoutedEventArgs e)
- {
- _isManulSelROI = false;
- BtnCleraRois.IsEnabled = false;
- BtnCalHRR.IsEnabled = false;
- BtnClearHRR.IsEnabled = false;
- BtnStartAutoDetect.IsEnabled = true;
- BtnStopAutoDetect.IsEnabled = true;
- }
- /// <summary>
- /// 加载图片或视频
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void OnBtnLoadVideoClick(object sender, RoutedEventArgs e)
- {
- StopPlay();
- var ofd = new OpenFileDialog();
- ofd.Filter = "图像或视频|*.png;*.bmp;*.jpg;*.jpeg;*.mp4;*.avi";
- ofd.Multiselect = false;
- ofd.Title = "选择一幅图像或一个视频";
- if (ofd.ShowDialog() == true)
- {
- var fileName = ofd.FileName;
- var fileExtension = Path.GetExtension(fileName);
- if (fileExtension == ".png" || fileExtension == ".bmp" || fileExtension == ".jpg" || fileExtension == ".jpeg")
- {
- BtnPauseOrPlay.Content = "播放";
- var bitmap = new Bitmap(fileName);
- MyImageCanvas.UpdateImage(bitmap);
- if (_imageBuffer != null)
- {
- _imageBuffer.Dispose();
- }
- _imageBuffer = bitmap.Clone(new Rectangle(0, 0, bitmap.Width, bitmap.Height), bitmap.PixelFormat); ;
- bitmap.Dispose();
- _testType = TestType.ImageTest;
- }
- else
- {
- _testType = TestType.VideoTest;
- double frameRate;
- //long frameCount;
- lock (_videoReaderLocker)
- {
- _videoReader = new VideoFileReader();
- _videoReader.Open(fileName);
- frameRate = _videoReader.FrameRate.ToDouble();
- _frameCount = (int)_videoReader.FrameCount;
- }
- //var sleepTime = (int)(1000 / frameRate);
- _playEvent.Reset();
- _readingVideo = true;
- _pausing = false; // 默认自动播放
- ProgressBarVideoPlay.IsEnabled = true;
- BtnBack.IsEnabled = true;
- BtnPauseOrPlay.IsEnabled = true;
- BtnForward.IsEnabled = true;
- BtnPauseOrPlay.Content = "暂停";
- ProgressBarVideoPlay.Minimum = 0;
- ProgressBarVideoPlay.Maximum = _frameCount;
- _lastDetectTickCount = Environment.TickCount;
- _fpsStartTickCount = _lastDetectTickCount;
- _displayCount = 0;
- _frameIndex = 0;
- _playEvent.Reset();
- MyImageCanvas.ClearTestResult();
- if (_videoPlayThread == null || !_videoPlayThread.IsAlive)
- {
- _videoPlayThread = new Thread(() => DoVideoPlay())
- {
- IsBackground = true,
- Name = "AI_Myocardial_Segmentation_VideoPlay",
- };
- _videoPlayThread.Start();
- }
- }
- }
- }
- /// <summary>
- /// 被选择的采集卡有变
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void OnComboBoxCardsSelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
- {
- StopPlay();
- }
- /// <summary>
- /// 刷新所有采集卡
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void OnRefreshCardsClick(object sender, RoutedEventArgs e)
- {
- RefreshComboBoxVideoCards();
- }
- /// <summary>
- /// 打开采集卡
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void OnBtnOpenGrabClick(object sender, RoutedEventArgs e)
- {
- if (ComboBoxCards.SelectedIndex != -1)
- {
- _testType = TestType.CardTest;
- StopPlay();
- var device = (FilterInfo)ComboBoxCards.SelectedItem;
- _videoCapture = new VideoCaptureDevice(device.MonikerString);
- _videoCapture.NewFrame += OnVideoCaptureNewFrame;
- _videoCapture.Start();
- }
- else
- {
- MessageBox.Show("未检测到可用的视频采集设备!");
- }
- }
- /// <summary>
- /// 暂停或播放视频
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void OnBtnPauseOrPlayClick(object sender, RoutedEventArgs e)
- {
- if (!_readingVideo)
- {
- return;
- }
- if (_continuouslyDetecting)
- {
- // 停止自动播放
- _videoPlayEvent.Set();
- _pausing = true;
- BtnPauseOrPlay.Content = "播放";
- }
- else
- {
- // 开始自动播放
- _pausing = false;
- _videoPlayEvent.Reset();
- if (_videoPlayThread == null || !_videoPlayThread.IsAlive)
- {
- _videoPlayThread = new Thread(() => DoVideoPlay())
- {
- IsBackground = true,
- Name = "AI_Myocardial_Segmentation_VideoPlay",
- };
- _videoPlayThread.Start();
- }
- BtnPauseOrPlay.Content = "暂停";
- }
- }
- /// <summary>
- /// 前一帧
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void OnBtnBackClick(object sender, RoutedEventArgs e)
- {
- if (!_readingVideo)
- {
- return;
- }
- if (_continuouslyDetecting)
- {
- // 停止自动播放
- _videoPlayEvent.Set();
- _pausing = true;
- BtnPauseOrPlay.Content = "播放";
- }
- // 往后退一帧
- if (_frameIndex <= 0)
- {
- _frameIndex = _frameCount - 1;
- }
- else
- {
- _frameIndex -= 2;
- }
- ProgressBarVideoPlay.Value = _frameIndex;
- TextFrameIndex.Text = _frameIndex.ToString();
- // 显示当前帧
- Task.Run(() =>
- {
- Bitmap bitmap;
- lock (_videoReaderLocker)
- {
- bitmap = _videoReader.ReadVideoFrame(_frameIndex);
- }
- // 更新显示图像
- MyImageCanvas.UpdateImage(bitmap);
- DetectImage(bitmap);
- bitmap.Dispose();
- });
- }
- /// <summary>
- /// 后一帧
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void OnBtnForwardClick(object sender, RoutedEventArgs e)
- {
- if (!_readingVideo)
- {
- return;
- }
- if (_continuouslyDetecting)
- {
- // 停止自动播放
- _videoPlayEvent.Set();
- _pausing = true;
- BtnPauseOrPlay.Content = "播放";
- }
- // 往前走一帧
- if (_frameIndex >= _frameCount - 2)
- {
- _frameIndex = 0;
- }
- else
- {
- _frameIndex += 2;
- }
- ProgressBarVideoPlay.Value = _frameIndex;
- TextFrameIndex.Text = _frameIndex.ToString();
- // 显示当前帧
- Task.Run(() =>
- {
- Bitmap bitmap;
- lock (_videoReaderLocker)
- {
- bitmap = _videoReader.ReadVideoFrame(_frameIndex);
- }
- MyImageCanvas.UpdateImage(bitmap);
- DetectImage(bitmap);
- bitmap.Dispose();
- });
- }
- /// <summary>
- /// 清空手动框
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void OnBtnClearRoisClick(object sender, RoutedEventArgs e)
- {
- MyImageCanvas.RemoveRoiInCanvas("liver");
- MyImageCanvas.RemoveRoiInCanvas("kidney");
- roisManual.Clear();
- }
- /// <summary>
- /// 根据输入框计算Hrr
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void OnBtnCalHRRClick(object sender, RoutedEventArgs e)
- {
- if (_imageBuffer == null)
- {
- MessageBox.Show("请重新加载一张图片!");
- return;
- }
- if (roisManual.Count != 2)
- {
- MessageBox.Show("请先选择两个roi框");
- return;
- }
- if (_echoContrastDetect == null)
- {
- _echoContrastDetect = new UsHepatoRenalRatioDetect();
- _echoContrastDetect.NotifyError += OnErrorOccur;
- _echoContrastDetect.NotifyLog += OnLogWrite;
- _echoContrastDetect.NotifyProcessFinish += OnPreprocessedDatasUpdate;
- var rawImage = RawImageShowUtils.BitmapToRawImage(_imageBuffer);
- double ratio = _echoContrastDetect.GetHepatoRenalRatio(rawImage, roisManual[0], roisManual[1]);
- // 更新显示结果
- MyImageCanvas.UpdateTestResults(new HRRDetectResultPerImg(new SegResultPerOrgan[0], new Dictionary<EnumOrgans, Rect>(), ratio));
- }
- }
- /// <summary>
- /// 清空手动框结果
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void OnBtnClearHRRClick(object sender, RoutedEventArgs e)
- {
- if (_echoContrastDetect != null)
- {
- _echoContrastDetect.NotifyError -= OnErrorOccur;
- _echoContrastDetect.NotifyLog -= OnLogWrite;
- _echoContrastDetect.NotifyProcessFinish -= OnPreprocessedDatasUpdate;
- _echoContrastDetect.Dispose();
- _echoContrastDetect = null;
- }
- // 清除耗时显示
- Dispatcher.Invoke(() =>
- {
- TextTestTotalTime.Text = string.Empty;
- TextOrganSegTime.Text = string.Empty;
- UsImage.Source = null;
- });
- // 清除结果显示
- MyImageCanvas.RemoveRoiInCanvas("liver");
- MyImageCanvas.RemoveRoiInCanvas("kidney");
- roisManual.Clear();
- MyImageCanvas.ClearTestResult();
- }
- /// <summary>
- /// 开始处理
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void OnBtnStartProcessClick(object sender, RoutedEventArgs e)
- {
- if (_testType == TestType.None)
- {
- MessageBox.Show("请先选择待测试数据(图片/视频/实时超声数据)!");
- return;
- }
- if (_isManulSelROI)
- {
- if (_imageBuffer == null)
- {
- MessageBox.Show("请重新加载一张图片!");
- return;
- }
- if (roisManual.Count != 2)
- {
- MessageBox.Show("请先选择两个roi框");
- return;
- }
- if (_echoContrastDetect == null)
- {
- _echoContrastDetect = new UsHepatoRenalRatioDetect();
- _echoContrastDetect.NotifyError += OnErrorOccur;
- _echoContrastDetect.NotifyLog += OnLogWrite;
- _echoContrastDetect.NotifyProcessFinish += OnPreprocessedDatasUpdate;
- var rawImage = RawImageShowUtils.BitmapToRawImage(_imageBuffer);
- double ratio = _echoContrastDetect.GetHepatoRenalRatio(rawImage, roisManual[0], roisManual[1]);
- // 更新显示结果
- MyImageCanvas.UpdateTestResults(new HRRDetectResultPerImg(new SegResultPerOrgan[0], new Dictionary<EnumOrgans, Rect>() , ratio));
- }
- }
- else
- {
- if (_echoContrastDetect == null)
- {
- _echoContrastDetect = new UsHepatoRenalRatioDetect();
- _echoContrastDetect.NotifyError += OnErrorOccur;
- _echoContrastDetect.NotifyLog += OnLogWrite;
- _echoContrastDetect.NotifyProcessFinish += OnPreprocessedDatasUpdate;
- _echoContrastDetect.Init(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Networks"), _isCropImag);
- _echoContrastDetect.LoadInferNet();
- }
- _echoContrastDetect.StartProcess();
- // 做单张图片测试
- if (_testType == TestType.ImageTest)
- {
- if (_imageBuffer == null)
- {
- MessageBox.Show("请先选择图片!");
- return;
- }
- DetectImage(_imageBuffer);
- }
- _isEnableDetect = true;
- }
- }
- /// <summary>
- /// 停止处理
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void OnBtnStopProcessClick(object sender, RoutedEventArgs e)
- {
- _isEnableDetect = false;
- if (_echoContrastDetect != null)
- {
- _echoContrastDetect.StopProcess();
- _echoContrastDetect.NotifyError -= OnErrorOccur;
- _echoContrastDetect.NotifyLog -= OnLogWrite;
- _echoContrastDetect.NotifyProcessFinish -= OnPreprocessedDatasUpdate;
- _echoContrastDetect.Dispose();
- _echoContrastDetect = null;
- }
- // 清除耗时显示
- Dispatcher.Invoke(() =>
- {
- TextTestTotalTime.Text = string.Empty;
- TextOrganSegTime.Text = string.Empty;
- UsImage.Source = null;
- });
- // 清除结果显示
- MyImageCanvas.ClearTestResult();
- }
- #endregion
- #region private
- /// <summary>
- /// 刷新列表中的采集卡
- /// </summary>
- private void RefreshComboBoxVideoCards()
- {
- StopPlay();
- ComboBoxCards.Items.Clear();
- var videoDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice);
- foreach (var device in videoDevices)
- {
- ComboBoxCards.Items.Add(device);
- }
- if (ComboBoxCards.Items.Count > 0)
- {
- ComboBoxCards.SelectedIndex = 0;
- }
- }
- /// <summary>
- /// 采集卡采集到了一幅新的图像
- /// </summary>
- private void OnVideoCaptureNewFrame(object sender, NewFrameEventArgs e)
- {
- var bitmap = e.Frame;
- MyImageCanvas.UpdateImage(bitmap);
- // 这里是否要每一帧都塞到探头模块去测?(探头模块中来不及测的会自动忽略)
- //DetectImage(bitmap);
- bitmap.Dispose();
- }
- /// <summary>
- /// 播放视频
- /// </summary>
- private void DoVideoPlay()
- {
- _continuouslyDetecting = true;
- while (!_pausing)
- {
- var currentTickCount = Environment.TickCount;
- Bitmap bitmap;
- lock (_videoReaderLocker)
- {
- bitmap = _videoReader?.ReadVideoFrame(_frameIndex);
- }
- if (bitmap == null)
- {
- break;
- }
- MyImageCanvas.UpdateImage(bitmap);
- if (_isEnableDetect)
- {
- DetectImage(bitmap);
- }
- bitmap.Dispose();
- // 更新显示帧率
- _displayCount++;
- // 最后还剩多少时间sleep
- var detectTimeUsed = Environment.TickCount - currentTickCount;
- var finalSleepTime = _frameIntervalTime - detectTimeUsed;
- if (finalSleepTime < 0)
- {
- finalSleepTime = 0;
- }
- if (_videoPlayEvent.WaitOne(finalSleepTime))
- {
- break;
- }
- // 如果到尾了,就从头开始
- if (_frameIndex >= _frameCount - 2)
- {
- _frameIndex = 0;
- }
- else
- {
- _frameIndex += 2;
- }
- // 更新进度条
- Dispatcher.Invoke(() =>
- {
- ProgressBarVideoPlay.Value = _frameIndex;
- TextFrameIndex.Text = _frameIndex.ToString();
- });
- }
- _continuouslyDetecting = false;
- }
- /// <summary>
- /// 停止播放视频
- /// </summary>
- private void StopPlay()
- {
- _playEvent.Set();
- lock (_videoReaderLocker)
- {
- if (_videoReader != null)
- {
- _videoReader.Close();
- _videoReader.Dispose();
- _videoReader = null;
- }
- }
- if (_videoCapture != null)
- {
- _videoCapture.NewFrame -= OnVideoCaptureNewFrame;
- _videoCapture.SignalToStop();
- _videoCapture = null;
- }
- }
- /// <summary>
- /// 检测一幅图像
- /// </summary>
- /// <param name="image"></param>
- private void DetectImage(Bitmap image)
- {
- var rawImage = RawImageShowUtils.BitmapToRawImage(image);
- _echoContrastDetect.UpdateUsImage(rawImage);
- }
- /// <summary>
- /// 有结果更新
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void OnPreprocessedDatasUpdate(object sender, UsHRRDetectUpdateEvent e)
- {
- if (e.HRRProcessStatus != EnumHRRStatus.HRR_SUCCESSED)
- {
- MessageBox.Show("HRR检测失败!原因是:" + e.HRRProcessStatus.ToString());
- }
- else
- {
- //// 更新显示耗时
- string totalTime = string.Empty;
- string organSegTime = string.Empty;
- if (e.TimeElapsed.ContainsKey(EnumProcessTime.TotalTime))
- {
- totalTime = e.TimeElapsed[EnumProcessTime.TotalTime].ToString();
- }
- if (e.TimeElapsed.ContainsKey(EnumProcessTime.OrganSegProcessTime))
- {
- organSegTime = e.TimeElapsed[EnumProcessTime.OrganSegProcessTime].ToString();
- }
- Dispatcher.Invoke(() =>
- {
- TextTestTotalTime.Text = totalTime;
- TextOrganSegTime.Text = organSegTime;
- });
- // 更新显示结果
- if (e.DetectedResult.SegResults != null)
- {
- MyImageCanvas.UpdateTestResults(e.DetectedResult);
- }
- else
- {
- MyImageCanvas.ClearTestResult();
- }
- if (e.Mask.ByteCount > 0)
- {
- var bitmap = RawImageShowUtils.RawImageToBitmap(e.Mask);
- Dispatcher.Invoke(() =>
- {
- UsImage.Source = RawImageShowUtils.BitmapToBitmapImage(bitmap);
- });
- }
- }
- }
- /// <summary>
- /// 有log要记
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void OnLogWrite(object sender, LogEventArgs e)
- {
- switch (e.LogType)
- {
- case EnumLogType.InfoLog:
- LogHelper.InfoLog(e.Msg, string.Empty);
- break;
- case EnumLogType.WarnLog:
- LogHelper.WarnLog(e.Msg, string.Empty);
- break;
- case EnumLogType.ErrorLog:
- LogHelper.ErrorLog(e.Msg, string.Empty);
- break;
- case EnumLogType.FatalLog:
- LogHelper.FatalLog(e.Msg, string.Empty);
- break;
- }
- }
- /// <summary>
- /// 有错误发生
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void OnErrorOccur(object sender, ErrorEventArgs e)
- {
- MessageBox.Show("发生错误:" + e.GetException());
- }
- #endregion
- /// <summary>
- /// 鼠标左键按下
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void Canvas_MouseLeftBtnDown(object sender, MouseButtonEventArgs e)
- {
- if (_isManulSelROI)
- {
- //清空原始图像上面的路径点
- _contourInOrigImg = new List<Point2D>();
- _drawingPoints = new List<System.Windows.Point>();
- System.Windows.Point mousePosInCanvas = e.GetPosition((IInputElement)sender);
- _mouseStartPos = mousePosInCanvas;
- _drawingPoints = new List<System.Windows.Point>();
- _drawingPoints.Add(mousePosInCanvas);
- _mouseLeftBtnPressed = true;
- }
- }
- /// <summary>
- /// 鼠标左键弹起
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void Canvas_MouseLeftBtnUp(object sender, MouseButtonEventArgs e)
- {
- if (_isManulSelROI)
- {
- _mouseLeftBtnPressed = false;
- System.Windows.Point mousePosInCanvas = e.GetPosition((IInputElement)sender);
- // 记录下在原图上面的起始点
- _mousePosInOrigImg = MyImageCanvas.TransBack(mousePosInCanvas);
- _startPosInOrigImg = MyImageCanvas.TransBack(_mouseStartPos);
- _contourInOrigImg.Add(_startPosInOrigImg);
- _contourInOrigImg.Add(_mousePosInOrigImg);
- // 更新显示
- int x = _startPosInOrigImg.X;
- int y = _startPosInOrigImg.Y;
- int w = _mousePosInOrigImg.X - _startPosInOrigImg.X;
- int h = _mousePosInOrigImg.Y - _startPosInOrigImg.Y;
- roisManual.Add(new Rect(x, y, w, h));
- RoiInfo rect = new RoiInfo(new Rect(x, y, w, h), System.Windows.Media.Brushes.Yellow);
- if (roisManual.Count == 1)
- {
- MyImageCanvas.DrawRoiInCanvas(rect, "liver");
- }
- else if (roisManual.Count == 2)
- {
- MyImageCanvas.DrawRoiInCanvas(rect, "kidney");
- }
- else
- {
- MyImageCanvas.RemoveRoiInCanvas("liver");
- MyImageCanvas.RemoveRoiInCanvas("kidney");
- roisManual.Clear();
- MessageBox.Show("选择超过两个框,清空重选!");
- }
- }
- }
- /// <summary>
- /// 鼠标移动
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void Canvas_MouseMove(object sender, MouseEventArgs e)
- {
- if (_isManulSelROI)
- {
- // 显示鼠标位置
- System.Windows.Point mousePosInCanvas = e.GetPosition((IInputElement)sender);
- //Point2D mousePosInOrigImg = MyImageCanvas.TransBack(mousePosInCanvas);
- // 判断鼠标当前状态
- if (e.LeftButton == MouseButtonState.Pressed)
- {
- }
- else
- {
- if (_mouseLeftBtnPressed)
- {
- Canvas_MouseLeftBtnUp(MyImageCanvas, new MouseButtonEventArgs(e.MouseDevice, e.Timestamp, MouseButton.Left));
- }
- }
- }
- }
- }
- }
|