/// <summary> /// Scans the row to determine the representative luminousity of the top most edge /// </summary> /// <param name="image"> Image to analyzed </param> /// <param name="col"> Number of the row to be scanned </param> /// <param name="buffer"> Number off residual pixels to be skipped </param> /// <param name="signal_size"> Number of pixels to be sampled to determine the representative Luminousity </param> /// <param name="weighting"> Weighting to average samples set with </param> /// <returns> Single representative double value </returns> public static double ScanRowFromTop(Image <Bgra, byte> image, int col, int buffer, int signal_size, double[] weighting) { int signalStart = (int)Utility.Defaults.Ignore; for (int ii = 0; ii < image.Height; ii++) { if (image[ii, col].Alpha == byte.MaxValue) { signalStart = ii; break; } } if (signalStart == (int)Utility.Defaults.Ignore) { return(Utility.Defaults.Ignore); } signalStart += buffer; if (signalStart + signal_size >= image.Height) { return(Utility.Defaults.Ignore); } Bgra[] pixels = new Bgra[signal_size]; for (int ii = 0; ii < signal_size; ii++) { pixels[ii] = image[ii + signalStart, col]; } return(RepresentativeLuma(pixels, weighting)); }
private void highlightCurrentCell(int cellNum) { int circleRadius = 4; //radius of the circles in the plot int circleThickness = 2; Bgra highlightColour = new Bgra(0, 255, 255, 255); //yellow highlight int imageNum = 1; foreach (var pointsImage in imagePointsList) { for (int i = 0; i < cellArray.GetLength(0); i++) { //Draw a highlighting circle around the current cell selected if (cellArray[i, 0] == currentCell && cellArray[i, 1] <= imageNum) { float centerX = (float)cellArray[i, 2]; float centerY = (float)cellArray[i, 3]; System.Drawing.PointF center = new System.Drawing.PointF(centerX, centerY); CircleF circle = new CircleF(center, circleRadius); pointsImage.Draw(circle, highlightColour, circleThickness); } } imageNum++; } }
/// <summary> /// Scans the row to determine the representative luminousity of the right most edge /// </summary> /// <param name="image"> Image to analyzed </param> /// <param name="row"> Number of the row to be scanned </param> /// <param name="buffer"> Number off residual pixels to be skipped </param> /// <param name="signal_size"> Number of pixels to be sampled to determine the representative Luminousity </param> /// <param name="weighting"> Weighting to average samples set with </param> /// <returns> Single representative double value </returns> public static double ScanRowFromRight(Image <Bgra, byte> image, int row, int buffer, int signal_size, double[] weighting) { int signalStart = (int)Utility.Defaults.Ignore; for (int ii = image.Width - 1; ii >= 0; ii--) { if (image[row, ii].Alpha == byte.MaxValue) { signalStart = ii; break; } } if (signalStart == (int)Utility.Defaults.Ignore) { return(Utility.Defaults.Ignore); } signalStart -= buffer; if (signalStart - signal_size < 0.0) { return(Utility.Defaults.Ignore); } Bgra[] pixels = new Bgra[signal_size]; for (int ii = 0; ii < signal_size; ii++) { pixels[ii] = image[row, signalStart - ii]; } return(RepresentativeLuma(pixels, weighting)); }
private void ConvertDirectToRGB(int width, int height, Image image, IEnumerable <SegmentBase> segments) { var StartFrameSegment = segments.OfType <StartOfFrame>().FirstOrDefault(); int cScale = StartFrameSegment.Components[0].HorizontalSamplingFactor / StartFrameSegment.Components[1].HorizontalSamplingFactor; var pixels = new Color[width * height]; Parallel.For( 0, height, y => { var yo = RowYOffset(y); var co = RowCOffset(y); for (int x = 0; x < width; x++) { byte red = YPixels[yo + x]; byte green = CBPixels[co + x / cScale]; byte blue = CRPixels[co + x / cScale]; pixels[(y * width) + x] = new Bgra(red, green, blue); } }); image.ReCreate(width, height, pixels); }
public static void SetBlue(this Image <Bgra, Byte> image, Point point, double blue) { Bgra bgra = image[point].Clone(); bgra.Blue = blue; image[point] = bgra; }
public static void SetRed(this Image <Bgra, Byte> image, Point point, double red) { Bgra bgra = image[point].Clone(); bgra.Red = red; image[point] = bgra; }
public static void SetGreen(this Image <Bgra, Byte> image, Point point, double green) { Bgra bgra = image[point].Clone(); bgra.Green = green; image[point] = bgra; }
/// <summary> /// Add a color into the tree /// </summary> /// <param name="pixel"> /// The color /// </param> /// <param name="colorBits"> /// The number of significant color bits /// </param> /// <param name="level"> /// The level in the tree /// </param> /// <param name="octree"> /// The tree to which this node belongs /// </param> public void AddColor(Bgra pixel, int colorBits, int level, Octree octree) { // Update the color information if this is a leaf if (this.leaf) { this.Increment(pixel); // Setup the previous node octree.TrackPrevious(this); } else { // Go to the next level down in the tree int shift = 7 - level; int index = ((pixel.Red & Mask[level]) >> (shift - 2)) | ((pixel.Green & Mask[level]) >> (shift - 1)) | ((pixel.Blue & Mask[level]) >> shift); Node child = this.children[index]; if (child == null) { // Create a new child node and store it in the array child = new Node(level + 1, colorBits, octree); this.children[index] = child; } // Add the color to the child node child.AddColor(pixel, colorBits, level + 1, octree); } }
/// <summary> /// Increment the pixel count and add to the color information /// </summary> /// <param name="pixel"> /// The pixel to add. /// </param> public void Increment(Bgra pixel) { this.pixelCount++; this.red += pixel.Red; this.green += pixel.Green; this.blue += pixel.Blue; }
public static void SetAlpha(this Image <Bgra, Byte> image, Point point, double alpha) { Bgra bgra = image[point].Clone(); bgra.Alpha = alpha; image[point] = bgra; }
private Image <Bgra, byte> AddImages(Image <Bgra, byte>[] imageArray) { if (imageArray == null || imageArray.Length == 0) { throw new Exception("wtf parameters are empty bro"); } int _height = imageArray[0].Height; int _width = imageArray[0].Width; foreach (Image <Bgra, byte> i in imageArray) { if (i.Height != _height || i.Width != _width) { throw new Exception("Array of images are not of the same dimensions"); } } //data cleaned Image <Bgra, byte> output = EngineTools.CreateTransparent(_width, _height); for (int x = 0; x < _width; x++) { for (int y = 0; y < _height; y++) { Bgra pixeldata = new Bgra(0, 0, 0, 0); List <double> alphaArray = new List <double>(); for (int i = 0; i < imageArray.Length; i++) //check to see if layers have anything on them at all, skip math if there is { alphaArray.Add(imageArray[i][y, x].Alpha); } if (alphaArray.Sum() <= 0.001) { goto SkipSum; } for (int i = 0; i < imageArray.Length; i++) { if (imageArray[i][y, x].Alpha > 0.001) { double a1 = imageArray[i][y, x].Alpha / 255; double r1 = imageArray[i][y, x].Red; double g1 = imageArray[i][y, x].Green; double b1 = imageArray[i][y, x].Blue; double a2 = pixeldata.Alpha / 255; double r2 = pixeldata.Red; double g2 = pixeldata.Green; double b2 = pixeldata.Blue; //stacking value 2 over value 1 //additive compositing pixeldata.Alpha = (a2 * (1 - a1) + a1) * 255; //x255 because scale of 0-255 pixeldata.Red = r2 * a2 * (1 - a1) + r1 * a1; pixeldata.Green = g2 * a2 * (1 - a1) + g1 * a1; pixeldata.Blue = b2 * a2 * (1 - a1) + b1 * a1; } } SkipSum: //cancel excecution of adding nothing to itself output[y, x] = pixeldata; } } return(output); }
/// <summary> /// Extracts a single object given the corresponding mask and rectangle /// </summary> /// <param name="TheBlob">blob taken from mask</param> /// <param name="Source">source rectangle from source</param> /// <returns>a bitmap of the extracted source</returns> private static Bitmap ExtractSingleImage(Bitmap TheBlob, Bitmap Source) { int width = TheBlob.Width; int height = TheBlob.Height; Bgr Background = new Bgr(Color.Black); Bgra FullAlpha = new Bgra(1, 13, 37, 0); //clear Emgu.CV.Image <Bgra, Byte> Extracted = new Image <Bgra, byte>(width, height); Emgu.CV.Image <Bgr, Byte> blob = new Image <Bgr, byte>(TheBlob); Emgu.CV.Image <Bgr, Byte> src = new Image <Bgr, byte>(Source); log.Debug("Extract Single Image out of original using Blob Mask"); for (int ii = 0; ii < width; ii++) { for (int jj = 0; jj < height; jj++) { if (Utility.IsEqual(Background, blob[jj, ii])) { //set extracted to full alpha Extracted[jj, ii] = FullAlpha; } else { Extracted[jj, ii] = new Bgra(src[jj, ii].Blue, src[jj, ii].Green, src[jj, ii].Red, 255); } } } log.Info("Return processed blob"); return(Extracted.ToBitmap()); }
public void Draw(Bgra color) { lock (control.Context) { control.Context.Draw(ellipse, color, -1); } }
/// <summary> /// Rectification filter for projective transformation. /// <para>Accord.NET internal call. Please see: <see cref="Accord.Imaging.Filters.Rectification"/> for details.</para> /// </summary> /// <param name="img">Image.</param> /// <param name="homography">The homography matrix used to map a image passed to the filter to the overlay image.</param> /// <param name="fillColor">The filling color used to fill blank spaces.</param> /// <returns>Rectified image.</returns> public static Bgra <byte>[,] Rectification(this Bgra <byte>[,] img, double[,] homography, Bgra <byte> fillColor) { Rectification r = new Rectification(homography); r.FillColor = fillColor.ToColor(); return(img.ApplyBaseTransformationFilter(r)); }
public void GetPixelDelta_SameColors_ReturnsZero() { var left = new Bgra(0, 0, 0, 255); var right = new Bgra(0, 0, 0, 255); var result = _testClass.GetPixelDelta(left, right); Assert.AreEqual(result, 0.0); }
public void GetPixelDelta_ValidColors_ReturnsCorrectValue() { var left = new Bgra(47, 125, 43, 255); var right = new Bgra(125, 53, 195, 255); var result = _testClass.GetPixelDelta(left, right); Assert.IsTrue(Math.Abs(185.3 - result) < 0.1); }
/// <summary> /// Given an array of pixels, a weighted average of the Luma value for that array is returned /// </summary> /// <param name="colors"> Array of pixels to be Luma-ed and averaged </param> /// <param name="weightings"> A double array of weightings </param> /// <returns> Single representative Luminousity Value </returns> public static double RepresentativeLuma(Bgra[] colors, double[] weightings) { double sum = 0; for (int ii = 0; ii < colors.Length; ii++) { sum += weightings[ii]*Luma(colors[ii]); } return sum; }
private Bgra Lerp(Bgra from, Bgra to, double percent) { byte a = Saturate(Lerp(from.Alpha, to.Alpha, percent), 0, 255); byte r = Saturate(Lerp(from.Red, to.Red, percent), 0, 255); byte g = Saturate(Lerp(from.Green, to.Green, percent), 0, 255); byte b = Saturate(Lerp(from.Blue, to.Blue, percent), 0, 255); return(new Bgra(b, g, r, a)); }
public void RepresentativeLuminousityTest() { const int imgSize = 100; // Create a Square Point[] square = new Point[4]; square[0] = new Point(25, 25); square[1] = new Point(75, 25); square[2] = new Point(75, 75); square[3] = new Point(25, 75); var backgroundColor = new Bgra(0, 0, 0, 0); var foregroundColor = new Bgra(255, 255, 255, 255); // Create an Original Image var original = new Image <Bgra, Byte>(imgSize, imgSize, backgroundColor); original.FillConvexPoly(square, foregroundColor); // Create an Expected output array var expected = new double[imgSize]; for (int ii = 0; ii < imgSize; ii++) { if (ii >= 25 && ii <= 75) { expected[ii] = Luminousity.Luma(foregroundColor); } else { expected[ii] = Utility.Defaults.Ignore; } } // Perform from the top, left right and bottom var actualLeft = Luminousity.RepresentativeLuminousity(original, 1, 5, Direction.FromLeft); var actualRight = Luminousity.RepresentativeLuminousity(original, 1, 5, Direction.FromRight); var actualTop = Luminousity.RepresentativeLuminousity(original, 1, 5, Direction.FromTop); var actualBottom = Luminousity.RepresentativeLuminousity(original, 1, 5, Direction.FromBottom); // Check that lengths match Assert.IsTrue(actualBottom.Length == expected.Length); Assert.IsTrue(actualLeft.Length == expected.Length); Assert.IsTrue(actualRight.Length == expected.Length); Assert.IsTrue(actualTop.Length == expected.Length); // Check that the values match for (int ii = 0; ii < expected.Length; ii++) { Assert.IsTrue(Math.Abs(actualBottom[ii] - expected[ii]) < 0.001); Assert.IsTrue(Math.Abs(actualLeft[ii] - expected[ii]) < 0.001); Assert.IsTrue(Math.Abs(actualRight[ii] - expected[ii]) < 0.001); Assert.IsTrue(Math.Abs(actualTop[ii] - expected[ii]) < 0.001); } Console.WriteLine("Luminousity Scanning Tests Succesful!"); }
private static IImage GetEmptyBitmap() { var writeableBitmap = new WriteableBitmap(10, 10, 96, 96, PixelFormats.Bgr24, new BitmapPalette(new List <Color> { Color.FromRgb(0, 0, 0) })); var bmpSrc = Extensions.ConvertWriteableBitmapToBitmapImage(writeableBitmap); Bgra <byte>[,] colorImg = bmpSrc.ToArray <Bgra <byte> >(); return(colorImg.Lock()); }
public void refreshImagePoints() { imagePointsList.Clear(); Bgra transparentBGRA = new Bgra(0, 0, 0, 0); for (int i = 0; i < numImages; i++) { Image <Bgra, Byte> imagePoints = new Image <Bgra, Byte>(imageList[currentImage].Width, imageList[currentImage].Height, transparentBGRA); imagePointsList.Add(imagePoints); } }
/// <summary> /// Quantizes the pixel. /// </summary> /// <param name="pixel">The pixel.</param> /// <returns>The resulting byte</returns> protected byte QuantizePixel(Bgra pixel) { var paletteIndex = (byte)colors; if (pixel.Alpha > TransparencyThreshold) { paletteIndex = (byte)octree.GetPaletteIndex(pixel); } return(paletteIndex); }
public void HighlightCountry(int index, Bgra color) { animStart = color; animCountry = index; animDirection = 1; if (!animClock.Running) { animClock.Start(); } }
public void TestFindColorAtCenter_NoCenters() { Point[] blockCenters = new Point[1]; Image <Bgra, Byte> inputImg = new Image <Bgra, byte>(3, 3); inputImg[1, 1] = new Bgra(255, 255, 255, 0); List <BlockData> actualColors = detector.FindColorAtCenters(blockCenters, inputImg); AssertColorsMatch(blockCenters, inputImg, actualColors); }
protected override void OnPaint(PaintEventArgs e) { if (mapData != null && !DesignMode) { if (animCountry != -1) { double percent = animClock.Elapsed / (double)highlightTime; if (animDirection == 1) { Bgra current = Lerp(animStart, animEnd, percent); graphics[animCountry].Draw(current); } else { Bgra current = Lerp(animEnd, animStart, percent); graphics[animCountry].Draw(current); } if (animClock.Elapsed >= highlightTime - animClock.TickTime) { animClock.Reset(); animDirection = -animDirection; } } else { animClock.Stop(); animClock.Reset(); animDirection = 0; } if (context != null) { lock (context) { try { Graphics gfx = e.Graphics; gfx.DrawImage(context.Bitmap, 0, 0, Width, Height); } catch (Exception ex) { Debug.WriteLine(ex.Message, "Fatal"); } } } } else { Graphics gfx = e.Graphics; gfx.Clear(BackColor); } }
public void RepresentativeLuminousityTest() { const int imgSize = 100; // Create a Square Point[] square = new Point[4]; square[0] = new Point(25, 25); square[1] = new Point(75, 25); square[2] = new Point(75, 75); square[3] = new Point(25, 75); var backgroundColor = new Bgra(0, 0, 0, 0); var foregroundColor = new Bgra(255, 255, 255, 255); // Create an Original Image var original = new Image<Bgra, Byte>(imgSize, imgSize, backgroundColor); original.FillConvexPoly(square, foregroundColor); // Create an Expected output array var expected = new double[imgSize]; for (int ii = 0; ii < imgSize; ii++) { if (ii >= 25 && ii <= 75) { expected[ii] = Luminousity.Luma(foregroundColor); } else { expected[ii] = Utility.Defaults.Ignore; } } // Perform from the top, left right and bottom var actualLeft = Luminousity.RepresentativeLuminousity(original, 1, 5, Direction.FromLeft); var actualRight = Luminousity.RepresentativeLuminousity(original, 1, 5, Direction.FromRight); var actualTop = Luminousity.RepresentativeLuminousity(original, 1, 5, Direction.FromTop); var actualBottom = Luminousity.RepresentativeLuminousity(original, 1, 5, Direction.FromBottom); // Check that lengths match Assert.IsTrue(actualBottom.Length == expected.Length); Assert.IsTrue(actualLeft.Length == expected.Length); Assert.IsTrue(actualRight.Length == expected.Length); Assert.IsTrue(actualTop.Length == expected.Length); // Check that the values match for (int ii = 0; ii < expected.Length; ii++) { Assert.IsTrue(Math.Abs(actualBottom[ii] - expected[ii]) < 0.001); Assert.IsTrue(Math.Abs(actualLeft[ii] - expected[ii]) < 0.001); Assert.IsTrue(Math.Abs(actualRight[ii] - expected[ii]) < 0.001); Assert.IsTrue(Math.Abs(actualTop[ii] - expected[ii]) < 0.001); } Console.WriteLine("Luminousity Scanning Tests Succesful!"); }
public List <BlockData> FindColorAtCenters(Point[] centers, Image <Bgra, Byte> image) { List <BlockData> allBlockDetails = new List <BlockData>(); int currBlockId = 1; foreach (Point center in centers) { Bgra color = image[center.Y, center.X]; allBlockDetails.Add(new BlockData(currBlockId, center.X, center.Y, color.Red, color.Green, color.Blue)); currBlockId++; } return(allBlockDetails); }
public void Draw(Bgra color) { if (contours != null) { lock (control.Context) { for (int i = 0; i < contours.Length; i++) { control.Context.Draw(contours[i], color, -1); } } } }
public void TestFindColorAtCenter_SingleCenterRedColor() { Point[] blockCenters = new Point[1] { new Point(1, 1) }; Image <Bgra, Byte> inputImg = new Image <Bgra, byte>(3, 3); inputImg[1, 1] = new Bgra(10, 10, 200, 0); List <BlockData> actualColors = detector.FindColorAtCenters(blockCenters, inputImg); AssertColorsMatch(blockCenters, inputImg, actualColors); }
private void flipTextureToolStripMenuItem_Click(object sender, EventArgs e) { if (flipUVsToolStripMenuItem.Checked) { flipUVsToolStripMenuItem.Checked = false; flipTextureToolStripMenuItem.Checked = true; } int x = (int.Parse(texSize.Text.Split('x')[0])); int y = (int.Parse(texSize.Text.Split('x')[1])); Bitmap imagething = new Bitmap(x, y); texconv.textureGraphics = Graphics.FromImage(imagething); Bitmap imagefile = new Bitmap(Path.GetDirectoryName(File.ReadAllLines("settings.bmfo")[0]) + "\\" + comboTexture.Text); Image <Bgra, byte> image = new Image <Bgra, byte>(Path.GetDirectoryName(File.ReadAllLines("settings.bmfo")[0]) + "\\" + comboTexture.Text); double[] b = new double[imagefile.Height * imagefile.Width]; double[] g = new double[imagefile.Height * imagefile.Width]; double[] r = new double[imagefile.Height * imagefile.Width]; double[] a = new double[imagefile.Height * imagefile.Width]; y = imagefile.Height - 1; if (flipUVsToolStripMenuItem.Checked) { y = 0; } x = 0; for (int xy = 0; xy < imagefile.Height * imagefile.Width; xy++) { if (x == imagefile.Width) { if (flipUVsToolStripMenuItem.Checked) { y += 2; } y--; x = 0; } Bgra pixel = image[y, x]; b[xy] = pixel.Blue; g[xy] = pixel.Green; r[xy] = pixel.Red; a[xy] = pixel.Alpha; x++; } double[][] rgba = { r, g, b, a }; fixCI(); for (int xy = 0; xy < imagefile.Height * imagefile.Width; xy++) { texconv.CalculateTex(rgba, comboFormat.Text, int.Parse(comboBpp.Text), xy, true, texSize.Text); } texturePreview.Image = imagething; }
public void TestFindColorAtCenter_MultipleCenters() { Point[] blockCenters = new Point[2] { new Point(1, 1), new Point(2, 0) }; Image <Bgra, Byte> inputImg = new Image <Bgra, byte>(3, 3); inputImg[0, 2] = new Bgra(255, 255, 255, 0); inputImg[1, 1] = new Bgra(105, 5, 58, 0); List <BlockData> actualColors = detector.FindColorAtCenters(blockCenters, inputImg); AssertColorsMatch(blockCenters, inputImg, actualColors); }
public void ArrayRepresentativeTest() { Console.WriteLine("Starting Luma( Color Array) Test"); var colors = new Bgra[4]; colors[0] = new Bgra(0, 0, 100, 0); colors[1] = new Bgra(0, 0, 90, 0); colors[2] = new Bgra(0, 0, 100, 0); colors[3] = new Bgra(0, 0, 100, 0); var expected = Luminousity.Luma(new Bgra(0, 0, (40 + 27 + 20 + 10), 0)); var actual = Luminousity.RepresentativeLuma(colors, Luminousity.LinearWeighting(colors.Length)); Assert.IsTrue(Math.Abs(actual - expected) < 0.01); Console.WriteLine("Representative of Array Luma Test Successful"); }
public Image<Bgra, byte> GetImage(string text) { var textCharacters = text.Select(l => _characters.First(c => c.Character == l)).ToList(); var image = new Image<Bgra, byte>( 1 + textCharacters.Sum(c => Math.Max(c.Width, c.Rectangle.Width)) + textCharacters.Sum(c => c.Offset.X), textCharacters.Max(c => c.Rectangle.Height) + textCharacters.Max(c => c.Offset.Y), new Bgra(228, 227, 201, 255)); int x = 1; foreach (var character in textCharacters) { if (character.Character == ' ') { x += character.Width; continue; } var targetRect = new Rectangle( x - character.Offset.X, character.Offset.Y, character.Rectangle.Width, character.Rectangle.Height); var mask = Bitmap.Copy(character.Rectangle).Convert<Gray, byte>(); var characterImage = new Image<Bgra, byte>(mask.Width, mask.Height); for (int cy = 0; cy < characterImage.Height; cy++) { for (int cx = 0; cx < characterImage.Width; cx++) { var alpha = mask[cy, cx].Intensity / 253; characterImage[cy, cx] = new Bgra( 101 * alpha + (228 * (1 - alpha)), 32 * alpha + (227 * (1 - alpha)), 21 * alpha + (201 * (1 - alpha)), 255); } } characterImage.CopyTo(image.GetSubRect(targetRect)); x += character.Width; } return image; }
public static Image<Bgra, byte> CombineAlpha(this Image<Bgra, byte> image, Image<Bgra, byte> alpha) { var intensity = alpha.Convert<Gray, byte>(); for (int y = 0; y < image.Height; y++) { for (int x = 0; x < image.Width; x++) { var existing = image[y, x]; image[y, x] = new Bgra( existing.Blue, existing.Green, existing.Red, intensity[y, x].Intensity); } } return image; }
public Picture Histogram() { var chans = 3; var bins = 256; var range = new RangeF(0, 255); var hist = new DenseHistogram(bins, range); var split = this.bgra.Split(); var colors = new Bgra[] { new Bgra(255, 0, 0, 255), new Bgra(0, 255, 0, 255), new Bgra(0, 0, 255, 255), }; var hip = new Picture(bins * chans, bins + 1); // Todo, plus one Jaap, really? Tssssk... wrote Jaap to himself. hip.Bgra.SetValue(Color.Black.ToBgra()); for (int chan = 0; chan < chans; ++chan) { hist.Calculate<byte>(new Image<Gray, byte>[] { split[chan] }, false, null); // Todo, Jaap, December 2010, hist.Normalize(bins - 1); float min, max; int[] minLoc, maxLoc; hist.MinMax(out min, out max, out minLoc, out maxLoc); if (max == min) continue; var scale = 255.0f / (max - min); for (int x = 0; x < bins; ++x) { var n = hip.Height - (int)(hist[x] * scale); for (int y = hip.Height - 1; y > n; --y) hip.Bgra[y, x + chan * bins] = colors[chan]; } } foreach (var c in split) c.Dispose(); return hip; }
public static Image Create(Draw.Image image) { Image result = null; if (image is Raster.Image) { if (image is Raster.Bgra) result = new Bgra(image as Raster.Bgra); else if (image is Raster.Bgr) result = new Bgr(image as Raster.Bgr); else if (image is Raster.Monochrome) result = new Monochrome(image as Raster.Monochrome); else if (image is Raster.Yuv420) result = new Yuv420(image as Raster.Yuv420); else result = new Bgra(image.Convert<Raster.Bgra>()); } else if (image is Image) result = image.Copy() as Image; else if (image.NotNull()) result = new Bgra(image.Convert<Raster.Bgra>()); return result; }
private unsafe byte[] Threshold(byte[] frame) { byte[] result = new byte[KinectManager.ColorSize * 4]; var sw = Stopwatch.StartNew(); fixed (byte* bgraPtr = frame) { var pBGR = (Bgra*)bgraPtr; fixed (byte* resultPtr = result) { var dst = (Bgra*)resultPtr; var defaultColor = new Bgra() { Blue = 44, Green = 250, Red = 88, Alpha = 255 }; var tooNearColor = new Bgra() { Blue = 88, Green = 44, Red = 250, Alpha = 255 }; var tooFarColor = new Bgra() { Blue = 250, Green = 44, Red = 88, Alpha = 255 }; for (int colorIndex = 0; colorIndex < KinectManager.ColorSize; ++colorIndex) { DepthSpacePoint dsp = depthSpaceData[colorIndex]; var src = &defaultColor; if (dsp.X != float.NegativeInfinity && dsp.Y != -float.NegativeInfinity) { int dx = (int)Math.Round(dsp.X); int dy = (int)Math.Round(dsp.Y); if (0 <= dx && dx < KinectManager.DepthWidth && 0 <= dy && dy < KinectManager.DepthHeight) { int depth = depthData[dx + dy * KinectManager.DepthWidth]; if (depth < NearThreshold) src = &tooNearColor; else if (depth > FarThreshold) src = &tooFarColor; else src = pBGR + colorIndex; } } dst[colorIndex] = *src; } } } Debug.WriteLine($"Thresholding took {sw.ElapsedMilliseconds} ms"); return result; }
/// <summary> /// Converts an multichannel pixel into a single one with the Luma Wieghting /// </summary> /// <param name="color"> Pixel </param> /// <returns> Weighted Average of Channels </returns> public static double Luma(Bgra color) { return (0.3*color.Red + 0.59*color.Green + 0.11*color.Blue); }
/// <summary> /// Scans the row to determine the representative luminousity of the top most edge /// </summary> /// <param name="image"> Image to analyzed </param> /// <param name="col"> Number of the row to be scanned </param> /// <param name="buffer"> Number off residual pixels to be skipped </param> /// <param name="signal_size"> Number of pixels to be sampled to determine the representative Luminousity </param> /// <param name="weighting"> Weighting to average samples set with </param> /// <returns> Single representative double value </returns> public static double ScanRowFromTop(Image<Bgra, byte> image, int col, int buffer, int signal_size, double[] weighting) { int signalStart = (int) Utility.Defaults.Ignore; for (int ii = 0; ii < image.Height; ii++) { if (image[ii, col].Alpha == byte.MaxValue) { signalStart = ii; break; } } if (signalStart == (int) Utility.Defaults.Ignore) { return Utility.Defaults.Ignore; } signalStart += buffer; if (signalStart + signal_size >= image.Height) { return Utility.Defaults.Ignore; } Bgra[] pixels = new Bgra[signal_size]; for (int ii = 0; ii < signal_size; ii++) { pixels[ii] = image[ii + signalStart, col]; } return RepresentativeLuma(pixels, weighting); }
public static Image AsImage(this System.Drawing.Bitmap me) { Image result = null; if (me.NotNull()) { if (me.PixelFormat != System.Drawing.Imaging.PixelFormat.Format24bppRgb && me.PixelFormat != System.Drawing.Imaging.PixelFormat.Format32bppArgb) { // Bitmap data that we don't support we draw upon a ARGB bitmap and use that instead. System.Drawing.Bitmap newBitmap = new System.Drawing.Bitmap(me.Width, me.Height); using (System.Drawing.Graphics canvas = System.Drawing.Graphics.FromImage(newBitmap)) canvas.DrawImageUnscaled(me, 0, 0); me.Dispose(); me = newBitmap; } System.Drawing.Imaging.BitmapData data = me.LockBits(new System.Drawing.Rectangle(0, 0, me.Width, me.Height), System.Drawing.Imaging.ImageLockMode.ReadOnly, me.PixelFormat); Buffer.Sized buffer = new Buffer.Sized(data.Scan0, data.Stride * data.Height, (pointer) => { if (me.NotNull()) me.Dispose(); }); switch (me.PixelFormat) { case System.Drawing.Imaging.PixelFormat.Format24bppRgb: result = new Bgr(buffer, new Geometry2D.Integer.Size(me.Width, me.Height)); break; case System.Drawing.Imaging.PixelFormat.Format32bppArgb: result = new Bgra(buffer, new Geometry2D.Integer.Size(me.Width, me.Height)); break; } } return result; }
/// <summary> /// Extracts a single object given the corresponding mask and rectangle /// </summary> /// <param name="TheBlob">blob taken from mask</param> /// <param name="Source">source rectangle from source</param> /// <returns>a bitmap of the extracted source</returns> private static Bitmap ExtractSingleImage(Bitmap TheBlob, Bitmap Source) { int width = TheBlob.Width; int height = TheBlob.Height; Bgr Background = new Bgr(Color.Black); Bgra FullAlpha = new Bgra(1, 13, 37, 0); //clear Emgu.CV.Image<Bgra, Byte> Extracted = new Image<Bgra, byte>(width, height); Emgu.CV.Image<Bgr, Byte> blob = new Image<Bgr, byte>(TheBlob); Emgu.CV.Image<Bgr, Byte> src = new Image<Bgr, byte>(Source); log.Info("Extract Single Image out of original using Blob Mask"); for (int ii = 0; ii < width; ii++) { for (int jj = 0; jj < height; jj++) { if (Utility.IsEqual(Background, blob[jj, ii])) { //set extracted to full alpha Extracted[jj, ii] = FullAlpha; } else { Extracted[jj, ii] = new Bgra(src[jj, ii].Blue, src[jj, ii].Green, src[jj, ii].Red, 255); } } } log.Info("Return processed blob"); return Extracted.ToBitmap(); }
/// <summary> /// Scans the row to determine the representative luminousity of the right most edge /// </summary> /// <param name="image"> Image to analyzed </param> /// <param name="row"> Number of the row to be scanned </param> /// <param name="buffer"> Number off residual pixels to be skipped </param> /// <param name="signal_size"> Number of pixels to be sampled to determine the representative Luminousity </param> /// <param name="weighting"> Weighting to average samples set with </param> /// <returns> Single representative double value </returns> public static double ScanRowFromRight(Image<Bgra, byte> image, int row, int buffer, int signal_size, double[] weighting) { int signalStart = (int) Utility.Defaults.Ignore; for (int ii = image.Width - 1; ii >= 0; ii--) { if (image[row, ii].Alpha == byte.MaxValue) { signalStart = ii; break; } } if (signalStart == (int) Utility.Defaults.Ignore) { return Utility.Defaults.Ignore; } signalStart -= buffer; if (signalStart - signal_size < 0.0) { return Utility.Defaults.Ignore; } Bgra[] pixels = new Bgra[signal_size]; for (int ii = 0; ii < signal_size; ii++) { pixels[ii] = image[row, signalStart - ii]; } return RepresentativeLuma(pixels, weighting); }
public Image<Bgra, byte> WearObject(Image<Bgra, byte> inputBgra, AccessoryOverlay accessory) { var bgraBlack = new Bgra(0, 0, 0, 0); if (accessory.FinalRectangle == Rectangle.Empty) { return inputBgra; } ConformRoi(accessory, inputBgra); var overlayRect = accessory.FinalRectangle; var resizeOverlayBgra = accessory.Overlay.Resize(overlayRect.Width, overlayRect.Height, Inter.Linear); var overlayTargetBgra = new Image<Bgra, byte>(inputBgra.Width, inputBgra.Height, bgraBlack); Log.Info(m => m("Overlay rect {0}", overlayRect)); overlayTargetBgra.ROI = overlayRect; resizeOverlayBgra.CopyTo(overlayTargetBgra); overlayTargetBgra.ROI = Rectangle.Empty; const bool useMask = true; if (useMask) { var overlayMask = new Image<Gray, Byte>(inputBgra.Width, inputBgra.Height); CvInvoke.CvtColor(overlayTargetBgra, overlayMask, ColorConversion.Bgr2Gray); overlayMask = overlayMask.ThresholdBinary(new Gray(1), new Gray(1)); inputBgra.SetValue(bgraBlack, overlayMask); } var outputBgra = inputBgra.AddWeighted(overlayTargetBgra, 1, 1, 1); inputBgra.ROI = Rectangle.Empty; outputBgra.ROI = Rectangle.Empty; return outputBgra; }
private static void AddRectList(BitmapFont font, string rectList) { rectList = rectList.Replace("Define RectList", String.Empty) .Replace(" ", String.Empty) .Replace(Environment.NewLine, String.Empty) .Replace("\n", String.Empty) .TrimStart('(') .TrimEnd(';', ')'); var rects = rectList.Split(new[] { "),(" }, StringSplitOptions.None); for (int i = 0; i < rects.Length; i++) { var r = rects[i].Replace(")", String.Empty) .Replace("(", String.Empty) .Split(new[] { "," }, StringSplitOptions.None); var rect = new Rectangle( Int32.Parse(r[0]), Int32.Parse(r[1]), Int32.Parse(r[2]), Int32.Parse(r[3])); font.Characters[i].Rectangle = rect; var matchImage = font.Bitmap.Copy(new Rectangle(rect.X, 6, rect.Width, 29)); for (int x = 0; x < matchImage.Width; x++) { for (int y = 0; y < matchImage.Height; y++) { var current = matchImage[y, x]; if (current.Alpha < 255) { matchImage[y, x] = new Bgra(0, 0, 0, 255); } else { matchImage[y, x] = new Bgra( current.Blue, current.Green, current.Red, 255); } } } font.Characters[i].MatchImage = matchImage; } }