123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566 |
- using System.Runtime.InteropServices;
- namespace YOLODetectProcessLib
- {
- /// <summary>
- /// 裁取超声图像区域的辅助类
- /// </summary>
- public class UsImageRegionSegUtils
- {
- #region dll import
- /// <summary>
- /// 调用CvCore.dll裁图
- /// </summary>
- /// <param name="scrImgData"></param>
- /// <param name="imgInfoIn"></param>
- /// <param name="rectInfoOut">左,上,宽,高</param>
- /// <returns></returns>
- [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
- }
- }
|