C#圖像處理之圖像目標質心檢測的方法
更新時間:2015年04月24日 10:23:15 作者:滄海一粟……
這篇文章主要介紹了C#圖像處理之圖像目標質心檢測的方法,可實現(xiàn)C#計算圖像質心的相關技巧,需要的朋友可以參考下
本文實例講述了C#圖像處理之圖像目標質心檢測的方法。分享給大家供大家參考。具體如下:
//采用二值化圖像,圖像中黑色表示背景,白色表示目標
//定義質心計算函數(shù)
private int[] CentPoints(Bitmap src)
{
//定義存儲質心坐標的數(shù)組變量
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;
//返回一個數(shù)組,該數(shù)組中第一個元素是質心的X坐標,
//第二個元素是質心的Y坐標
}
希望本文所述對大家的C#程序設計有所幫助。
相關文章
c#中SqlHelper封裝SqlDataReader的方法
這篇文章主要介紹了c#中SqlHelper封裝SqlDataReader的方法,涉及C#針對數(shù)據(jù)庫相關操作封裝與使用的技巧,需要的朋友可以參考下2015-05-05
C# 使用PictureBox實現(xiàn)圖片按鈕控件的示例步驟
這篇文章主要介紹了C# 使用PictureBox實現(xiàn)圖片按鈕控件的示例步驟,幫助大家更好的理解和使用c#,感興趣的朋友可以了解下2021-02-02

