public void Init(Bitmap src, int distancia, int deslocamento) { LockedBitmap FrameBuffer = new LockedBitmap(src); List <KMCPoint> Centroids = new List <KMCPoint>(); Generate(ref Centroids, FrameBuffer, distancia, deslocamento); KMCPoint Mean = this.GetMean(FrameBuffer, Centroids); clusters.Add(new KMCFrame(FrameBuffer, Centroids, Mean)); }
private bool IsValidColor(List <KMCPoint> Points, KMCPoint Target, int deslocamento) { int Index = -1; bool Exists = false; while (++Index < Points.Count() && !Exists) { Exists = (Math.Sqrt(Math.Pow(Math.Abs(Points[Index].Clr.R - Target.Clr.R), 2) + Math.Pow(Math.Abs(Points[Index].Clr.G - Target.Clr.G), 2) + Math.Pow(Math.Abs(Points[Index].Clr.B - Target.Clr.B), 2))) <= deslocamento ? true : false; } return(Exists); }
private bool IsValidDistance(List <KMCPoint> Points, KMCPoint Target, int distancia) { int Index = -1; bool Exists = false; while (++Index < Points.Count() && !Exists) { Exists = ((Math.Abs(Target.X - Points.ElementAt(Index).X) <= distancia) || (Math.Abs(Target.Y - Points.ElementAt(Index).Y) <= distancia)) ? true : false; } return(Exists); }
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(); }
public double GetEuclClr(KMCPoint Point1, KMCPoint Point2) { return(Math.Sqrt(Math.Pow(Math.Abs(Point1.Clr.R - Point2.Clr.R), 2) + Math.Pow(Math.Abs(Point1.Clr.G - Point2.Clr.G), 2) + Math.Pow(Math.Abs(Point1.Clr.B - Point2.Clr.B), 2))); }
public KMCFrame(LockedBitmap imagem, List <KMCPoint> Centroids, KMCPoint centro) { this.imagem = imagem; this.centroids = Centroids; this.centro = centro; }
public void Add(LockedBitmap FrameImage, List <KMCPoint> Centroids, KMCPoint centro) { clusters.Add(new KMCFrame(FrameImage, Centroids, centro)); }