public Point <int> GetMean(LockedBitmap frameBuffer, List <Point <int> > centroids) { double mean_x = 0; double mean_y = 0; for (int i = 0; i < centroids.Count(); i++) { mean_x += centroids[i].X / (double)centroids.Count(); mean_y += centroids[i].Y / (double)centroids.Count(); } int x = Convert.ToInt32(mean_x); int y = Convert.ToInt32(mean_y); frameBuffer.LockBits(); Color c = frameBuffer.GetPixel(x, y); frameBuffer.UnlockBits(); return(new Point <int>(x, y, c)); }
public void Generate(ref List <Point <int> > centroids, LockedBitmap imageFrame, int size, int distance, int offset) { imageFrame.LockBits(); for (int i = 0; i < size; i++) { int rand_x = rand.Next(0, imageFrame.width); int rand_y = rand.Next(0, imageFrame.height); Point <int> rand_p = new Point <int>(rand_x, rand_y, imageFrame.GetPixel(rand_x, rand_y)); if (!this.IsValidColor(centroids, rand_p, offset) && !this.IsValidDistance(centroids, rand_p, distance)) { if (!centroids.Contains(rand_p)) { centroids.Add(rand_p); } } } imageFrame.UnlockBits(); }
public Bitmap Compute(Bitmap picBitmap, int size) { clusters.Init(picBitmap, size, distance, offset); LockedBitmap rsltBitmap = new LockedBitmap(clusters[0].bitmapFrame.width, clusters[0].bitmapFrame.height); int frameIndex = 0; for (int i = 0; i < clusters.Count(); i++) { List <Point <int> > centroids = clusters[i].centroids.ToList(); LockedBitmap frameBuffer = new LockedBitmap(clusters[i].bitmapFrame.bit); frameBuffer.LockBits(); for (int c = 0; c < centroids.Count(); c++) { int width = frameBuffer.width; int height = frameBuffer.height; LockedBitmap targetFrame = new LockedBitmap(frameBuffer.width, frameBuffer.height); targetFrame.LockBits(); for (int row = 0; row < frameBuffer.width; row++) { for (int column = 0; column < height; column++) { double offset = Euclidian(new Point <int>(row, column, frameBuffer.GetPixel(row, column)), new Point <int>(centroids[c].X, centroids[c].Y, centroids[c].Clr)); if (offset <= 50) { targetFrame.SetPixel(row, column, centroids[c].Clr); } else { targetFrame.SetPixel(row, column, Color.FromArgb(255, 255, 255)); } } } targetFrame.UnlockBits(); List <Point <int> > targetCnts = new List <Point <int> >(); targetCnts.Add(centroids[0]); Point <int> mean = clusters.GetMean(targetFrame, targetCnts); if (mean.X != clusters[i].imCenter.X && mean.Y != clusters[i].imCenter.Y) { clusters.Add(targetFrame, targetCnts, mean); } frameIndex++; } frameBuffer.UnlockBits(); } rsltBitmap.LockBits(); for (int i = 0; i < clusters.Count(); i++) { LockedBitmap frameOut = new LockedBitmap(clusters[i].bitmapFrame.bit); frameOut.LockBits(); int width = frameOut.width; int height = frameOut.height; for (int row = 0; row < width; row++) { for (int column = 0; column < height; column++) { if (frameOut.GetPixel(row, column) != Color.FromArgb(255, 255, 255)) { rsltBitmap.SetPixel(row, column, frameOut.GetPixel(row, column)); } } } frameOut.UnlockBits(); } rsltBitmap.UnlockBits(); return(rsltBitmap.bit); }