1
0

MainWindow.xaml.cs 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854
  1. using System;
  2. using System.Windows;
  3. using System.Windows.Threading;
  4. using System.Windows.Media.Imaging;
  5. using System.Threading.Tasks;
  6. using System.Drawing;
  7. using System.Drawing.Imaging;
  8. using Microsoft.Win32;
  9. using System.Threading;
  10. using System.Diagnostics;
  11. using Accord.Video.FFMPEG;
  12. using System.IO;
  13. using System.Runtime.InteropServices;
  14. using Accord.Video.DirectShow;
  15. using Accord.Video;
  16. using AI.Common;
  17. using AI.Common.Log;
  18. using Rect = AI.Common.Rect;
  19. using Rectangle = System.Drawing.Rectangle;
  20. using CameraDataCapture;
  21. using RUSCommon.CameraConfigure;
  22. using ImageShowUtilsLib;
  23. using HumanOrganSegDemo.HumanBodyPartAnalyser;
  24. using HumanOrganSegDemo;
  25. using EnumOrgans = HumanOrganSegDemo.EnumOrgans;
  26. using HumanOrganSegDemo.RoughPositioningAnalyser;
  27. namespace HumanOrganSegmentDemo
  28. {
  29. /// <summary>
  30. /// MainWindow.xaml 的交互逻辑
  31. /// </summary>
  32. public partial class MainWindow : Window
  33. {
  34. private int _doneImageNum = 0;
  35. private volatile bool _isCropped = false;
  36. private Stopwatch _stopWatch = new Stopwatch();
  37. private int _coreCount = Environment.ProcessorCount * 2;
  38. private int _numCPU;
  39. private volatile bool _continuouslyDetecting = false;
  40. private DetectedObject _inferResult = new DetectedObject();
  41. private readonly object _videoReaderLocker = new object();
  42. private ManualResetEvent _videoPlayEvent = new ManualResetEvent(false);
  43. private Thread _videoPlayThread = null;
  44. private VideoFileReader _videoReader;
  45. private volatile bool _readingVideo = false;
  46. private volatile bool _pausing = false;
  47. private volatile int _frameCount = 0;
  48. private volatile int _frameIndex = 0;
  49. private volatile int _frameIntervalTime = 0;
  50. private volatile int _lastDetectTickCount;
  51. private volatile int _fpsStartTickCount;
  52. private volatile int _displayCount = 0;
  53. private ManualResetEvent _playEvent = new ManualResetEvent(false);
  54. private VideoCaptureDevice _cameraCapture;
  55. // 相机图像
  56. private CameraDataCapture.CameraDataCapture _cameraImageCapture;
  57. private bool _isConfirmCameraInfo;// 是否已确认相机信息
  58. private CameraInformation _cameraInfo;
  59. private ICameraPara _cameraPara;
  60. private DateTime _cameraImageReceiveStartTime;
  61. private volatile int _cameraImageReceiveCount = 0;
  62. private EnumOrgans _organInfo;
  63. private volatile bool _isLoadNet = false;
  64. private string _netDir = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Networks");
  65. private IHumanSurfaceOrganAnalyser _humanSurfaceOrganAnalyser;
  66. private IRoughPositioningAnalyser _rooughPositioningAnalyser = new RoughPositioningAnalyser();
  67. private volatile bool _enable = false;
  68. public MainWindow()
  69. {
  70. InitializeComponent();
  71. try
  72. {
  73. _numCPU = Convert.ToInt32(TextBoxCPU.Text);
  74. if (_numCPU <= 0 || _numCPU > _coreCount)
  75. throw new Exception("CPU数目设置错误");
  76. try
  77. {
  78. RefreshComboBoxVideoCards();
  79. // 遍历所有的相机、采集卡
  80. var videoDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice);
  81. foreach (var device in videoDevices)
  82. {
  83. ComboBoxCameras.Items.Add(device);
  84. }
  85. // 相机类型(当前仅支持知微)
  86. ComboBoxCameraType.Items.Add("Intel");
  87. ComboBoxCameraType.Items.Add("知微");
  88. ComboBoxCameraType.Items.Add("单目");
  89. ComboBoxCameraType.SelectedIndex = 0;
  90. }
  91. catch (Exception e)
  92. {
  93. MessageBox.Show("初始化过程中出错:" + e);
  94. }
  95. try
  96. {
  97. // 脏器类型(当前仅支持心脏、肝脏)
  98. ComboBoxOrganType.Items.Add("心脏");
  99. ComboBoxOrganType.Items.Add("肝脏");
  100. ComboBoxOrganType.SelectedIndex = 0;
  101. }
  102. catch (Exception e)
  103. {
  104. MessageBox.Show("初始化过程中出错:" + e);
  105. }
  106. }
  107. catch (Exception excep)
  108. {
  109. MessageBox.Show("CPU数目设置错误,只能为正整数且小于本机CPU线程总数!");
  110. return;
  111. }
  112. }
  113. private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
  114. {
  115. try
  116. {
  117. if (_humanSurfaceOrganAnalyser != null)
  118. {
  119. _humanSurfaceOrganAnalyser.NotifyError -= OnErrorOccur;
  120. _humanSurfaceOrganAnalyser.NotifyLogWrite -= OnLogWrite;
  121. _humanSurfaceOrganAnalyser.NotifyHumanDetectFinish -= OnHumanSurfaceOrganProcessFinish;
  122. _humanSurfaceOrganAnalyser.Dispose();
  123. _humanSurfaceOrganAnalyser = null;
  124. }
  125. }
  126. catch (Exception excep)
  127. {
  128. MessageBox.Show("释放时出错:" + excep);
  129. }
  130. }
  131. private void OnCheckedIsCropped(object sender, RoutedEventArgs e)
  132. {
  133. _isCropped = true;
  134. }
  135. private void OnUncheckedIsCropped(object sender, RoutedEventArgs e)
  136. {
  137. _isCropped = false;
  138. }
  139. private void OnComboBoxCameraTypeSelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
  140. {
  141. switch (ComboBoxCameraType.SelectedIndex)
  142. {
  143. case 0:
  144. _cameraInfo.CameraType = EnumCameraType.CameraRealSense435;
  145. break;
  146. case 1:
  147. _cameraInfo.CameraType = EnumCameraType.CameraZhiSensor;
  148. break;
  149. case 2:
  150. _cameraInfo.CameraType = EnumCameraType.CameraMonocular;
  151. break;
  152. }
  153. }
  154. /// <summary>
  155. /// 加载本地数据
  156. /// </summary>
  157. /// <param name="sender"></param>
  158. /// <param name="e"></param>
  159. private void OnBtnLoadImagClicked(object sender, RoutedEventArgs e)
  160. {
  161. StopPlay();
  162. try
  163. {
  164. OpenFileDialog openFileDialog = new OpenFileDialog
  165. {
  166. Filter = "图片文件|*.png;*.bmp;*.jpg;*.jpeg;*.avi;*.mp4",
  167. Multiselect = true,
  168. Title = "选择一幅或多幅图",
  169. };
  170. if (openFileDialog.ShowDialog() == true)
  171. {
  172. var fileName = openFileDialog.FileName;
  173. var fileExtension = Path.GetExtension(fileName);
  174. if (fileExtension == ".mp4" || fileExtension == ".avi")
  175. {
  176. long frameCount;
  177. lock (_videoReaderLocker)
  178. {
  179. _videoReader = new VideoFileReader();
  180. _videoReader.Open(fileName);
  181. frameCount = _videoReader.FrameCount;
  182. }
  183. _displayCount = 0;
  184. _playEvent.Reset();
  185. if (_humanSurfaceOrganAnalyser == null)
  186. {
  187. _humanSurfaceOrganAnalyser = new HumanSurfaceOrganAnalyser(_organInfo, _numCPU, _netDir);
  188. _humanSurfaceOrganAnalyser.NotifyHumanDetectFinish += OnHumanSurfaceOrganProcessFinish;
  189. _humanSurfaceOrganAnalyser.NotifyError += OnErrorOccur;
  190. _humanSurfaceOrganAnalyser.NotifyLogWrite += OnLogWrite;
  191. _humanSurfaceOrganAnalyser.Enable = true;
  192. }
  193. Task.Run(() =>
  194. {
  195. _continuouslyDetecting = true;
  196. bool breakFlag = false;
  197. while (!_playEvent.WaitOne(1) && !breakFlag)
  198. {
  199. for (var ni = 0; ni < frameCount; ni++)
  200. {
  201. if (_playEvent.WaitOne(1))
  202. {
  203. breakFlag = true;
  204. break;
  205. }
  206. Bitmap bitmap;
  207. lock (_videoReaderLocker)
  208. {
  209. bitmap = _videoReader.ReadVideoFrame(ni);
  210. }
  211. _displayCount++;
  212. _enable = true;
  213. if (_enable)
  214. {
  215. DoImgProcess(bitmap);
  216. }
  217. // 更新显示图像
  218. MyImageCanvas.UpdateImage(bitmap);
  219. bitmap.Dispose();
  220. if (_playEvent.WaitOne(0))
  221. {
  222. breakFlag = true;
  223. break;
  224. }
  225. }
  226. }
  227. _continuouslyDetecting = false;
  228. });
  229. VideoFileReader videoReader = new VideoFileReader();
  230. videoReader.Open(fileName);
  231. int totalCount = (int)videoReader.FrameCount;
  232. videoReader.Close();
  233. for (int ni = 0; ni < totalCount; ni++)
  234. {
  235. videoReader.Open(fileName);
  236. videoReader.Close();
  237. }
  238. videoReader.Dispose();
  239. }
  240. if (fileExtension == ".png" || fileExtension == ".jpg" || fileExtension == ".bmp")
  241. {
  242. var bitmap = new Bitmap(fileName);
  243. if (_humanSurfaceOrganAnalyser == null)
  244. {
  245. _humanSurfaceOrganAnalyser = new HumanSurfaceOrganAnalyser(_organInfo, _numCPU, _netDir);
  246. _humanSurfaceOrganAnalyser.NotifyHumanDetectFinish += OnHumanSurfaceOrganProcessFinish;
  247. _humanSurfaceOrganAnalyser.NotifyError += OnErrorOccur;
  248. _humanSurfaceOrganAnalyser.NotifyLogWrite += OnLogWrite;
  249. _humanSurfaceOrganAnalyser.Enable = true;
  250. }
  251. DoImgProcess(bitmap);
  252. MyImageCanvas.UpdateImage(bitmap);
  253. // 异步测试,如果这里图像dispose了,结果无法show出来,暂时屏蔽,有时间再好好处理,todo
  254. //bitmap.Dispose();
  255. }
  256. }
  257. }
  258. catch (Exception excep)
  259. {
  260. MessageBox.Show("加载测试图片时出错:" + excep);
  261. }
  262. }
  263. private void DoImgProcess(Bitmap bmp)
  264. {
  265. try
  266. {
  267. RawImage rawImg = BitmapToRawImage(bmp);
  268. _humanSurfaceOrganAnalyser.EvaluateOneImage(rawImg);
  269. _doneImageNum += 1;
  270. }
  271. catch (Exception excep)
  272. {
  273. MessageBox.Show("测试图像时出错:" + excep);
  274. }
  275. }
  276. /// <summary>
  277. /// 有检测结果更新
  278. /// </summary>
  279. /// <param name="sender"></param>
  280. /// <param name="e"></param>
  281. private void OnHumanSurfaceOrganProcessFinish(object sender, HumanDetectResultPerImage e)
  282. {
  283. Dispatcher.BeginInvoke(DispatcherPriority.Normal, new Action(() =>
  284. {
  285. ForwardTime.Text = e.TimeElapsed.ToString();
  286. }));
  287. MyImageCanvas.UpdateHumanDetectResults(e);
  288. Point2D[] roughPathPts = _rooughPositioningAnalyser.GetRoughSanPathInPCS(_organInfo, e.ObjectOrientationInPCS, e.ObjectBoundingBox, e.ObjectOrganContours);
  289. MyImageCanvas.UpdateRoughPathResults(roughPathPts);
  290. }
  291. private void StopPlay()
  292. {
  293. _playEvent.Set();
  294. lock (_videoReaderLocker)
  295. {
  296. if (_videoReader != null)
  297. {
  298. _videoReader.Close();
  299. _videoReader.Dispose();
  300. _videoReader = null;
  301. }
  302. }
  303. if (_cameraImageCapture != null)
  304. {
  305. _cameraImageCapture.StopCameraDataAcquire();
  306. _cameraImageCapture.NotifyCameraDataUpdate -= OnCameraDatasUpdate;
  307. _cameraImageCapture.NotifyError -= OnErrorOccur;
  308. _cameraImageCapture.Dispose();
  309. _cameraImageCapture = null;
  310. }
  311. _continuouslyDetecting = false;
  312. }
  313. /// <summary>
  314. /// 打开相机
  315. /// </summary>
  316. /// <param name="sender"></param>
  317. /// <param name="e"></param>
  318. private void OnBtnOpenCameraClick(object sender, RoutedEventArgs e)
  319. {
  320. if (ComboBoxCameraType.SelectedIndex == -1)
  321. {
  322. MessageBox.Show("未选中相机类型");
  323. return;
  324. }
  325. if (ComboBoxCameras.SelectedIndex == -1)
  326. {
  327. MessageBox.Show("未选中相机源(用于拍摄人体)");
  328. return;
  329. }
  330. // 如果已打开相机,则关闭
  331. if (_cameraImageCapture != null)
  332. {
  333. _cameraImageCapture.StopCameraDataAcquire();
  334. _cameraImageCapture.NotifyCameraDataUpdate -= OnCameraDatasUpdate;
  335. _cameraImageCapture.NotifyError -= OnErrorOccur;
  336. _cameraImageCapture.Dispose();
  337. _cameraImageCapture = null;
  338. // 记录一下总共收到多少幅相机图像(主要用来对比一下保存数据输入记录、启用模型监测和不做任何操作时,是否有影响帧率)
  339. var timeSpan = DateTime.Now - _cameraImageReceiveStartTime;
  340. LogHelper.InfoLog("TotalReceivedCameraImageCount:" + _cameraImageReceiveCount + ",TimeSpan:" + timeSpan.TotalMilliseconds + "(ms).", string.Empty);
  341. BtnOpenCameras.Content = "打开相机";
  342. _isConfirmCameraInfo = false;
  343. ComboBoxCameraType.IsEnabled = true;
  344. ComboBoxCameras.IsEnabled = true;
  345. BtnRefreshCameras.IsEnabled = true;
  346. }
  347. else
  348. {
  349. // 检查有没有重复打开同一个视频源
  350. var device = (FilterInfo)ComboBoxCameras.SelectedItem;
  351. var deviceName = device.MonikerString;
  352. _cameraInfo.CameraDeviceName = deviceName;
  353. // 临时设置相机参数
  354. SetCameraPara(_cameraInfo.CameraType);
  355. // todo 根据不同的相机类型选则相应实例
  356. switch (_cameraInfo.CameraType)
  357. {
  358. case EnumCameraType.CameraRealSense435:
  359. break;
  360. case EnumCameraType.CameraZhiSensor:
  361. _cameraInfo.CameraDeviceName = "172.31.0.7";
  362. break;
  363. case EnumCameraType.CameraMonocular:
  364. break;
  365. default:
  366. break;
  367. }
  368. _cameraImageCapture = new CameraDataCapture.CameraDataCapture(_cameraInfo);
  369. _cameraImageCapture.NotifyCameraDataUpdate += OnCameraDatasUpdate;
  370. _cameraImageCapture.NotifyError += OnErrorOccur;
  371. _cameraPara = _cameraImageCapture.GetCameraParaInfo;
  372. _cameraImageCapture.StartCameraDataAcquire();
  373. BtnOpenCameras.Content = "关闭相机";
  374. _isConfirmCameraInfo = true;
  375. _cameraImageReceiveStartTime = DateTime.Now;
  376. ComboBoxCameraType.IsEnabled = false;
  377. ComboBoxCameras.IsEnabled = false;
  378. BtnRefreshCameras.IsEnabled = false;
  379. }
  380. }
  381. /// <summary>
  382. /// 相机数据更新
  383. /// </summary>
  384. /// <param name="sender"></param>
  385. /// <param name="e"></param>
  386. private unsafe void OnCameraDatasUpdate(object sender, CameraDataUpdateEventArgs e)
  387. {
  388. var imgColor = e.UpdateCameraImageData.ImageColor;
  389. var bitmapRGB = RawImageShowUtils.RawImageToBitmap(imgColor.Clone());
  390. if (_enable)
  391. {
  392. DoImgProcess(bitmapRGB);
  393. }
  394. // 更新界面上显示的图片
  395. MyImageCanvas.UpdateImage(bitmapRGB);
  396. bitmapRGB.Dispose();
  397. imgColor.Dispose();
  398. bitmapRGB.Dispose();
  399. }
  400. /// <summary>
  401. /// 被选择的采集卡有变
  402. /// </summary>
  403. /// <param name="sender"></param>
  404. /// <param name="e"></param>
  405. private void OnComboBoxCardsSelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
  406. {
  407. StopPlay();
  408. }
  409. /// <summary>
  410. /// 刷新所有采集卡
  411. /// </summary>
  412. /// <param name="sender"></param>
  413. /// <param name="e"></param>
  414. private void OnRefreshCardsClick(object sender, RoutedEventArgs e)
  415. {
  416. RefreshComboBoxVideoCards();
  417. }
  418. /// <summary>
  419. /// 刷新列表中的采集卡
  420. /// </summary>
  421. private void RefreshComboBoxVideoCards()
  422. {
  423. StopPlay();
  424. ComboBoxCameras.Items.Clear();
  425. var videoDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice);
  426. foreach (var device in videoDevices)
  427. {
  428. ComboBoxCameras.Items.Add(device);
  429. }
  430. if (ComboBoxCameras.Items.Count > 0)
  431. {
  432. ComboBoxCameras.SelectedIndex = 0;
  433. }
  434. }
  435. /// <summary>
  436. /// 设置相机参数
  437. /// </summary>
  438. /// <param name="cameraType"></param>
  439. private void SetCameraPara(EnumCameraType cameraType)
  440. {
  441. switch (cameraType)
  442. {
  443. case EnumCameraType.CameraMonocular:
  444. _cameraInfo.ImageWidth = 640;
  445. _cameraInfo.ImageHeight = 480;
  446. _cameraInfo.CameraIntrinsicParas = new StructCameraMatrix[1];
  447. _cameraInfo.CameraIntrinsicParas[0] = new StructCameraMatrix { Fx = 968.7391609376731f, Cx = 287.327201924683f, Fy = 973.0819774437676f, Cy = 228.3629053369949f };
  448. _cameraInfo.CameraDistortionParas = new StructDistortionCoeffs[1];
  449. _cameraInfo.CameraDistortionParas[0] = new StructDistortionCoeffs
  450. {
  451. K1 = -0.1423318502459511f,
  452. K2 = 0.9421222939220543f,
  453. P1 = -0.00623928181067679f,
  454. P2 = -0.0001133860088364576f,
  455. K3 = -1.764144824643008f
  456. };
  457. //_cameraPara = new CameraPara(_cameraInfo.CameraType, _cameraInfo.CameraIntrinsicParas, _cameraInfo.CameraDistortionParas);
  458. break;
  459. case EnumCameraType.CameraStereo:
  460. _cameraInfo.ImageWidth = 640;
  461. _cameraInfo.ImageHeight = 480;
  462. _cameraInfo.CameraIntrinsicParas = new StructCameraMatrix[2];
  463. _cameraInfo.CameraDistortionParas = new StructDistortionCoeffs[2];
  464. _cameraInfo.CameraIntrinsicParas[0] = new StructCameraMatrix { Fx = 418.20295986882917f, Cx = 331.58936826262072f, Fy = 416.81959360159908f, Cy = 237.67218546829204f };
  465. _cameraInfo.CameraDistortionParas[0] = new StructDistortionCoeffs
  466. {
  467. K1 = -0.054219464530630361f,
  468. K2 = 0.34091210469992994f,
  469. P1 = -0.0015726118762586696f,
  470. P2 = -0.0044293500195294868f,
  471. K3 = -0.54448581741567326f
  472. };
  473. _cameraInfo.CameraIntrinsicParas[1] = new StructCameraMatrix { Fx = 415.80928115541127f, Cx = 317.71374656324372f, Fy = 415.03444466070465f, Cy = 241.40091921800106f };
  474. _cameraInfo.CameraDistortionParas[1] = new StructDistortionCoeffs
  475. {
  476. K1 = -0.10663788123585277f,
  477. K2 = 0.52465203680907246f,
  478. P1 = -0.0035902534296184912f,
  479. P2 = -0.0031625627870525645f,
  480. K3 = -0.81617126606653123f
  481. };
  482. _cameraInfo.CameraExteriorPara = new StructCamExternalParam
  483. {
  484. RVec1 = 0.0032517163600536804f,
  485. RVec2 = -0.014466251153525995f,
  486. RVec3 = -0.0020912953401341617f,
  487. TVec1 = -120.19263575225523f,
  488. TVec2 = 0.37262915065413843f,
  489. TVec3 = -0.41720831397963321f
  490. };
  491. //_cameraPara = new CameraPara(_cameraInfo.CameraType, _cameraInfo.CameraIntrinsicParas, _cameraInfo.CameraDistortionParas);
  492. break;
  493. case EnumCameraType.CameraRealSense435:
  494. _cameraInfo.ImageWidth = 848;
  495. _cameraInfo.ImageHeight = 480;
  496. _cameraInfo.CameraIntrinsicParas = new StructCameraMatrix[1];
  497. //_cameraInfo.CameraIntrinsicParas[0] = new StructCameraMatrix { Fx = 603.784f, Cx = 324.581f, Fy = 602.461f, Cy = 253.871f };
  498. // 848 * 480 下realsense的Cx 有误,已修改
  499. _cameraInfo.CameraIntrinsicParas[0] = new StructCameraMatrix { Fx = 603.784f, Cx = 428.581f, Fy = 602.461f, Cy = 253.871f };
  500. _cameraInfo.CameraDistortionParas = new StructDistortionCoeffs[1];
  501. _cameraInfo.CameraDistortionParas[0] = new StructDistortionCoeffs
  502. {
  503. K1 = 0.0f,
  504. K2 = 0.0f,
  505. P1 = 0.0f,
  506. P2 = 0.0f,
  507. K3 = 0.0f
  508. };
  509. //_cameraPara = new CameraPara(_cameraInfo.CameraType, _cameraInfo.CameraIntrinsicParas, _cameraInfo.CameraDistortionParas);
  510. break;
  511. case EnumCameraType.CameraZhiSensor:
  512. _cameraInfo.ImageWidth = 1280;
  513. _cameraInfo.ImageHeight = 720;
  514. _cameraInfo.CameraIntrinsicParas = new StructCameraMatrix[1];
  515. _cameraInfo.CameraIntrinsicParas[0] = new StructCameraMatrix { Fx = 957.37f, Cx = 660.29f, Fy = 950.03f, Cy = 479.29f };
  516. _cameraInfo.CameraDistortionParas = new StructDistortionCoeffs[1];
  517. _cameraInfo.CameraDistortionParas[0] = new StructDistortionCoeffs
  518. {
  519. K1 = 0.06723f,
  520. K2 = -0.06952f,
  521. P1 = -2.49e-03f,
  522. P2 = 1.24e-03f,
  523. K3 = 0.0f
  524. };
  525. //_cameraPara = new CameraPara(_cameraInfo.CameraType, _cameraInfo.CameraIntrinsicParas, _cameraInfo.CameraDistortionParas);
  526. break;
  527. }
  528. }
  529. /// <summary>
  530. /// /下拉选择要分割的脏器类型
  531. /// </summary>
  532. /// <param name="sender"></param>
  533. /// <param name="e"></param>
  534. private void OnComboBoxOrganTypeSelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
  535. {
  536. switch (ComboBoxOrganType.SelectedIndex)
  537. {
  538. case 0:
  539. _organInfo = EnumOrgans.Heart;
  540. break;
  541. case 1:
  542. _organInfo = EnumOrgans.Liver;
  543. break;
  544. }
  545. }
  546. /// <summary>
  547. /// 开始执行
  548. /// </summary>
  549. /// <param name="sender"></param>
  550. /// <param name="e"></param>
  551. private void OnBtnRunClicked(object sender, RoutedEventArgs e)
  552. {
  553. if (_humanSurfaceOrganAnalyser == null)
  554. {
  555. _humanSurfaceOrganAnalyser = new HumanSurfaceOrganAnalyser(_organInfo, _numCPU, _netDir);
  556. _humanSurfaceOrganAnalyser.NotifyHumanDetectFinish += OnHumanSurfaceOrganProcessFinish;
  557. _humanSurfaceOrganAnalyser.NotifyError += OnErrorOccur;
  558. _humanSurfaceOrganAnalyser.NotifyLogWrite += OnLogWrite;
  559. _humanSurfaceOrganAnalyser.Enable = true;
  560. }
  561. _enable = true;
  562. }
  563. /// <summary>
  564. /// 停止执行
  565. /// </summary>
  566. /// <param name="sender"></param>
  567. /// <param name="e"></param>
  568. private void OnBtnStopClicked(object sender, RoutedEventArgs e)
  569. {
  570. if (_humanSurfaceOrganAnalyser != null)
  571. {
  572. _humanSurfaceOrganAnalyser.NotifyHumanDetectFinish -= OnHumanSurfaceOrganProcessFinish;
  573. _humanSurfaceOrganAnalyser.NotifyError -= OnErrorOccur;
  574. _humanSurfaceOrganAnalyser.NotifyLogWrite -= OnLogWrite;
  575. _humanSurfaceOrganAnalyser.Enable = false;
  576. _humanSurfaceOrganAnalyser.Dispose();
  577. _humanSurfaceOrganAnalyser = null;
  578. }
  579. _enable = false;
  580. MyImageCanvas.Clear();
  581. }
  582. /// <summary>
  583. /// 收到发生了错误的通知
  584. /// </summary>
  585. /// <param name="sender"></param>
  586. /// <param name="e"></param>
  587. private void OnErrorOccur(object sender, ErrorEventArgs e)
  588. {
  589. MessageBox.Show("错误:" + e.GetException());
  590. }
  591. /// <summary>
  592. /// 有log要记
  593. /// </summary>
  594. /// <param name="sender"></param>
  595. /// <param name="e"></param>
  596. private void OnLogWrite(object sender, LogEventArgs e)
  597. {
  598. LogHelper.InfoLog(e.Msg, "");
  599. }
  600. public static void SaveBitmapImageIntoFile(BitmapImage bitmapImage, string filePath)
  601. {
  602. BitmapEncoder encoder = new PngBitmapEncoder();
  603. encoder.Frames.Add(BitmapFrame.Create(bitmapImage));
  604. using (var fileStream = new System.IO.FileStream(filePath, System.IO.FileMode.Create))
  605. {
  606. encoder.Save(fileStream);
  607. }
  608. }
  609. /// <summary>
  610. /// 将System.Drawimg.Bitmap转为RawImage
  611. /// </summary>
  612. /// <param name="image"></param>
  613. /// <param name="keepChannel"></param>
  614. /// <returns></returns>
  615. public static RawImage BitmapToRawImage(Bitmap image, bool keepColorType = false)
  616. {
  617. int width = image.Width;
  618. int height = image.Height;
  619. PixelFormat pixelFormat = image.PixelFormat;
  620. if (pixelFormat != PixelFormat.Format24bppRgb && pixelFormat != PixelFormat.Format32bppArgb && pixelFormat != PixelFormat.Format8bppIndexed)
  621. {
  622. throw new Exception("Unexpected image pixel format:" + pixelFormat);
  623. }
  624. EnumColorType origColorType = EnumColorType.Gray8;
  625. int origBytesPerPixel = 1;
  626. if (pixelFormat == PixelFormat.Format8bppIndexed)
  627. {
  628. origColorType = EnumColorType.Gray8;
  629. origBytesPerPixel = 1;
  630. }
  631. else if (pixelFormat == PixelFormat.Format24bppRgb)
  632. {
  633. // 注意Bitmap里的24bppRgb其实通道顺序是BGR
  634. origColorType = EnumColorType.Bgr;
  635. origBytesPerPixel = 3;
  636. }
  637. else
  638. {
  639. // 注意Bitmap里的32Argb其实通道顺序是Bgra
  640. origColorType = EnumColorType.Bgra;
  641. origBytesPerPixel = 4;
  642. }
  643. EnumColorType dstColorType;
  644. int dstBytesPerPixel;
  645. if (keepColorType)
  646. {
  647. dstColorType = origColorType;
  648. dstBytesPerPixel = origBytesPerPixel;
  649. }
  650. else
  651. {
  652. // 如果没有指定要keepColorType的话,默认都转成BGR
  653. dstColorType = EnumColorType.Bgr;
  654. dstBytesPerPixel = 3;
  655. }
  656. var bmData = image.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.ReadOnly, pixelFormat);
  657. byte[] dstDataArray = new byte[width * height * dstBytesPerPixel];
  658. unsafe
  659. {
  660. int stride = bmData.Stride;
  661. int dstStride = width * dstBytesPerPixel;
  662. IntPtr ptr = bmData.Scan0;
  663. IntPtr ptrH, ptrW;
  664. // 原图和目标图的ColorType相同
  665. if (origColorType == dstColorType)
  666. {
  667. for (int nh = 0; nh < height; nh++)
  668. {
  669. ptrH = IntPtr.Add(ptr, nh * stride);
  670. Marshal.Copy(ptrH, dstDataArray, nh * dstStride, dstStride);
  671. }
  672. }
  673. // 原图和目标图的ColorType不同
  674. else
  675. {
  676. // 如果原图是8位灰度图
  677. if (origColorType == EnumColorType.Gray8)
  678. {
  679. byte gray;
  680. int dstOffset;
  681. for (int nh = 0; nh < height; nh++)
  682. {
  683. ptrH = IntPtr.Add(ptr, nh * stride);
  684. for (int nw = 0; nw < width; nw++)
  685. {
  686. ptrW = IntPtr.Add(ptrH, nw * origBytesPerPixel);
  687. gray = Marshal.ReadByte(ptrW);
  688. dstOffset = nh * dstStride + nw * dstBytesPerPixel;
  689. dstDataArray[dstOffset] = gray;
  690. dstDataArray[dstOffset + 1] = gray;
  691. dstDataArray[dstOffset + 2] = gray;
  692. }
  693. }
  694. if (dstColorType == EnumColorType.Bgra)
  695. {
  696. for (int nh = 0; nh < height; nh++)
  697. {
  698. for (int nw = 0; nw < width; nw++)
  699. {
  700. dstOffset = nh * dstStride + nw * dstBytesPerPixel;
  701. dstDataArray[dstOffset + 3] = 255;
  702. }
  703. }
  704. }
  705. }
  706. // 如果原图是BGR彩色图
  707. if (origColorType == EnumColorType.Bgr)
  708. {
  709. if (dstColorType == EnumColorType.Gray8)
  710. {
  711. byte red, green, blue, gray;
  712. int dstOffset;
  713. for (int nh = 0; nh < height; nh++)
  714. {
  715. ptrH = IntPtr.Add(ptr, nh * stride);
  716. for (int nw = 0; nw < width; nw++)
  717. {
  718. ptrW = IntPtr.Add(ptrH, nw * origBytesPerPixel);
  719. blue = Marshal.ReadByte(ptrW);
  720. ptrW = IntPtr.Add(ptrW, 1);
  721. green = Marshal.ReadByte(ptrW);
  722. ptrW = IntPtr.Add(ptrW, 1);
  723. red = Marshal.ReadByte(ptrW);
  724. gray = RawImage.PixelRGBToGray(red, green, blue);
  725. dstOffset = nh * dstStride + nw * dstBytesPerPixel;
  726. dstDataArray[dstOffset] = gray;
  727. }
  728. }
  729. }
  730. if (dstColorType == EnumColorType.Bgra)
  731. {
  732. int dstOffset;
  733. for (int nh = 0; nh < height; nh++)
  734. {
  735. ptrH = IntPtr.Add(ptr, nh * stride);
  736. for (int nw = 0; nw < width; nw++)
  737. {
  738. ptrW = IntPtr.Add(ptrH, nw * origBytesPerPixel);
  739. dstOffset = nh * dstStride + nw * dstBytesPerPixel;
  740. Marshal.Copy(ptrW, dstDataArray, dstOffset, 3);
  741. dstDataArray[dstOffset + 3] = 255;
  742. }
  743. }
  744. }
  745. }
  746. // 如果原图是BGRA彩色图
  747. if (origColorType == EnumColorType.Bgra)
  748. {
  749. if (dstColorType == EnumColorType.Gray8)
  750. {
  751. byte red, green, blue, gray;
  752. int dstOffset;
  753. for (int nh = 0; nh < height; nh++)
  754. {
  755. ptrH = IntPtr.Add(ptr, nh * stride);
  756. for (int nw = 0; nw < width; nw++)
  757. {
  758. ptrW = IntPtr.Add(ptrH, nw * origBytesPerPixel);
  759. blue = Marshal.ReadByte(ptrW);
  760. ptrW = IntPtr.Add(ptrW, 1);
  761. green = Marshal.ReadByte(ptrW);
  762. ptrW = IntPtr.Add(ptrW, 1);
  763. red = Marshal.ReadByte(ptrW);
  764. gray = RawImage.PixelRGBToGray(red, green, blue);
  765. dstOffset = nh * dstStride + nw * dstBytesPerPixel;
  766. dstDataArray[dstOffset] = gray;
  767. }
  768. }
  769. }
  770. if (dstColorType == EnumColorType.Bgr)
  771. {
  772. int dstOffset;
  773. for (int nh = 0; nh < height; nh++)
  774. {
  775. ptrH = IntPtr.Add(ptr, nh * stride);
  776. for (int nw = 0; nw < width; nw++)
  777. {
  778. ptrW = IntPtr.Add(ptrH, nw * origBytesPerPixel);
  779. dstOffset = nh * dstStride + nw * dstBytesPerPixel;
  780. Marshal.Copy(ptrW, dstDataArray, dstOffset, 3);
  781. }
  782. }
  783. }
  784. }
  785. }
  786. }
  787. image.UnlockBits(bmData);
  788. return new RawImage(dstDataArray, width, height, dstColorType);
  789. }
  790. }
  791. }