public void AddToColor(MyPixel other) { this.red += other.red; this.green += other.green; this.blue += other.blue; this.pixelCount++; }
public void InitCanvas(int row, int column) { var p = new List <MyPixel>(); var time = new Stopwatch(); time.Start(); int index = 1; for (int rowIndex = 0; rowIndex < row; rowIndex++) { for (int columnIndex = 0; columnIndex < column; columnIndex++) { var button = new MyPixel() { Row = rowIndex + 1, Colunm = columnIndex + 1, Index = index, Height = 20, Width = 20, Margin = new Thickness(columnIndex * 20, rowIndex * 20, 0, 0), }; index++; p.Add(button); } } Points = p; time.Stop(); Console.WriteLine(time.ElapsedMilliseconds); }
public MyPixel(MyPixel other) { this.blue = other.blue; this.red = other.red; this.green = other.green; this.segment = other.segment; }
private void MyButton_OnMouseLeftButtonDown(object sender, MouseButtonEventArgs e) { if (_mainViewModel.CurrentBrushType == BrushType.矩形) { var btn = sender as MyButton; firstPixel = _mainViewModel.Points.FirstOrDefault(p => p.Margin == btn?.Margin); } }
public void DefaultCtor() { MyPixel pixel = new MyPixel(); Assert.AreEqual(3, pixel.BandCount); Assert.AreEqual(System.TypeCode.Byte, pixel[0].TypeCode); Assert.AreEqual(System.TypeCode.Int16, pixel[1].TypeCode); Assert.AreEqual(System.TypeCode.Double, pixel[2].TypeCode); Assert.AreEqual(0, pixel.Band0); Assert.AreEqual(0, pixel.Band1); Assert.AreEqual(0, pixel.Band2); }
private bool IsUnique(MyPixel temp) { bool isUnique = true; for (int i = 0; i < SegList.Count; i++) { if (temp == SegList[i]) { isUnique = false; break; } } return(isUnique); }
private byte FindClosestSegmentPoint(MyPixel pixel) { int closestIndex = -1; int minimumDistance = Int32.MaxValue; for (int i = 0; i < SegList.Count; i++) { int temp = Math.Min(minimumDistance, EquidianDistance(pixel, SegList[i])); if (temp < minimumDistance) { minimumDistance = temp; closestIndex = i; } } return((byte)closestIndex); }
public void CtorWithArgs() { byte band0 = 200; short band1 = -4321; double band2 = 1e65; MyPixel pixel = new MyPixel(band0, band1, band2); Assert.AreEqual(3, pixel.BandCount); Assert.AreEqual(System.TypeCode.Byte, pixel[0].TypeCode); Assert.AreEqual(System.TypeCode.Int16, pixel[1].TypeCode); Assert.AreEqual(System.TypeCode.Double, pixel[2].TypeCode); Assert.AreEqual(band0, pixel.Band0); Assert.AreEqual(band1, pixel.Band1); Assert.AreEqual(band2, pixel.Band2); }
private void SetSegments() { Random rng = new Random(); for (byte i = 1; i <= this.segmentNumber; i++) { int num = rng.Next(arrP.Count); MyPixel temp = new MyPixel(arrP[num].blue, arrP[num].green, arrP[num].red, i); if (IsUnique(temp)) { SegList.Add(new MyPixel(temp)); } else { i--; } } }
private void CalcSegmentation() { bool isReady = false; Dictionary <int, MyPixelAvrg> averageP = new Dictionary <int, MyPixelAvrg>(); for (byte i = 0; i < SegList.Count; i++) { averageP[i] = new MyPixelAvrg(i); } int counter = 20 * this.segmentNumber; while (!isReady) { isReady = true; for (int i = 0; i < arrP.Count; i++) { byte SegmentIndex = FindClosestSegmentPoint(arrP[i]); if (SegmentIndex != arrP[i].segment) { isReady = false; } MyPixel tp = arrP[i]; tp.setSegment(SegmentIndex); arrP[i] = tp; MyPixelAvrg temp = averageP[SegmentIndex]; temp.AddToColor(arrP[i]); averageP[SegmentIndex] = temp; counter--; } for (int i = 0; i < SegList.Count; i++) { MyPixelAvrg temp = averageP[i]; temp.AverageOut(); averageP[i] = temp; MyPixel tp = SegList[i]; tp.CopyPixelData(averageP[i]); SegList[i] = tp; } } }
public void BandIndex_TooBig() { MyPixel pixel = new MyPixel(); IPixelBand band = pixel[pixel.BandCount]; }
public void BandIndex_Negative() { MyPixel pixel = new MyPixel(); IPixelBand band = pixel[-1]; }
private int EquidianDistance(MyPixel p1, MyPixel p2) { return((p1.blue - p2.blue) * (p1.blue - p2.blue) + (p1.red - p2.red) * (p1.red - p2.red) + (p1.green - p2.green) * (p1.green - p2.green)); }
public void CopyPixelData(MyPixel avrg) { this.red = avrg.red; this.green = avrg.green; this.blue = avrg.blue; }
private void LoadedImageBox_LoadCompleted(object sender, AsyncCompletedEventArgs e) { // if there is no bitmap, don't do anything if (originalBitmap == null) { return; } int width = originalBitmap.Width; int height = originalBitmap.Height; // initialize MyPixel matrix MyPixel[][] pixelMatrix = new MyPixel[height][]; for (int row = 0; row < height; row++) { pixelMatrix[row] = new MyPixel[width]; } // Start detection // for each pixel for (int row = 0; row < height; row++) { for (int col = 0; col < width; col++) { Color pixelColor = originalBitmap.GetPixel(col, row); // put Y value into 2D matrix pixelMatrix[row][col].y = (int)(Y[0] * pixelColor.R + Y[1] * pixelColor.G + Y[2] * pixelColor.B); } } // Pad yMatrix int paddedHeight = height + 4; int paddedWidth = width + 4; int[][] paddedYMatrix = new int[paddedHeight][]; for (int row = 0; row < paddedYMatrix.Length; row++) { paddedYMatrix[row] = new int[paddedWidth]; } // Inner paddedYMatrix for (int row = 0; row < height; row++) { for (int col = 0; col < width; col++) { paddedYMatrix[row + 2][col + 2] = pixelMatrix[row][col].y; } } // Four Corners // Top Left Corner paddedYMatrix[0][0] = pixelMatrix[1][1].y; paddedYMatrix[0][1] = pixelMatrix[1][0].y; paddedYMatrix[1][0] = pixelMatrix[0][1].y; paddedYMatrix[1][1] = pixelMatrix[0][0].y; // Top Right Corner paddedYMatrix[0][paddedWidth - 1] = pixelMatrix[1][width - 2].y; paddedYMatrix[0][paddedWidth - 2] = pixelMatrix[1][width - 1].y; paddedYMatrix[1][paddedWidth - 1] = pixelMatrix[0][width - 2].y; paddedYMatrix[1][paddedWidth - 2] = pixelMatrix[0][width - 1].y; // Bottom Right Corner paddedYMatrix[paddedHeight - 1][paddedWidth - 1] = pixelMatrix[height - 2][width - 2].y; paddedYMatrix[paddedHeight - 1][paddedWidth - 2] = pixelMatrix[height - 2][width - 1].y; paddedYMatrix[paddedHeight - 2][paddedWidth - 1] = pixelMatrix[height - 1][width - 2].y; paddedYMatrix[paddedHeight - 2][paddedWidth - 2] = pixelMatrix[height - 1][width - 1].y; // Bottom Left Corner paddedYMatrix[paddedHeight - 1][0] = pixelMatrix[height - 2][1].y; paddedYMatrix[paddedHeight - 1][1] = pixelMatrix[height - 2][0].y; paddedYMatrix[paddedHeight - 2][0] = pixelMatrix[height - 1][1].y; paddedYMatrix[paddedHeight - 2][1] = pixelMatrix[height - 1][0].y; // Four Edges // Top & Bottom Edge for (int col = 0; col < width; col++) { // Very Top Edge paddedYMatrix[0][col + 2] = pixelMatrix[1][col].y; // Second From Top paddedYMatrix[1][col + 2] = pixelMatrix[0][col].y; // Second From Bottom paddedYMatrix[paddedHeight - 2][col + 2] = pixelMatrix[height - 1][col].y; // Very Bottom Edge paddedYMatrix[paddedHeight - 1][col + 2] = pixelMatrix[height - 2][col].y; } // Left and Right Edges for (int row = 0; row < width; row++) { if (row != 0 || row != 1 || row != width - 2 || row != width - 1) { continue; } paddedYMatrix[row + 2][0] = pixelMatrix[row][1].y; paddedYMatrix[row + 2][1] = pixelMatrix[row][0].y; paddedYMatrix[row + 2][paddedWidth - 2] = pixelMatrix[row][width - 1].y; paddedYMatrix[row + 2][paddedWidth - 1] = pixelMatrix[row][width - 2].y; } // Smooth the pixels for (int row = 2; row < paddedHeight - 2; row++) { for (int col = 2; col < paddedWidth - 2; col++) { int[][] arr = new int[5][]; for (int i = 0; i < arr.Length; i++) { arr[i] = paddedYMatrix[i + row - 2].Skip(col - 2).Take(5).ToArray(); } pixelMatrix[row - 2][col - 2].smoothY = SmoothPixel(new int[][] { paddedYMatrix[row - 2].Skip(col - 2).Take(5).ToArray(), paddedYMatrix[row - 1].Skip(col - 2).Take(5).ToArray(), paddedYMatrix[row].Skip(col - 2).Take(5).ToArray(), paddedYMatrix[row + 1].Skip(col - 2).Take(5).ToArray(), paddedYMatrix[row + 2].Skip(col - 2).Take(5).ToArray(), }); } } // Calculate the gradient X value for (int row = 0; row < height; row++) { for (int col = 0; col < width; col++) { int prev, next; if (col == 0) { prev = pixelMatrix[row][col + 1].smoothY; } else { prev = pixelMatrix[row][col - 1].smoothY; } if (col == width - 1) { next = pixelMatrix[row][col - 1].smoothY; } else { next = pixelMatrix[row][col + 1].smoothY; } pixelMatrix[row][col].gradientX = (next - prev) / 2; } } // Calculate the gradient Y value for (int row = 0; row < height; row++) { for (int col = 0; col < width; col++) { int prev, next; if (row == 0) { prev = pixelMatrix[row + 1][col].smoothY; } else { prev = pixelMatrix[row - 1][col].smoothY; } if (row == height - 1) { next = pixelMatrix[row - 1][col].smoothY; } else { next = pixelMatrix[row + 1][col].smoothY; } pixelMatrix[row][col].gradientY = (next - prev) / 2; } } // Find is Max by figuring the gradient and finding its relation to other pixels for (int row = 0; row < height; row++) { for (int col = 0; col < width; col++) { int rowPlusOne, rowMinusOne, colPlusOne, colMinusOne; if (row == 0) { rowMinusOne = row + 1; } else { rowMinusOne = row - 1; } if (row == height - 1) { rowPlusOne = row - 1; } else { rowPlusOne = row + 1; } if (col == 0) { colMinusOne = col + 1; } else { colMinusOne = col - 1; } if (col == width - 1) { colPlusOne = col - 1; } else { colPlusOne = col + 1; } int x = pixelMatrix[row][col].gradientX; int y = pixelMatrix[row][col].gradientY; if (x > 0 && y > 0) { // In quadrant A if (y > x) { // Test against ([row-1][col] + [row-1][col+1]) / 2 and ([row+1][col] + [row+1][col-1]) / 2 int smoothY1 = (pixelMatrix[rowMinusOne][col].smoothY + pixelMatrix[rowMinusOne][colPlusOne].smoothY) / 2; int smoothY2 = (pixelMatrix[rowPlusOne][col].smoothY + pixelMatrix[rowPlusOne][colMinusOne].smoothY) / 2; int originalSmoothY = pixelMatrix[row][col].smoothY; if (originalSmoothY > smoothY1 && originalSmoothY > smoothY2) { pixelMatrix[row][col].isEdge = IsEdge.Yes; } else { pixelMatrix[row][col].isEdge = IsEdge.Maybe; } } else { // Test against ([row][col+1] + [row+1][col+1]) /2 and ([row][col-1] + [row-1][col-1]) / 2 int smoothY1 = (pixelMatrix[row][colPlusOne].smoothY + pixelMatrix[rowPlusOne][colPlusOne].smoothY) / 2; int smoothY2 = (pixelMatrix[row][colMinusOne].smoothY + pixelMatrix[rowMinusOne][colMinusOne].smoothY) / 2; int originalSmoothY = pixelMatrix[row][col].smoothY; if (originalSmoothY > smoothY1 && originalSmoothY > smoothY2) { pixelMatrix[row][col].isEdge = IsEdge.Yes; } else { pixelMatrix[row][col].isEdge = IsEdge.Maybe; } } } else if (y > 0) { // In quadrant S if (y > Math.Abs(x)) { // Test against ([row-1][col] + [row-1][col-1]) / 2 and ([row+1][col] + [row+1][col+1]) / 2 int smoothY1 = (pixelMatrix[rowMinusOne][col].smoothY + pixelMatrix[rowMinusOne][colMinusOne].smoothY) / 2; int smoothY2 = (pixelMatrix[rowPlusOne][col].smoothY + pixelMatrix[rowPlusOne][colPlusOne].smoothY) / 2; int originalSmoothY = pixelMatrix[row][col].smoothY; if (originalSmoothY > smoothY1 && originalSmoothY > smoothY2) { pixelMatrix[row][col].isEdge = IsEdge.Yes; } else { pixelMatrix[row][col].isEdge = IsEdge.Maybe; } } else { // Test against ([row][col+1] + [row-1][col+1]) / 2 and ([row][col-1] + [row+1][col-1] int smoothY1 = (pixelMatrix[row][colPlusOne].smoothY + pixelMatrix[rowMinusOne][colPlusOne].smoothY) / 2; int smoothY2 = (pixelMatrix[row][colMinusOne].smoothY + pixelMatrix[rowPlusOne][colMinusOne].smoothY) / 2; int originalSmoothY = pixelMatrix[row][col].smoothY; if (originalSmoothY > smoothY1 && originalSmoothY > smoothY2) { pixelMatrix[row][col].isEdge = IsEdge.Yes; } else { pixelMatrix[row][col].isEdge = IsEdge.Maybe; } } } else if (x > 0) { // In quadrant C if (Math.Abs(y) > x) { // Test against ([row-1][col] + [row-1][col-1]) / 2 and ([row+1][col] + [row+1][col+1]) / 2 int smoothY1 = (pixelMatrix[rowMinusOne][col].smoothY + pixelMatrix[rowMinusOne][colMinusOne].smoothY) / 2; int smoothY2 = (pixelMatrix[rowPlusOne][col].smoothY + pixelMatrix[rowPlusOne][colPlusOne].smoothY) / 2; int originalSmoothY = pixelMatrix[row][col].smoothY; if (originalSmoothY > smoothY1 && originalSmoothY > smoothY2) { pixelMatrix[row][col].isEdge = IsEdge.Yes; } else { pixelMatrix[row][col].isEdge = IsEdge.Maybe; } } else { // Test against ([row][col+1] + [row-1][col+1]) / 2 and ([row][col-1] + [row+1][col-1] int smoothY1 = (pixelMatrix[row][colPlusOne].smoothY + pixelMatrix[rowMinusOne][colPlusOne].smoothY) / 2; int smoothY2 = (pixelMatrix[row][colMinusOne].smoothY + pixelMatrix[rowPlusOne][colMinusOne].smoothY) / 2; int originalSmoothY = pixelMatrix[row][col].smoothY; if (originalSmoothY > smoothY1 && originalSmoothY > smoothY2) { pixelMatrix[row][col].isEdge = IsEdge.Yes; } else { pixelMatrix[row][col].isEdge = IsEdge.Maybe; } } } else { // In quadrant T if (Math.Abs(y) > Math.Abs(x)) { // Test against ([row-1][col] + [row-1][col+1]) / 2 and ([row+1][col] + [row+1][col-1]) / 2 int smoothY1 = (pixelMatrix[rowMinusOne][col].smoothY + pixelMatrix[rowMinusOne][colPlusOne].smoothY) / 2; int smoothY2 = (pixelMatrix[rowPlusOne][col].smoothY + pixelMatrix[rowPlusOne][colMinusOne].smoothY) / 2; int originalSmoothY = pixelMatrix[row][col].smoothY; if (originalSmoothY > smoothY1 && originalSmoothY > smoothY2) { pixelMatrix[row][col].isEdge = IsEdge.Yes; } else { pixelMatrix[row][col].isEdge = IsEdge.Maybe; } } else { // Test against ([row][col+1] + [row+1][col+1]) /2 and ([row][col-1] + [row-1][col-1]) / 2 int smoothY1 = (pixelMatrix[row][colPlusOne].smoothY + pixelMatrix[rowPlusOne][colPlusOne].smoothY) / 2; int smoothY2 = (pixelMatrix[row][colMinusOne].smoothY + pixelMatrix[rowMinusOne][colMinusOne].smoothY) / 2; int originalSmoothY = pixelMatrix[row][col].smoothY; if (originalSmoothY > smoothY1 && originalSmoothY > smoothY2) { pixelMatrix[row][col].isEdge = IsEdge.Yes; } else { pixelMatrix[row][col].isEdge = IsEdge.Maybe; } } } } } // evaluate top 5% and low 5% Dictionary <int, int> yHist = new Dictionary <int, int>(); List <int> topYValues = new List <int>(); List <int> lowYValues = new List <int>(); for (int row = 0; row < height; row++) { for (int col = 0; col < width; col++) { int y = pixelMatrix[row][col].smoothY; if (!yHist.ContainsKey(y)) { yHist.Add(y, 0); } else { yHist[y]++; } } } Dictionary <int, int> .KeyCollection keyColl = yHist.Keys; List <int> sortedYValues = keyColl.ToList(); sortedYValues.Sort(new Comparison <int>( (i1, i2) => yHist[i2].CompareTo(yHist[i1]))); int numberOfTopOrLow = sortedYValues.Count / 100 * 5; for (int i = 0; i < numberOfTopOrLow; i++) { lowYValues.Add(sortedYValues[i]); topYValues.Add(sortedYValues[sortedYValues.Count - 1 - i]); } // Threshold top and low y values for (int row = 0; row < height; row++) { for (int col = 0; col < width; col++) { int y = pixelMatrix[row][col].smoothY; if (lowYValues.Contains(y)) { pixelMatrix[row][col].isEdge = IsEdge.No; } if (topYValues.Contains(y)) { pixelMatrix[row][col].isEdge = IsEdge.Yes; } } } // duplicate the MyPixel matrix IsEdge[][] edgeMatrix = new IsEdge[height][]; for (int i = 0; i < height; i++) { edgeMatrix[i] = new IsEdge[width]; for (int j = 0; j < width; j++) { edgeMatrix[i][j] = pixelMatrix[i][j].isEdge; } } // Get rid of Maybes for (int row = 0; row < height; row++) { for (int col = 0; col < width; col++) { if (pixelMatrix[row][col].isEdge == IsEdge.Maybe) { bool isTopEdge = row == 0; bool isBottomEdge = row == height - 1; bool isLeftEdge = col == 0; bool isRightEdge = col == width - 1; if (!isLeftEdge) { // do row and col - 1 check if (pixelMatrix[row][col - 1].isEdge == IsEdge.Yes) { edgeMatrix[row][col] = IsEdge.Yes; continue; } } if (!isRightEdge) { // do row and col + 1 check if (pixelMatrix[row][col + 1].isEdge == IsEdge.Yes) { edgeMatrix[row][col] = IsEdge.Yes; continue; } } if (!isTopEdge) { // do row - 1 and col check if (pixelMatrix[row - 1][col].isEdge == IsEdge.Yes) { edgeMatrix[row][col] = IsEdge.Yes; continue; } if (!isLeftEdge) { // do row - 1 and col - 1 check if (pixelMatrix[row - 1][col - 1].isEdge == IsEdge.Yes) { edgeMatrix[row][col] = IsEdge.Yes; continue; } } if (!isRightEdge) { // do row - 1 and col + 1 check if (pixelMatrix[row - 1][col + 1].isEdge == IsEdge.Yes) { edgeMatrix[row][col] = IsEdge.Yes; continue; } } } if (!isBottomEdge) { // do row + 1 and col check if (pixelMatrix[row + 1][col].isEdge == IsEdge.Yes) { edgeMatrix[row][col] = IsEdge.Yes; continue; } if (!isLeftEdge) { // do row + 1 and col - 1 check if (pixelMatrix[row + 1][col - 1].isEdge == IsEdge.Yes) { edgeMatrix[row][col] = IsEdge.Yes; continue; } } if (!isRightEdge) { // do row + 1 and col + 1 check if (pixelMatrix[row + 1][col + 1].isEdge == IsEdge.Yes) { edgeMatrix[row][col] = IsEdge.Yes; continue; } } } } } } Bitmap newBitmap = new Bitmap(width, height); for (int row = 0; row < height; row++) { for (int col = 0; col < width; col++) { IsEdge isEdge = edgeMatrix[row][col]; if (isEdge == IsEdge.Yes) { newBitmap.SetPixel(col, row, Color.White); } else { newBitmap.SetPixel(col, row, Color.Black); } } } if (EdgeForm == null) { EdgeForm = new EdgeDetectionForm(); EdgeForm.FormClosed += (_, arg) => { EdgeForm = null; }; EdgeForm.SetImage(newBitmap); EdgeForm.Show(); } else { EdgeForm.SetImage(newBitmap); } }