C#調用C++製作的含opencv圖像處理庫的DLL

namespace FaceDetectTestPro_CSharp_
{
    public partial class Form1 : Form
    {

         [DllImport("DllFaceDetect.dll", EntryPoint = "faceDetectMP", CallingConvention = CallingConvention.Cdecl)]
        /// gray_14:輸入的原始圖像數據
        /// posX,posY,width,height  矩形框位置信息
        /// contoursNum:矩形框個數
        /// thresholdGray:灰度閾值
public static extern void faceDetectMP(IntPtr gray_14, out IntPtr posX, out IntPtr posY, out IntPtr width, out IntPtr height, out int contoursNum, int thresholdGray);


         public Form1()
        {
            InitializeComponent();


        }

        /// <summary>
        /// 根據輸入圖像獲得矩形框座標
        /// </summary>
        /// <param name="DataImg"></param>
        private void GetRectPos( ushort[] DataImg)
        {


            int[] DataInt = new int[PixCount];
            for (int i = 0; i < PixCount; i++)
            {
                DataInt[i] = DataImg[i];
            }

            IntPtr gray_14 = Marshal.UnsafeAddrOfPinnedArrayElement(DataInt, 0);
            IntPtr posX, posY, width, height; //矩形框座標
            int contoursNum, thresholdGray = 220; //框架數量

            DateTime t1 = DateTime.Now;
            //多人人臉檢測
            faceDetectMP(gray_14, out posX, out posY, out width, out height, out contoursNum, thresholdGray);
            DateTime t2 = DateTime.Now;
            TimeSpan span = t2 - t1;
            lbTimeSpan.Text = span.TotalMilliseconds.ToString();

            int[] posXInt = new int[contoursNum];
            int[] posYInt = new int[contoursNum];
            int[] widthInt = new int[contoursNum];
            int[] heightInt = new int[contoursNum];
            Marshal.Copy(posX, posXInt, 0, contoursNum);
            Marshal.Copy(posY, posYInt, 0, contoursNum);
            Marshal.Copy(width, widthInt, 0, contoursNum);
            Marshal.Copy(height, heightInt, 0, contoursNum);
            for (int i = 0; i < contoursNum; i++)
            {
                lbRect.Items.Add("(" + posXInt[i] + "," + posYInt[i] + "," + 
                               widthInt[i] + "," + heightInt[i] + ")");
            }
        }

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章