using System.Runtime.InteropServices; namespace YOLODetectProcessLib { /// /// 裁取超声图像区域的辅助类 /// public class UsImageRegionSegUtils { #region dll import /// /// 调用CvCore.dll裁图 /// /// /// /// 左,上,宽,高 /// [DllImport(@"CvCropUltImgRegion.dll", CallingConvention = CallingConvention.Cdecl)] [return: MarshalAs(UnmanagedType.I1)] public static extern bool CropImage(byte[] scrImgData, int[] imgInfoIn, int[] rectInfoOut); #endregion #region public public static bool CropWithCvCore(IImage image, out Rect rect) { if (image.Channel != 3 && image.Channel != 4) { rect = new Rect(0, 0, image.Width, image.Height); return false; } int[] imgInfo = new int[4]; imgInfo[0] = image.Width; imgInfo[1] = image.Height; imgInfo[2] = image.Stride; imgInfo[3] = image.Channel; int[] rectInfo = new int[4]; if (!CropImage(image.DataBuffer, imgInfo, rectInfo)) { rect = new Rect(0, 0, image.Width, image.Height); return false; } int rectX = rectInfo[0]; int rectY = rectInfo[1]; int rectWidth = rectInfo[2]; int rectHeight = rectInfo[3]; if ((rectX < 0) || (rectY < 0) || (rectWidth <= 0) || (rectHeight <= 0) || (rectX + rectWidth >= image.Width) || (rectY + rectHeight >= image.Height)) { rect = new Rect(0, 0, image.Width, image.Height); return false; } rect = new Rect(rectX, rectY, rectWidth, rectHeight); return true; } #endregion } }