static public HSVBit getFromColor(Color color) { HSVBit hsvBit = new HSVBit(); float redPrim = (float)color.R / 255; float greenPrim = (float)color.G / 255; float bluePrim = (float)color.B / 255; float cMax = HSVBit.maxValue(redPrim, greenPrim, bluePrim); float cMin = HSVBit.minValue(redPrim, greenPrim, bluePrim); float delta = cMax - cMin; if (delta == 0) { hsvBit.HUE = 0; } else { if (cMax == redPrim) { hsvBit.HUE = 60 * (((greenPrim - redPrim) / delta) % 6); } else if (cMax == greenPrim) { hsvBit.HUE = 60 * ((bluePrim - redPrim) / delta + 2); } else if (cMax == bluePrim) { hsvBit.HUE = 60 * ((redPrim - greenPrim) / delta + 4); } } if (cMax == 0) { hsvBit.saturation = 0; } else { hsvBit.saturation = delta / cMax; } hsvBit.value = cMax; return(hsvBit); }
private void pictureBox1_MouseMove(object sender, MouseEventArgs e) { int helperValue = 0; List <Point> listOfDrawnPoints; if (isMouseDown == true && drawingType == DrawingType.FreeHand)//check to see if the mouse button is down { if (lastPoint != null)//if our last point is not null, which in this case we have assigned above { //Rozpoczęcie nanoszenia zmian na obiekt pictureBox1 using (Graphics g = Graphics.FromImage(pictureBox1.Image)) { //pobranie listy narysowanych pikseli listOfDrawnPoints = this.EnumerateLineNoDiagonalSteps(lastPoint.X, lastPoint.Y, e.X, e.Y); //generowanie modeli histogramów w oparciu o piksele które zostały zamalowane foreach (var x in listOfDrawnPoints) { HSVBit hsvBit = directBitmapOrig.GetPixelHSV(x.X, x.Y); Color color = directBitmapOrig.GetPixel(x.X, x.Y); drawnPointList.Add(new DrawnPoint(directBitmapOrig.GetPixel(x.X, x.Y), x)); //Dane histogramu RGB histogramDataLineRGB.redHisto[color.R]++; histogramDataLineRGB.blueHisto[color.B]++; histogramDataLineRGB.greenHisto[color.G]++; //Dane hhhistogramu HSV histogramDataLineRGB.HSV_HHisto.TryGetValue(hsvBit.HUE, out helperValue); histogramDataLineRGB.HSV_HHisto[hsvBit.HUE] = helperValue + 1; histogramDataLineRGB.HSV_SHisto.TryGetValue(hsvBit.saturation, out helperValue); histogramDataLineRGB.HSV_SHisto[hsvBit.saturation] = helperValue + 1; histogramDataLineRGB.HSV_VHisto.TryGetValue(hsvBit.value, out helperValue); histogramDataLineRGB.HSV_VHisto[hsvBit.value] = helperValue + 1; //Dodawanie wygenerowanych danych do zbiorów danych połączonych z hisotgramami w Form1 formCaller.ser1.Points.AddXY(color.R, histogramDataLineRGB.redHisto[color.R]); formCaller.ser2.Points.AddXY(color.G, histogramDataLineRGB.greenHisto[color.G]); formCaller.ser3.Points.AddXY(color.B, histogramDataLineRGB.blueHisto[color.B]); formCaller.seriesHExt.Points.AddXY(hsvBit.HUE, histogramDataLineRGB.HSV_HHisto[hsvBit.HUE]); formCaller.seriesSExt.Points.AddXY(hsvBit.saturation, histogramDataLineRGB.HSV_SHisto[hsvBit.saturation]); formCaller.seriesVExt.Points.AddXY(hsvBit.value, histogramDataLineRGB.HSV_VHisto[hsvBit.value]); } //"Zamalowywanie narysowanych punktów na czarno foreach (var x in listOfDrawnPoints) { directBitmap.SetPixel(x.X, x.Y, Color.Black); } g.DrawImage(directBitmap.Bitmap, new Point(0, 0)); //Narysowanie nowego obrazu z użyciem directBitmap (obiketu edytowanego) } //odświeżenie obiektu pictureBox1 pictureBox1.Invalidate();//refreshes the picturebox //przypisanie zmiennej przechowywującej ostatni punkt na którym zarejestrowano zdarzenie ruchu myszki lastPoint = e.Location; drawnPointList.Add(new DrawnPoint(directBitmap.GetPixel(lastPoint.X, lastPoint.Y), lastPoint)); } } }
public void neighborhoodOperationHSV(int[] mask, DirectBitmap directBitmapPre, DirectBitmap directBitmapPost, KernelMethod _method = KernelMethod.NoBorders, SccalingMethod sccalingMethod = SccalingMethod.Cut, Boolean directionEdge = false) { int maskSize = (int)Math.Sqrt(mask.Length); int calculatedPixel = 0; HSVBit calculatedHSVBit = new HSVBit(); int maskSum = mask.Min() < 0 ? 1 : mask.Sum(); int maskSumInner = 0; List <float> angles = new List <float>(); int[] bitMapToScale = null; int minPixel = directBitmapPre.getMin(); int maxPixel = directBitmapPre.getMax(); int maskValue; int xo; int yo; int lowerXOverlap = 0; int higherXOverlap = 0; int lowerYOverlap = 0; int higherYOverlap = 0; int yiInner = 0; int calculatedPixelIndexX = 0; int calculatedPixelIndexY = 0; Color helpColor; HSVBit helperhSVBit; int overlap = 0; // = maskSize / 2; int maskOverlap = maskSize / 2; switch (_method) { case KernelMethod.NoBorders: overlap = maskSize / 2; break; case KernelMethod.CloneBorder: overlap = 0; break; case KernelMethod.UseExisting: overlap = 0; break; } //Iterate through image for (int x = overlap; x < directBitmapPre.Width - overlap; ++x) { for (int y = overlap; y < directBitmapPre.Height - overlap; ++y) { //Iterate through image //Iterate through mask for (int xi = 0; xi < maskSize; xi++) { for (int yi = 0; yi < maskSize; yi++) { maskValue = mask[xi + (yi * maskSize)]; xo = x + xi - maskOverlap; yo = y + yi - maskOverlap; calculatedPixelIndexX = x + xi - maskOverlap; calculatedPixelIndexY = y + yi - maskOverlap; if ((x == 0 && y == 0) || (x == directBitmapPre.Width && y == directBitmapPre.Height)) { higherXOverlap = 0; } if (_method == KernelMethod.UseExisting && (calculatedPixelIndexX < 0 || calculatedPixelIndexY < 0 || calculatedPixelIndexX >= directBitmapPre.Width || calculatedPixelIndexY >= directBitmapPre.Height)) { continue; } if (_method == KernelMethod.CloneBorder && (calculatedPixelIndexX < 0 || calculatedPixelIndexY < 0 || calculatedPixelIndexX >= directBitmapPre.Width || calculatedPixelIndexY >= directBitmapPre.Height)) { var cloneIndexValue = mask[xi + (yi * maskSize)]; for (int i = 0; i < cloneIndexValue; i++) { angles.Add(directBitmapPre.GetPixelHSV(x, y).HUE); } calculatedHSVBit.value += (directBitmapPre.GetPixelHSV(xi, yi).saturation *cloneIndexValue); continue; } var cloneIndexValueMain = mask[xi + (yi * maskSize)]; maskSumInner += cloneIndexValueMain; for (int i = 0; i < cloneIndexValueMain; i++) { angles.Add(directBitmapPre.GetPixelHSV(calculatedPixelIndexX, calculatedPixelIndexY).HUE); } //calculatedHSVBit.HUE += (directBitmapPre.GetPixelHSV(x, y).HUE * cloneIndexValueMain); calculatedHSVBit.saturation += (directBitmapPre.GetPixelHSV(calculatedPixelIndexX, calculatedPixelIndexY).saturation *cloneIndexValueMain); calculatedHSVBit.value += (directBitmapPre.GetPixelHSV(calculatedPixelIndexX, calculatedPixelIndexY).value *cloneIndexValueMain); } } if (_method == KernelMethod.UseExisting) { calculatedHSVBit.HUE = this.MeanAngle(angles.ToArray()); calculatedHSVBit.saturation = maskSum == 1 ? calculatedHSVBit.saturation : calculatedHSVBit.saturation / maskSumInner; calculatedHSVBit.value = maskSum == 1 ? calculatedHSVBit.value : calculatedHSVBit.value / maskSumInner; //calculatedPixel = maskSum == 1 ? calculatedPixel : calculatedPixel / maskSumInner; } else { var bit = directBitmapPost.GetPixelHSV(x, y); // calculatedPixel /= maskSum; calculatedHSVBit.HUE = this.MeanAngle(angles.ToArray()); calculatedHSVBit.saturation = maskSum == 1 ? calculatedHSVBit.saturation : calculatedHSVBit.saturation / maskSumInner; calculatedHSVBit.value = maskSum == 1 ? calculatedHSVBit.value : calculatedHSVBit.value / maskSumInner; } int newPixel = calculatedPixel; if (sccalingMethod != SccalingMethod.Scale) { newPixel = this.ScalePixelValue(newPixel, sccalingMethod, sccalingMethod == SccalingMethod.Scale ? minPixel : 0, sccalingMethod == SccalingMethod.Scale ? maxPixel : 255); } if (sccalingMethod != SccalingMethod.Scale) { var bit = directBitmapPost.GetPixelHSV(x, y); calculatedHSVBit.saturation = bit.saturation; calculatedHSVBit.HUE = bit.HUE; //apply cut scalling if (calculatedHSVBit.value > 1) { calculatedHSVBit.value = 1; } if (calculatedHSVBit.value < 0) { calculatedHSVBit.value = 0; } directBitmapPost.SetPixel(x, y, calculatedHSVBit.getColor()); } else { bitMapToScale[x + (y * directBitmapPre.Width)] = newPixel; } maskSumInner = 0; calculatedPixel = 0; calculatedHSVBit.HUE = 0; calculatedHSVBit.saturation = 0; calculatedHSVBit.value = 0; angles.Clear(); } } }
private void pictureBox1_MouseDown(object sender, MouseEventArgs e) { List <Point> listOfDrawnPoints; int helperValue = 0; switch (drawingType) { case DrawingType.Retclange: int XStart = 0; int XEnd = 0; int YStart = 0; int YEnd = 0; if (lastPoint == Point.Empty) { lastPoint = e.Location; } else { _nextPoint = e.Location; var differenceX = _nextPoint.X - lastPoint.X; var differenceY = _nextPoint.Y - lastPoint.Y; if (Math.Abs(differenceX) > 20) { if (differenceX > 0) { _nextPoint.X = lastPoint.X + 20; XStart = lastPoint.X; XEnd = _nextPoint.X; } else { _nextPoint.X = lastPoint.X - 20; XStart = _nextPoint.X; XEnd = lastPoint.X; } } if (Math.Abs(differenceY) > 20) { if (differenceY > 0) { _nextPoint.Y = lastPoint.Y + 20; YStart = lastPoint.Y; YEnd = _nextPoint.Y; } else { _nextPoint.Y = lastPoint.Y - 20; YStart = _nextPoint.Y; YEnd = lastPoint.Y; } } } if (_nextPoint != Point.Empty) { listOfDrawnPoints = this.EnumerateLineNoDiagonalSteps(lastPoint.X, lastPoint.Y, lastPoint.X, _nextPoint.Y); listOfDrawnPoints.AddRange(this.EnumerateLineNoDiagonalSteps(lastPoint.X, _nextPoint.Y, _nextPoint.X, _nextPoint.Y)); listOfDrawnPoints.AddRange(this.EnumerateLineNoDiagonalSteps(_nextPoint.X, _nextPoint.Y, _nextPoint.X, lastPoint.Y)); listOfDrawnPoints.AddRange(this.EnumerateLineNoDiagonalSteps(_nextPoint.X, lastPoint.Y, lastPoint.X, lastPoint.Y)); foreach (var x in listOfDrawnPoints) { drawnPointList.Add(new DrawnPoint(directBitmapOrig.GetPixel(x.X, x.Y), x)); } foreach (var x in listOfDrawnPoints) { // drawnPointList.Add(new DrawnPoint(bitmap.GetPixel(x.X,x.Y), x)); directBitmap.SetPixel(x.X, x.Y, Color.Black); } using (Graphics g = Graphics.FromImage(pictureBox1.Image)) { g.DrawImage(directBitmap.Bitmap, new Point(0, 0)); } pictureBox1.Invalidate(); int Width = Math.Abs(XStart - XEnd); int Height = Math.Abs(YStart - YEnd); Color[] valuesToPass = new Color[Width * Height]; int xInner = 0; int yInner = 0; for (int x = XStart; x < XEnd; x++) { for (int y = YStart; y < YEnd; y++) { valuesToPass[xInner + (yInner * Width)] = directBitmapOrig.GetPixel(x, y); yInner++; } yInner = 0; xInner++; } Form3 form3 = new Form3(Height, Width, valuesToPass, lastPoint, _nextPoint, this); form3.Show(); _nextPoint = Point.Empty; _lastPoint = Point.Empty; } break; case DrawingType.FreeHand: lastPoint = e.Location; //we assign the lastPoint to the current mouse position: e.Location ('e' is from the MouseEventArgs passed into the MouseDown event) drawnPointList.Add(new DrawnPoint(Color.Pink, lastPoint)); isMouseDown = true; //we set to true because our mouse button is down (clicked) break; case DrawingType.Line: if (lastPoint == Point.Empty) { lastPoint = e.Location; } else { _nextPoint = e.Location; using (Graphics g = Graphics.FromImage(pictureBox1.Image)) { listOfDrawnPoints = this.EnumerateLineNoDiagonalSteps(lastPoint.X, lastPoint.Y, _nextPoint.X, _nextPoint.Y); foreach (var x in listOfDrawnPoints) { Color color = directBitmapOrig.GetPixel(x.X, x.Y); HSVBit hsvBit = directBitmapOrig.GetPixelHSV(x.X, x.Y); drawnPointList.Add(new DrawnPoint(directBitmapOrig.GetPixel(x.X, x.Y), x)); histogramDataLineRGB.redHisto[color.R]++; histogramDataLineRGB.blueHisto[color.B]++; histogramDataLineRGB.greenHisto[color.G]++; histogramDataLineRGB.HSV_HHisto.TryGetValue(hsvBit.HUE, out helperValue); histogramDataLineRGB.HSV_HHisto[hsvBit.HUE] = helperValue + 1; histogramDataLineRGB.HSV_SHisto.TryGetValue(hsvBit.saturation, out helperValue); histogramDataLineRGB.HSV_SHisto[hsvBit.saturation] = helperValue + 1; histogramDataLineRGB.HSV_VHisto.TryGetValue(hsvBit.value, out helperValue); histogramDataLineRGB.HSV_VHisto[hsvBit.value] = helperValue + 1; //formCaller.histogramDataLineRGBProp = histogramDataLineRGB; formCaller.ser1.Points.AddXY(color.R, histogramDataLineRGB.redHisto[color.R]); formCaller.ser2.Points.AddXY(color.G, histogramDataLineRGB.greenHisto[color.G]); formCaller.ser3.Points.AddXY(color.B, histogramDataLineRGB.blueHisto[color.B]); formCaller.seriesHExt.Points.AddXY(hsvBit.HUE, histogramDataLineRGB.HSV_HHisto[hsvBit.HUE]); formCaller.seriesSExt.Points.AddXY(hsvBit.saturation, histogramDataLineRGB.HSV_SHisto[hsvBit.saturation]); formCaller.seriesVExt.Points.AddXY(hsvBit.value, histogramDataLineRGB.HSV_VHisto[hsvBit.value]); } foreach (var x in listOfDrawnPoints) { directBitmap.SetPixel(x.X, x.Y, Color.Black); } g.DrawImage(directBitmap.Bitmap, new Point(0, 0)); } pictureBox1.Invalidate(); //refreshes the picturebox lastPoint = new Point(); drawnPointList.Add(new DrawnPoint(directBitmap.GetPixel(lastPoint.X, lastPoint.Y), lastPoint)); } break; case DrawingType.ManyLines: if (lastPoint == Point.Empty) { lastPoint = e.Location; } else { _nextPoint = e.Location; using (Graphics g = Graphics.FromImage(pictureBox1.Image)) { listOfDrawnPoints = this.EnumerateLineNoDiagonalSteps(lastPoint.X, lastPoint.Y, _nextPoint.X, _nextPoint.Y); foreach (var x in listOfDrawnPoints) { HSVBit hsvBit = directBitmapOrig.GetPixelHSV(x.X, x.Y); Color color = directBitmapOrig.GetPixel(x.X, x.Y); drawnPointList.Add(new DrawnPoint(directBitmapOrig.GetPixel(x.X, x.Y), x)); histogramDataLineRGB.redHisto[color.R]++; histogramDataLineRGB.blueHisto[color.B]++; histogramDataLineRGB.greenHisto[color.G]++; //formCaller.histogramDataLineRGBProp = histogramDataLineRGB; formCaller.ser1.Points.AddXY(color.R, histogramDataLineRGB.redHisto[color.R]); formCaller.ser2.Points.AddXY(color.G, histogramDataLineRGB.greenHisto[color.G]); formCaller.ser3.Points.AddXY(color.B, histogramDataLineRGB.blueHisto[color.B]); histogramDataLineRGB.HSV_HHisto.TryGetValue(hsvBit.HUE, out helperValue); histogramDataLineRGB.HSV_HHisto[hsvBit.HUE] = helperValue + 1; histogramDataLineRGB.HSV_SHisto.TryGetValue(hsvBit.saturation, out helperValue); histogramDataLineRGB.HSV_SHisto[hsvBit.saturation] = helperValue + 1; histogramDataLineRGB.HSV_VHisto.TryGetValue(hsvBit.value, out helperValue); histogramDataLineRGB.HSV_VHisto[hsvBit.value] = helperValue + 1; formCaller.seriesHExt.Points.AddXY(hsvBit.HUE, histogramDataLineRGB.HSV_HHisto[hsvBit.HUE]); formCaller.seriesSExt.Points.AddXY(hsvBit.saturation, histogramDataLineRGB.HSV_SHisto[hsvBit.saturation]); formCaller.seriesVExt.Points.AddXY(hsvBit.value, histogramDataLineRGB.HSV_VHisto[hsvBit.value]); } foreach (var x in listOfDrawnPoints) { // drawnPointList.Add(new DrawnPoint(bitmap.GetPixel(x.X,x.Y), x)); directBitmap.SetPixel(x.X, x.Y, Color.Black); } g.DrawImage(directBitmap.Bitmap, new Point(0, 0)); } pictureBox1.Invalidate(); //refreshes the picturebox lastPoint = _nextPoint; //keep assigning the lastPoint to the current mouse position drawnPointList.Add(new DrawnPoint(directBitmap.GetPixel(lastPoint.X, lastPoint.Y), lastPoint)); } break; } }