123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854 |
- using System;
- using System.Windows;
- using System.Windows.Threading;
- using System.Windows.Media.Imaging;
- using System.Threading.Tasks;
- using System.Drawing;
- using System.Drawing.Imaging;
- using Microsoft.Win32;
- using System.Threading;
- using System.Diagnostics;
- using Accord.Video.FFMPEG;
- using System.IO;
- using System.Runtime.InteropServices;
- using Accord.Video.DirectShow;
- using Accord.Video;
- using AI.Common;
- using AI.Common.Log;
- using Rect = AI.Common.Rect;
- using Rectangle = System.Drawing.Rectangle;
- using CameraDataCapture;
- using RUSCommon.CameraConfigure;
- using ImageShowUtilsLib;
- using HumanOrganSegDemo.HumanBodyPartAnalyser;
- using HumanOrganSegDemo;
- using EnumOrgans = HumanOrganSegDemo.EnumOrgans;
- using HumanOrganSegDemo.RoughPositioningAnalyser;
- namespace HumanOrganSegmentDemo
- {
- /// <summary>
- /// MainWindow.xaml 的交互逻辑
- /// </summary>
- public partial class MainWindow : Window
- {
- private int _doneImageNum = 0;
- private volatile bool _isCropped = false;
- private Stopwatch _stopWatch = new Stopwatch();
- private int _coreCount = Environment.ProcessorCount * 2;
- private int _numCPU;
- private volatile bool _continuouslyDetecting = false;
- private DetectedObject _inferResult = new DetectedObject();
- private readonly object _videoReaderLocker = new object();
- private ManualResetEvent _videoPlayEvent = new ManualResetEvent(false);
- private Thread _videoPlayThread = null;
- private VideoFileReader _videoReader;
- private volatile bool _readingVideo = false;
- private volatile bool _pausing = false;
- private volatile int _frameCount = 0;
- private volatile int _frameIndex = 0;
- private volatile int _frameIntervalTime = 0;
- private volatile int _lastDetectTickCount;
- private volatile int _fpsStartTickCount;
- private volatile int _displayCount = 0;
- private ManualResetEvent _playEvent = new ManualResetEvent(false);
- private VideoCaptureDevice _cameraCapture;
- // 相机图像
- private CameraDataCapture.CameraDataCapture _cameraImageCapture;
- private bool _isConfirmCameraInfo;// 是否已确认相机信息
- private CameraInformation _cameraInfo;
- private ICameraPara _cameraPara;
- private DateTime _cameraImageReceiveStartTime;
- private volatile int _cameraImageReceiveCount = 0;
- private EnumOrgans _organInfo;
- private volatile bool _isLoadNet = false;
- private string _netDir = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Networks");
- private IHumanSurfaceOrganAnalyser _humanSurfaceOrganAnalyser;
- private IRoughPositioningAnalyser _rooughPositioningAnalyser = new RoughPositioningAnalyser();
- private volatile bool _enable = false;
- public MainWindow()
- {
- InitializeComponent();
- try
- {
- _numCPU = Convert.ToInt32(TextBoxCPU.Text);
- if (_numCPU <= 0 || _numCPU > _coreCount)
- throw new Exception("CPU数目设置错误");
- try
- {
- RefreshComboBoxVideoCards();
- // 遍历所有的相机、采集卡
- var videoDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice);
- foreach (var device in videoDevices)
- {
- ComboBoxCameras.Items.Add(device);
- }
- // 相机类型(当前仅支持知微)
- ComboBoxCameraType.Items.Add("Intel");
- ComboBoxCameraType.Items.Add("知微");
- ComboBoxCameraType.Items.Add("单目");
- ComboBoxCameraType.SelectedIndex = 0;
- }
- catch (Exception e)
- {
- MessageBox.Show("初始化过程中出错:" + e);
- }
- try
- {
- // 脏器类型(当前仅支持心脏、肝脏)
- ComboBoxOrganType.Items.Add("心脏");
- ComboBoxOrganType.Items.Add("肝脏");
- ComboBoxOrganType.SelectedIndex = 0;
- }
- catch (Exception e)
- {
- MessageBox.Show("初始化过程中出错:" + e);
- }
- }
- catch (Exception excep)
- {
- MessageBox.Show("CPU数目设置错误,只能为正整数且小于本机CPU线程总数!");
- return;
- }
- }
- private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
- {
- try
- {
- if (_humanSurfaceOrganAnalyser != null)
- {
- _humanSurfaceOrganAnalyser.NotifyError -= OnErrorOccur;
- _humanSurfaceOrganAnalyser.NotifyLogWrite -= OnLogWrite;
- _humanSurfaceOrganAnalyser.NotifyHumanDetectFinish -= OnHumanSurfaceOrganProcessFinish;
- _humanSurfaceOrganAnalyser.Dispose();
- _humanSurfaceOrganAnalyser = null;
- }
- }
- catch (Exception excep)
- {
- MessageBox.Show("释放时出错:" + excep);
- }
- }
- private void OnCheckedIsCropped(object sender, RoutedEventArgs e)
- {
- _isCropped = true;
- }
- private void OnUncheckedIsCropped(object sender, RoutedEventArgs e)
- {
- _isCropped = false;
- }
- private void OnComboBoxCameraTypeSelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
- {
- switch (ComboBoxCameraType.SelectedIndex)
- {
- case 0:
- _cameraInfo.CameraType = EnumCameraType.CameraRealSense435;
- break;
- case 1:
- _cameraInfo.CameraType = EnumCameraType.CameraZhiSensor;
- break;
- case 2:
- _cameraInfo.CameraType = EnumCameraType.CameraMonocular;
- break;
- }
- }
- /// <summary>
- /// 加载本地数据
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void OnBtnLoadImagClicked(object sender, RoutedEventArgs e)
- {
- StopPlay();
- try
- {
- OpenFileDialog openFileDialog = new OpenFileDialog
- {
- Filter = "图片文件|*.png;*.bmp;*.jpg;*.jpeg;*.avi;*.mp4",
- Multiselect = true,
- Title = "选择一幅或多幅图",
- };
- if (openFileDialog.ShowDialog() == true)
- {
- var fileName = openFileDialog.FileName;
- var fileExtension = Path.GetExtension(fileName);
- if (fileExtension == ".mp4" || fileExtension == ".avi")
- {
- long frameCount;
- lock (_videoReaderLocker)
- {
- _videoReader = new VideoFileReader();
- _videoReader.Open(fileName);
- frameCount = _videoReader.FrameCount;
- }
- _displayCount = 0;
- _playEvent.Reset();
- if (_humanSurfaceOrganAnalyser == null)
- {
- _humanSurfaceOrganAnalyser = new HumanSurfaceOrganAnalyser(_organInfo, _numCPU, _netDir);
- _humanSurfaceOrganAnalyser.NotifyHumanDetectFinish += OnHumanSurfaceOrganProcessFinish;
- _humanSurfaceOrganAnalyser.NotifyError += OnErrorOccur;
- _humanSurfaceOrganAnalyser.NotifyLogWrite += OnLogWrite;
- _humanSurfaceOrganAnalyser.Enable = true;
- }
- Task.Run(() =>
- {
- _continuouslyDetecting = true;
- bool breakFlag = false;
- while (!_playEvent.WaitOne(1) && !breakFlag)
- {
- for (var ni = 0; ni < frameCount; ni++)
- {
- if (_playEvent.WaitOne(1))
- {
- breakFlag = true;
- break;
- }
- Bitmap bitmap;
- lock (_videoReaderLocker)
- {
- bitmap = _videoReader.ReadVideoFrame(ni);
- }
- _displayCount++;
- _enable = true;
- if (_enable)
- {
- DoImgProcess(bitmap);
- }
- // 更新显示图像
- MyImageCanvas.UpdateImage(bitmap);
- bitmap.Dispose();
- if (_playEvent.WaitOne(0))
- {
- breakFlag = true;
- break;
- }
- }
- }
- _continuouslyDetecting = false;
- });
- VideoFileReader videoReader = new VideoFileReader();
- videoReader.Open(fileName);
- int totalCount = (int)videoReader.FrameCount;
- videoReader.Close();
- for (int ni = 0; ni < totalCount; ni++)
- {
- videoReader.Open(fileName);
- videoReader.Close();
- }
- videoReader.Dispose();
- }
- if (fileExtension == ".png" || fileExtension == ".jpg" || fileExtension == ".bmp")
- {
- var bitmap = new Bitmap(fileName);
- if (_humanSurfaceOrganAnalyser == null)
- {
- _humanSurfaceOrganAnalyser = new HumanSurfaceOrganAnalyser(_organInfo, _numCPU, _netDir);
- _humanSurfaceOrganAnalyser.NotifyHumanDetectFinish += OnHumanSurfaceOrganProcessFinish;
- _humanSurfaceOrganAnalyser.NotifyError += OnErrorOccur;
- _humanSurfaceOrganAnalyser.NotifyLogWrite += OnLogWrite;
- _humanSurfaceOrganAnalyser.Enable = true;
- }
- DoImgProcess(bitmap);
- MyImageCanvas.UpdateImage(bitmap);
- // 异步测试,如果这里图像dispose了,结果无法show出来,暂时屏蔽,有时间再好好处理,todo
- //bitmap.Dispose();
- }
- }
- }
- catch (Exception excep)
- {
- MessageBox.Show("加载测试图片时出错:" + excep);
- }
- }
- private void DoImgProcess(Bitmap bmp)
- {
- try
- {
- RawImage rawImg = BitmapToRawImage(bmp);
- _humanSurfaceOrganAnalyser.EvaluateOneImage(rawImg);
- _doneImageNum += 1;
- }
- catch (Exception excep)
- {
- MessageBox.Show("测试图像时出错:" + excep);
- }
- }
- /// <summary>
- /// 有检测结果更新
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void OnHumanSurfaceOrganProcessFinish(object sender, HumanDetectResultPerImage e)
- {
- Dispatcher.BeginInvoke(DispatcherPriority.Normal, new Action(() =>
- {
- ForwardTime.Text = e.TimeElapsed.ToString();
- }));
- MyImageCanvas.UpdateHumanDetectResults(e);
- Point2D[] roughPathPts = _rooughPositioningAnalyser.GetRoughSanPathInPCS(_organInfo, e.ObjectOrientationInPCS, e.ObjectBoundingBox, e.ObjectOrganContours);
- MyImageCanvas.UpdateRoughPathResults(roughPathPts);
- }
- private void StopPlay()
- {
- _playEvent.Set();
- lock (_videoReaderLocker)
- {
- if (_videoReader != null)
- {
- _videoReader.Close();
- _videoReader.Dispose();
- _videoReader = null;
- }
- }
- if (_cameraImageCapture != null)
- {
- _cameraImageCapture.StopCameraDataAcquire();
- _cameraImageCapture.NotifyCameraDataUpdate -= OnCameraDatasUpdate;
- _cameraImageCapture.NotifyError -= OnErrorOccur;
- _cameraImageCapture.Dispose();
- _cameraImageCapture = null;
- }
- _continuouslyDetecting = false;
- }
- /// <summary>
- /// 打开相机
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void OnBtnOpenCameraClick(object sender, RoutedEventArgs e)
- {
- if (ComboBoxCameraType.SelectedIndex == -1)
- {
- MessageBox.Show("未选中相机类型");
- return;
- }
- if (ComboBoxCameras.SelectedIndex == -1)
- {
- MessageBox.Show("未选中相机源(用于拍摄人体)");
- return;
- }
- // 如果已打开相机,则关闭
- if (_cameraImageCapture != null)
- {
- _cameraImageCapture.StopCameraDataAcquire();
- _cameraImageCapture.NotifyCameraDataUpdate -= OnCameraDatasUpdate;
- _cameraImageCapture.NotifyError -= OnErrorOccur;
- _cameraImageCapture.Dispose();
- _cameraImageCapture = null;
- // 记录一下总共收到多少幅相机图像(主要用来对比一下保存数据输入记录、启用模型监测和不做任何操作时,是否有影响帧率)
- var timeSpan = DateTime.Now - _cameraImageReceiveStartTime;
- LogHelper.InfoLog("TotalReceivedCameraImageCount:" + _cameraImageReceiveCount + ",TimeSpan:" + timeSpan.TotalMilliseconds + "(ms).", string.Empty);
- BtnOpenCameras.Content = "打开相机";
- _isConfirmCameraInfo = false;
- ComboBoxCameraType.IsEnabled = true;
- ComboBoxCameras.IsEnabled = true;
- BtnRefreshCameras.IsEnabled = true;
- }
- else
- {
- // 检查有没有重复打开同一个视频源
- var device = (FilterInfo)ComboBoxCameras.SelectedItem;
- var deviceName = device.MonikerString;
- _cameraInfo.CameraDeviceName = deviceName;
- // 临时设置相机参数
- SetCameraPara(_cameraInfo.CameraType);
- // todo 根据不同的相机类型选则相应实例
- switch (_cameraInfo.CameraType)
- {
- case EnumCameraType.CameraRealSense435:
- break;
- case EnumCameraType.CameraZhiSensor:
- _cameraInfo.CameraDeviceName = "172.31.0.7";
- break;
- case EnumCameraType.CameraMonocular:
- break;
- default:
- break;
- }
- _cameraImageCapture = new CameraDataCapture.CameraDataCapture(_cameraInfo);
- _cameraImageCapture.NotifyCameraDataUpdate += OnCameraDatasUpdate;
- _cameraImageCapture.NotifyError += OnErrorOccur;
- _cameraPara = _cameraImageCapture.GetCameraParaInfo;
- _cameraImageCapture.StartCameraDataAcquire();
- BtnOpenCameras.Content = "关闭相机";
- _isConfirmCameraInfo = true;
- _cameraImageReceiveStartTime = DateTime.Now;
- ComboBoxCameraType.IsEnabled = false;
- ComboBoxCameras.IsEnabled = false;
- BtnRefreshCameras.IsEnabled = false;
- }
- }
- /// <summary>
- /// 相机数据更新
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private unsafe void OnCameraDatasUpdate(object sender, CameraDataUpdateEventArgs e)
- {
- var imgColor = e.UpdateCameraImageData.ImageColor;
- var bitmapRGB = RawImageShowUtils.RawImageToBitmap(imgColor.Clone());
- if (_enable)
- {
- DoImgProcess(bitmapRGB);
- }
- // 更新界面上显示的图片
- MyImageCanvas.UpdateImage(bitmapRGB);
- bitmapRGB.Dispose();
- imgColor.Dispose();
- bitmapRGB.Dispose();
- }
- /// <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>
- private void RefreshComboBoxVideoCards()
- {
- StopPlay();
- ComboBoxCameras.Items.Clear();
- var videoDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice);
- foreach (var device in videoDevices)
- {
- ComboBoxCameras.Items.Add(device);
- }
- if (ComboBoxCameras.Items.Count > 0)
- {
- ComboBoxCameras.SelectedIndex = 0;
- }
- }
- /// <summary>
- /// 设置相机参数
- /// </summary>
- /// <param name="cameraType"></param>
- private void SetCameraPara(EnumCameraType cameraType)
- {
- switch (cameraType)
- {
- case EnumCameraType.CameraMonocular:
- _cameraInfo.ImageWidth = 640;
- _cameraInfo.ImageHeight = 480;
- _cameraInfo.CameraIntrinsicParas = new StructCameraMatrix[1];
- _cameraInfo.CameraIntrinsicParas[0] = new StructCameraMatrix { Fx = 968.7391609376731f, Cx = 287.327201924683f, Fy = 973.0819774437676f, Cy = 228.3629053369949f };
- _cameraInfo.CameraDistortionParas = new StructDistortionCoeffs[1];
- _cameraInfo.CameraDistortionParas[0] = new StructDistortionCoeffs
- {
- K1 = -0.1423318502459511f,
- K2 = 0.9421222939220543f,
- P1 = -0.00623928181067679f,
- P2 = -0.0001133860088364576f,
- K3 = -1.764144824643008f
- };
- //_cameraPara = new CameraPara(_cameraInfo.CameraType, _cameraInfo.CameraIntrinsicParas, _cameraInfo.CameraDistortionParas);
- break;
- case EnumCameraType.CameraStereo:
- _cameraInfo.ImageWidth = 640;
- _cameraInfo.ImageHeight = 480;
- _cameraInfo.CameraIntrinsicParas = new StructCameraMatrix[2];
- _cameraInfo.CameraDistortionParas = new StructDistortionCoeffs[2];
- _cameraInfo.CameraIntrinsicParas[0] = new StructCameraMatrix { Fx = 418.20295986882917f, Cx = 331.58936826262072f, Fy = 416.81959360159908f, Cy = 237.67218546829204f };
- _cameraInfo.CameraDistortionParas[0] = new StructDistortionCoeffs
- {
- K1 = -0.054219464530630361f,
- K2 = 0.34091210469992994f,
- P1 = -0.0015726118762586696f,
- P2 = -0.0044293500195294868f,
- K3 = -0.54448581741567326f
- };
- _cameraInfo.CameraIntrinsicParas[1] = new StructCameraMatrix { Fx = 415.80928115541127f, Cx = 317.71374656324372f, Fy = 415.03444466070465f, Cy = 241.40091921800106f };
- _cameraInfo.CameraDistortionParas[1] = new StructDistortionCoeffs
- {
- K1 = -0.10663788123585277f,
- K2 = 0.52465203680907246f,
- P1 = -0.0035902534296184912f,
- P2 = -0.0031625627870525645f,
- K3 = -0.81617126606653123f
- };
- _cameraInfo.CameraExteriorPara = new StructCamExternalParam
- {
- RVec1 = 0.0032517163600536804f,
- RVec2 = -0.014466251153525995f,
- RVec3 = -0.0020912953401341617f,
- TVec1 = -120.19263575225523f,
- TVec2 = 0.37262915065413843f,
- TVec3 = -0.41720831397963321f
- };
- //_cameraPara = new CameraPara(_cameraInfo.CameraType, _cameraInfo.CameraIntrinsicParas, _cameraInfo.CameraDistortionParas);
- break;
- case EnumCameraType.CameraRealSense435:
- _cameraInfo.ImageWidth = 848;
- _cameraInfo.ImageHeight = 480;
- _cameraInfo.CameraIntrinsicParas = new StructCameraMatrix[1];
- //_cameraInfo.CameraIntrinsicParas[0] = new StructCameraMatrix { Fx = 603.784f, Cx = 324.581f, Fy = 602.461f, Cy = 253.871f };
- // 848 * 480 下realsense的Cx 有误,已修改
- _cameraInfo.CameraIntrinsicParas[0] = new StructCameraMatrix { Fx = 603.784f, Cx = 428.581f, Fy = 602.461f, Cy = 253.871f };
- _cameraInfo.CameraDistortionParas = new StructDistortionCoeffs[1];
- _cameraInfo.CameraDistortionParas[0] = new StructDistortionCoeffs
- {
- K1 = 0.0f,
- K2 = 0.0f,
- P1 = 0.0f,
- P2 = 0.0f,
- K3 = 0.0f
- };
- //_cameraPara = new CameraPara(_cameraInfo.CameraType, _cameraInfo.CameraIntrinsicParas, _cameraInfo.CameraDistortionParas);
- break;
- case EnumCameraType.CameraZhiSensor:
- _cameraInfo.ImageWidth = 1280;
- _cameraInfo.ImageHeight = 720;
- _cameraInfo.CameraIntrinsicParas = new StructCameraMatrix[1];
- _cameraInfo.CameraIntrinsicParas[0] = new StructCameraMatrix { Fx = 957.37f, Cx = 660.29f, Fy = 950.03f, Cy = 479.29f };
- _cameraInfo.CameraDistortionParas = new StructDistortionCoeffs[1];
- _cameraInfo.CameraDistortionParas[0] = new StructDistortionCoeffs
- {
- K1 = 0.06723f,
- K2 = -0.06952f,
- P1 = -2.49e-03f,
- P2 = 1.24e-03f,
- K3 = 0.0f
- };
- //_cameraPara = new CameraPara(_cameraInfo.CameraType, _cameraInfo.CameraIntrinsicParas, _cameraInfo.CameraDistortionParas);
- break;
- }
- }
- /// <summary>
- /// /下拉选择要分割的脏器类型
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void OnComboBoxOrganTypeSelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
- {
- switch (ComboBoxOrganType.SelectedIndex)
- {
- case 0:
- _organInfo = EnumOrgans.Heart;
- break;
- case 1:
- _organInfo = EnumOrgans.Liver;
- break;
- }
- }
- /// <summary>
- /// 开始执行
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void OnBtnRunClicked(object sender, RoutedEventArgs e)
- {
- if (_humanSurfaceOrganAnalyser == null)
- {
- _humanSurfaceOrganAnalyser = new HumanSurfaceOrganAnalyser(_organInfo, _numCPU, _netDir);
- _humanSurfaceOrganAnalyser.NotifyHumanDetectFinish += OnHumanSurfaceOrganProcessFinish;
- _humanSurfaceOrganAnalyser.NotifyError += OnErrorOccur;
- _humanSurfaceOrganAnalyser.NotifyLogWrite += OnLogWrite;
- _humanSurfaceOrganAnalyser.Enable = true;
- }
- _enable = true;
- }
- /// <summary>
- /// 停止执行
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void OnBtnStopClicked(object sender, RoutedEventArgs e)
- {
- if (_humanSurfaceOrganAnalyser != null)
- {
- _humanSurfaceOrganAnalyser.NotifyHumanDetectFinish -= OnHumanSurfaceOrganProcessFinish;
- _humanSurfaceOrganAnalyser.NotifyError -= OnErrorOccur;
- _humanSurfaceOrganAnalyser.NotifyLogWrite -= OnLogWrite;
- _humanSurfaceOrganAnalyser.Enable = false;
- _humanSurfaceOrganAnalyser.Dispose();
- _humanSurfaceOrganAnalyser = null;
- }
- _enable = false;
- MyImageCanvas.Clear();
- }
- /// <summary>
- /// 收到发生了错误的通知
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void OnErrorOccur(object sender, ErrorEventArgs e)
- {
- MessageBox.Show("错误:" + e.GetException());
- }
- /// <summary>
- /// 有log要记
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void OnLogWrite(object sender, LogEventArgs e)
- {
- LogHelper.InfoLog(e.Msg, "");
- }
- public static void SaveBitmapImageIntoFile(BitmapImage bitmapImage, string filePath)
- {
- BitmapEncoder encoder = new PngBitmapEncoder();
- encoder.Frames.Add(BitmapFrame.Create(bitmapImage));
- using (var fileStream = new System.IO.FileStream(filePath, System.IO.FileMode.Create))
- {
- encoder.Save(fileStream);
- }
- }
- /// <summary>
- /// 将System.Drawimg.Bitmap转为RawImage
- /// </summary>
- /// <param name="image"></param>
- /// <param name="keepChannel"></param>
- /// <returns></returns>
- public static RawImage BitmapToRawImage(Bitmap image, bool keepColorType = false)
- {
- int width = image.Width;
- int height = image.Height;
- PixelFormat pixelFormat = image.PixelFormat;
- if (pixelFormat != PixelFormat.Format24bppRgb && pixelFormat != PixelFormat.Format32bppArgb && pixelFormat != PixelFormat.Format8bppIndexed)
- {
- throw new Exception("Unexpected image pixel format:" + pixelFormat);
- }
- EnumColorType origColorType = EnumColorType.Gray8;
- int origBytesPerPixel = 1;
- if (pixelFormat == PixelFormat.Format8bppIndexed)
- {
- origColorType = EnumColorType.Gray8;
- origBytesPerPixel = 1;
- }
- else if (pixelFormat == PixelFormat.Format24bppRgb)
- {
- // 注意Bitmap里的24bppRgb其实通道顺序是BGR
- origColorType = EnumColorType.Bgr;
- origBytesPerPixel = 3;
- }
- else
- {
- // 注意Bitmap里的32Argb其实通道顺序是Bgra
- origColorType = EnumColorType.Bgra;
- origBytesPerPixel = 4;
- }
- EnumColorType dstColorType;
- int dstBytesPerPixel;
- if (keepColorType)
- {
- dstColorType = origColorType;
- dstBytesPerPixel = origBytesPerPixel;
- }
- else
- {
- // 如果没有指定要keepColorType的话,默认都转成BGR
- dstColorType = EnumColorType.Bgr;
- dstBytesPerPixel = 3;
- }
- var bmData = image.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.ReadOnly, pixelFormat);
- byte[] dstDataArray = new byte[width * height * dstBytesPerPixel];
- unsafe
- {
- int stride = bmData.Stride;
- int dstStride = width * dstBytesPerPixel;
- IntPtr ptr = bmData.Scan0;
- IntPtr ptrH, ptrW;
- // 原图和目标图的ColorType相同
- if (origColorType == dstColorType)
- {
- for (int nh = 0; nh < height; nh++)
- {
- ptrH = IntPtr.Add(ptr, nh * stride);
- Marshal.Copy(ptrH, dstDataArray, nh * dstStride, dstStride);
- }
- }
- // 原图和目标图的ColorType不同
- else
- {
- // 如果原图是8位灰度图
- if (origColorType == EnumColorType.Gray8)
- {
- byte gray;
- int dstOffset;
- for (int nh = 0; nh < height; nh++)
- {
- ptrH = IntPtr.Add(ptr, nh * stride);
- for (int nw = 0; nw < width; nw++)
- {
- ptrW = IntPtr.Add(ptrH, nw * origBytesPerPixel);
- gray = Marshal.ReadByte(ptrW);
- dstOffset = nh * dstStride + nw * dstBytesPerPixel;
- dstDataArray[dstOffset] = gray;
- dstDataArray[dstOffset + 1] = gray;
- dstDataArray[dstOffset + 2] = gray;
- }
- }
- if (dstColorType == EnumColorType.Bgra)
- {
- for (int nh = 0; nh < height; nh++)
- {
- for (int nw = 0; nw < width; nw++)
- {
- dstOffset = nh * dstStride + nw * dstBytesPerPixel;
- dstDataArray[dstOffset + 3] = 255;
- }
- }
- }
- }
- // 如果原图是BGR彩色图
- if (origColorType == EnumColorType.Bgr)
- {
- if (dstColorType == EnumColorType.Gray8)
- {
- byte red, green, blue, gray;
- int dstOffset;
- for (int nh = 0; nh < height; nh++)
- {
- ptrH = IntPtr.Add(ptr, nh * stride);
- for (int nw = 0; nw < width; nw++)
- {
- ptrW = IntPtr.Add(ptrH, nw * origBytesPerPixel);
- blue = Marshal.ReadByte(ptrW);
- ptrW = IntPtr.Add(ptrW, 1);
- green = Marshal.ReadByte(ptrW);
- ptrW = IntPtr.Add(ptrW, 1);
- red = Marshal.ReadByte(ptrW);
- gray = RawImage.PixelRGBToGray(red, green, blue);
- dstOffset = nh * dstStride + nw * dstBytesPerPixel;
- dstDataArray[dstOffset] = gray;
- }
- }
- }
- if (dstColorType == EnumColorType.Bgra)
- {
- int dstOffset;
- for (int nh = 0; nh < height; nh++)
- {
- ptrH = IntPtr.Add(ptr, nh * stride);
- for (int nw = 0; nw < width; nw++)
- {
- ptrW = IntPtr.Add(ptrH, nw * origBytesPerPixel);
- dstOffset = nh * dstStride + nw * dstBytesPerPixel;
- Marshal.Copy(ptrW, dstDataArray, dstOffset, 3);
- dstDataArray[dstOffset + 3] = 255;
- }
- }
- }
- }
- // 如果原图是BGRA彩色图
- if (origColorType == EnumColorType.Bgra)
- {
- if (dstColorType == EnumColorType.Gray8)
- {
- byte red, green, blue, gray;
- int dstOffset;
- for (int nh = 0; nh < height; nh++)
- {
- ptrH = IntPtr.Add(ptr, nh * stride);
- for (int nw = 0; nw < width; nw++)
- {
- ptrW = IntPtr.Add(ptrH, nw * origBytesPerPixel);
- blue = Marshal.ReadByte(ptrW);
- ptrW = IntPtr.Add(ptrW, 1);
- green = Marshal.ReadByte(ptrW);
- ptrW = IntPtr.Add(ptrW, 1);
- red = Marshal.ReadByte(ptrW);
- gray = RawImage.PixelRGBToGray(red, green, blue);
- dstOffset = nh * dstStride + nw * dstBytesPerPixel;
- dstDataArray[dstOffset] = gray;
- }
- }
- }
- if (dstColorType == EnumColorType.Bgr)
- {
- int dstOffset;
- for (int nh = 0; nh < height; nh++)
- {
- ptrH = IntPtr.Add(ptr, nh * stride);
- for (int nw = 0; nw < width; nw++)
- {
- ptrW = IntPtr.Add(ptrH, nw * origBytesPerPixel);
- dstOffset = nh * dstStride + nw * dstBytesPerPixel;
- Marshal.Copy(ptrW, dstDataArray, dstOffset, 3);
- }
- }
- }
- }
- }
- }
- image.UnlockBits(bmData);
- return new RawImage(dstDataArray, width, height, dstColorType);
- }
- }
- }
|