图4.3 “彩色图像K-均值聚类分割”结果
以下为“K-均值聚类”处理代码如下:
private void button1_Click(object sender, EventArgs e)
{
if (curBitmap != null)
{
segColor segmentationC = new segColor();
if (segmentationC.ShowDialog() == DialogResult.OK)
{
Rectangle rect = new Rectangle(0, 0, curBitmap.Width, curBitmap.Height);
System.Drawing.Imaging.BitmapData bmpData = curBitmap.LockBits(rect,
System.Drawing.Imaging.ImageLockMode.ReadWrite,
curBitmap.PixelFormat);
IntPtr ptr = bmpData.Scan0;
int bytes = curBitmap.Width * curBitmap.Height;
byte[] rgbValues = new byte[bytes * 3];
System.Runtime.InteropServices.Marshal.Copy(ptr, rgbValues, 0, bytes * 3);
byte numbers = segmentationC.GetNum;
int[] kNum = new int[numbers];
int[] kAver = new int[numbers * 3];
int[] kOldAver = new int[numbers * 3];
int[] kSum = new int[numbers * 3];
double[] kTemp = new double[numbers];
byte[] segmentMap = new byte[bytes * 3];
//初始化聚类均值
for (int i = 0; i < numbers; i++)
{