C#圖象處置之圖象目的質心檢測的辦法。本站提示廣大學習愛好者:(C#圖象處置之圖象目的質心檢測的辦法)文章只能為提供參考,不一定能成為您想要的結果。以下是C#圖象處置之圖象目的質心檢測的辦法正文
本文實例講述了C#圖象處置之圖象目的質心檢測的辦法。分享給年夜家供年夜家參考。詳細以下:
//采取二值化圖象,圖象中黑色表現配景,白色表現目的 //界說質心盤算函數 private int[] CentPoints(Bitmap src) { //界說存儲質心坐標的數組變量 int[] CentreP = new int[2]; int M00 = 0, M01 = 0, M10 = 0; Rectangle rect = new Rectangle(0, 0, src.Width, src.Height); System.Drawing.Imaging.BitmapData bmpData = src.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadWrite, System.Drawing.Imaging.PixelFormat.Format24bppRgb); unsafe { int stride = bmpData.Stride; byte* p; byte* pIn = (byte*)bmpData.Scan0.ToPointer(); int R, G, B; for (int y = 0; y < src.Height; y++) { for (int x = 0; x < src.Width; x++) { p = pIn; R = p[2]; G = p[1]; B = p[0]; if (R + G + B != 0) { M00++; M01 += y; M10 += x; } pIn += 3; } pIn += stride - src.Width * 3; } CentreP[0] = (int)(M10 / M00); CentreP[1] = (int)(M01 / M00); } src.UnlockBits(bmpData); return CentreP; //前往一個數組,該數組中第一個元素是質心的X坐標, //第二個元素是質心的Y坐標 }
願望本文所述對年夜家的C#法式設計有所贊助。