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 { /// /// MainWindow.xaml 的交互逻辑 /// 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; } } /// /// 加载本地数据 /// /// /// 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); } } /// /// 有检测结果更新 /// /// /// 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; } /// /// 打开相机 /// /// /// 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; } } /// /// 相机数据更新 /// /// /// 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(); } /// /// 被选择的采集卡有变 /// /// /// private void OnComboBoxCardsSelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e) { StopPlay(); } /// /// 刷新所有采集卡 /// /// /// private void OnRefreshCardsClick(object sender, RoutedEventArgs e) { RefreshComboBoxVideoCards(); } /// /// 刷新列表中的采集卡 /// 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; } } /// /// 设置相机参数 /// /// 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; } } /// /// /下拉选择要分割的脏器类型 /// /// /// 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; } } /// /// 开始执行 /// /// /// 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; } /// /// 停止执行 /// /// /// 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(); } /// /// 收到发生了错误的通知 /// /// /// private void OnErrorOccur(object sender, ErrorEventArgs e) { MessageBox.Show("错误:" + e.GetException()); } /// /// 有log要记 /// /// /// 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); } } /// /// 将System.Drawimg.Bitmap转为RawImage /// /// /// /// 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); } } }