C#图像处理之图像目标质心检测的方法
本文实例讲述了C#图像处理之图像目标质心检测的方法。分享给大家供大家参考。具体如下:
//采用二值化图像,图像中黑色表示背景,白色表示目标
//定义质心计算函数
privateint[]CentPoints(Bitmapsrc)
{
//定义存储质心坐标的数组变量
int[]CentreP=newint[2];
intM00=0,M01=0,M10=0;
Rectanglerect=newRectangle(0,0,src.Width,src.Height);
System.Drawing.Imaging.BitmapDatabmpData=src.LockBits(rect,System.Drawing.Imaging.ImageLockMode.ReadWrite,System.Drawing.Imaging.PixelFormat.Format24bppRgb);
unsafe
{
intstride=bmpData.Stride;
byte*p;
byte*pIn=(byte*)bmpData.Scan0.ToPointer();
intR,G,B;
for(inty=0;y<src.Height;y++)
{
for(intx=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);
returnCentreP;
//返回一个数组,该数组中第一个元素是质心的X坐标,
//第二个元素是质心的Y坐标
}
希望本文所述对大家的C#程序设计有所帮助。