UsImageRegionSegUtils.cs 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. using System.Runtime.InteropServices;
  2. namespace YOLODetectProcessLib
  3. {
  4. /// <summary>
  5. /// 裁取超声图像区域的辅助类
  6. /// </summary>
  7. public class UsImageRegionSegUtils
  8. {
  9. #region dll import
  10. /// <summary>
  11. /// 调用CvCore.dll裁图
  12. /// </summary>
  13. /// <param name="scrImgData"></param>
  14. /// <param name="imgInfoIn"></param>
  15. /// <param name="rectInfoOut">左,上,宽,高</param>
  16. /// <returns></returns>
  17. [DllImport(@"CvCropUltImgRegion.dll", CallingConvention = CallingConvention.Cdecl)]
  18. [return: MarshalAs(UnmanagedType.I1)]
  19. public static extern bool CropImage(byte[] scrImgData, int[] imgInfoIn, int[] rectInfoOut);
  20. #endregion
  21. #region public
  22. public static bool CropWithCvCore(IImage image, out Rect rect)
  23. {
  24. if (image.Channel != 3 && image.Channel != 4)
  25. {
  26. rect = new Rect(0, 0, image.Width, image.Height);
  27. return false;
  28. }
  29. int[] imgInfo = new int[4];
  30. imgInfo[0] = image.Width;
  31. imgInfo[1] = image.Height;
  32. imgInfo[2] = image.Stride;
  33. imgInfo[3] = image.Channel;
  34. int[] rectInfo = new int[4];
  35. if (!CropImage(image.DataBuffer, imgInfo, rectInfo))
  36. {
  37. rect = new Rect(0, 0, image.Width, image.Height);
  38. return false;
  39. }
  40. int rectX = rectInfo[0];
  41. int rectY = rectInfo[1];
  42. int rectWidth = rectInfo[2];
  43. int rectHeight = rectInfo[3];
  44. if ((rectX < 0) || (rectY < 0) || (rectWidth <= 0) || (rectHeight <= 0) ||
  45. (rectX + rectWidth >= image.Width) || (rectY + rectHeight >= image.Height))
  46. {
  47. rect = new Rect(0, 0, image.Width, image.Height);
  48. return false;
  49. }
  50. rect = new Rect(rectX, rectY, rectWidth, rectHeight);
  51. return true;
  52. }
  53. #endregion
  54. }
  55. }