public KMCPoint GetMean(LockedBitmap FrameBuffer, List <KMCPoint> Centroids) { double Mean_X = 0, Mean_Y = 0; for (int Index = 0; Index < Centroids.Count(); Index++) { Mean_X += Centroids[Index].X / (double)Centroids.Count(); Mean_Y += Centroids[Index].Y / (double)Centroids.Count(); } int X = Convert.ToInt32(Mean_X); int Y = Convert.ToInt32(Mean_Y); FrameBuffer.LockBits(); Color Clr = FrameBuffer.GetPixel(X, Y); FrameBuffer.UnlockBits(); return(new KMCPoint(X, Y, Clr)); }
public void Generate(ref List <KMCPoint> Centroids, LockedBitmap ImageFrame, int distancia, int deslocamento) { int Size = ImageFrame.Width * ImageFrame.Height; ImageFrame.LockBits(); for (int IterCount = 0; IterCount < Size; IterCount++) { int Rand_X = rand.Next(0, ImageFrame.Width); int Rand_Y = rand.Next(0, ImageFrame.Height); KMCPoint RandPoint = new KMCPoint(Rand_X, Rand_Y, ImageFrame.GetPixel(Rand_X, Rand_Y)); if (!IsValidColor(Centroids, RandPoint, deslocamento) && !IsValidDistance(Centroids, RandPoint, distancia)) { if (!Centroids.Contains(RandPoint)) { Centroids.Add(RandPoint); } } } ImageFrame.UnlockBits(); }
public void Compute(Bitmap InputFile, out Bitmap OutputFile) { m_Clusters.Init(InputFile, distancia, deslocamento); LockedBitmap ResultBitmap = new LockedBitmap(m_Clusters[0].Imagem.Width, m_Clusters[0].Imagem.Height); int FrameIndex = 0; for (int i = 0; i < m_Clusters.Count(); i++) { List <KMCPoint> Centroids = m_Clusters[i].Centroids.ToList(); LockedBitmap FrameBuffer = new LockedBitmap(m_Clusters[i].Imagem.bitmap); FrameBuffer.LockBits(); for (int j = 0; j < Centroids.Count(); j++) { int Width = FrameBuffer.Width; int Height = FrameBuffer.Height; LockedBitmap TargetFrame = new LockedBitmap(FrameBuffer.Width, FrameBuffer.Height); TargetFrame.LockBits(); for (int y = 0; y < Width; y++) { for (int x = 0; x < Height; x++) { double OffsetClr = GetEuclClr(new KMCPoint(y, x, FrameBuffer.GetPixel(y, x)), new KMCPoint(Centroids[j].X, Centroids[j].Y, Centroids[j].Clr)); if (OffsetClr <= 50) { TargetFrame.SetPixel(y, x, Centroids[j].Clr); } else { TargetFrame.SetPixel(y, x, Color.FromArgb(255, 255, 255)); } } } TargetFrame.UnlockBits(); List <KMCPoint> Targetjs = new List <KMCPoint>(); Targetjs.Add(Centroids[0]); KMCPoint Mean = m_Clusters.GetMean(TargetFrame, Targetjs); if (Mean.X != m_Clusters[i].Centro.X && Mean.Y != m_Clusters[i].Centro.Y) { m_Clusters.Add(TargetFrame, Targetjs, Mean); } FrameIndex++; } FrameBuffer.UnlockBits(); } ResultBitmap.LockBits(); for (int Index = 0; Index < m_Clusters.Count(); Index++) { LockedBitmap FrameOut = new LockedBitmap(m_Clusters[Index].Imagem.bitmap); FrameOut.LockBits(); int Width = FrameOut.Width, Height = FrameOut.Height; for (int y = 0; y < Width; y++) { for (int x = 0; x < Height; x++) { if (FrameOut.GetPixel(y, x) != Color.FromArgb(255, 255, 255)) { ResultBitmap.SetPixel(y, x, FrameOut.GetPixel(y, x)); } } } FrameOut.UnlockBits(); } ResultBitmap.UnlockBits(); OutputFile = ResultBitmap.getBitMap(); }