private static unsafe SoftwareBitmap MatToSoftwareBitmap(this Mat input) { try { SoftwareBitmap output = new SoftwareBitmap(BitmapPixelFormat.Bgra8, input.Width, input.Height, BitmapAlphaMode.Premultiplied); using (BitmapBuffer Buffer = output.LockBuffer(BitmapBufferAccessMode.ReadWrite)) using (Windows.Foundation.IMemoryBufferReference Reference = Buffer.CreateReference()) { ((IMemoryBufferByteAccess)Reference).GetBuffer(out byte *DataInBytes, out uint capacity); BitmapPlaneDescription BufferLayout = Buffer.GetPlaneDescription(0); for (int i = 0; i < BufferLayout.Height; i++) { for (int j = 0; j < BufferLayout.Width; j++) { int Index = BufferLayout.StartIndex + (BufferLayout.Stride * i) + (4 * j); DataInBytes[Index] = input.DataPointer[Index]; DataInBytes[Index + 1] = input.DataPointer[Index + 1]; DataInBytes[Index + 2] = input.DataPointer[Index + 2]; DataInBytes[Index + 3] = input.DataPointer[Index + 3]; } } } return(output); } catch { return(new SoftwareBitmap(BitmapPixelFormat.Bgra8, input.Width, input.Height, BitmapAlphaMode.Premultiplied)); } }
public static unsafe SoftwareBitmap GetImage_() { var softwareBitmap = new SoftwareBitmap(BitmapPixelFormat.Bgra8, 100, 100, BitmapAlphaMode.Premultiplied); using (BitmapBuffer buffer = softwareBitmap.LockBuffer(BitmapBufferAccessMode.Write)) { using (var reference = buffer.CreateReference()) { byte *dataInBytes; uint capacity; ((IMemoryBufferByteAccess)reference).GetBuffer(out dataInBytes, out capacity); // Fill-in the BGRA plane BitmapPlaneDescription bufferLayout = buffer.GetPlaneDescription(0); for (int i = 0; i < bufferLayout.Height; i++) { for (int j = 0; j < bufferLayout.Width; j++) { byte value = (byte)((float)j / bufferLayout.Width * 255); dataInBytes[bufferLayout.StartIndex + bufferLayout.Stride * i + 4 * j + 0] = value; // Blue dataInBytes[bufferLayout.StartIndex + bufferLayout.Stride * i + 4 * j + 1] = value; // Green dataInBytes[bufferLayout.StartIndex + bufferLayout.Stride * i + 4 * j + 2] = value; // Red dataInBytes[bufferLayout.StartIndex + bufferLayout.Stride * i + 4 * j + 3] = 255; // Alpha } } } } return(softwareBitmap); }
public unsafe void CreateGraphicFile() { softwareBitmap = new SoftwareBitmap(BitmapPixelFormat.Bgra8, ImageWidth, ImageHeight, BitmapAlphaMode.Ignore); using (BitmapBuffer buffer = softwareBitmap.LockBuffer(BitmapBufferAccessMode.Write)) { using (var reference = buffer.CreateReference()) { byte *dataInBytes; uint capacity; ((IMemoryBufferByteAccess)reference).GetBuffer(out dataInBytes, out capacity); // Fill-in the BGRA plane BitmapPlaneDescription bufferLayout = buffer.GetPlaneDescription(0); for (int i = 0; i < bufferLayout.Width; i++) { for (int j = 0; j < bufferLayout.Height; j++) { Color tempColor = waveSamples[i].CheckArea(j, ImageHeight) ? ForegroundColor : BackgroundColor; //Blue dataInBytes[bufferLayout.StartIndex + bufferLayout.Stride * j + 4 * i + 0] = (byte)tempColor.B; //Green dataInBytes[bufferLayout.StartIndex + bufferLayout.Stride * j + 4 * i + 1] = (byte)tempColor.G; //Red dataInBytes[bufferLayout.StartIndex + bufferLayout.Stride * j + 4 * i + 2] = (byte)tempColor.R; //Alpha dataInBytes[bufferLayout.StartIndex + bufferLayout.Stride * j + 4 * i + 3] = (byte)tempColor.A; } } } } }
private unsafe void SoftwareBitmapChangeColorRed(SoftwareBitmap softwareBitmap) { using (BitmapBuffer buffer = softwareBitmap.LockBuffer(BitmapBufferAccessMode.Write)) { using (var reference = buffer.CreateReference()) { byte *dataInBytes; uint capacity; ((IMemoryBufferByteAccess)reference).GetBuffer(out dataInBytes, out capacity); // Fill-in the BGRA plane BitmapPlaneDescription bufferLayout = buffer.GetPlaneDescription(0); for (int i = 0; i < bufferLayout.Height; i++) { for (int j = 0; j < bufferLayout.Width; j++) { int pixelIndex = bufferLayout.StartIndex + bufferLayout.Stride * i + 4 * j; if (dataInBytes[pixelIndex + 3] != 0) { dataInBytes[pixelIndex + 0] = 0; dataInBytes[pixelIndex + 1] = 0; dataInBytes[pixelIndex + 2] = 255; } } } } } }
private unsafe void ChangeRandomBgColorPixel() { using (BitmapBuffer buffer = randomBgSoftwareBitmap.LockBuffer(BitmapBufferAccessMode.Write)) { using (var reference = buffer.CreateReference()) { byte *dataInBytes; uint capacity; ((IMemoryBufferByteAccess)reference).GetBuffer(out dataInBytes, out capacity); // Fill-in the BGRA plane BitmapPlaneDescription bufferLayout = buffer.GetPlaneDescription(0); double imgWidth = bufferLayout.Width; double imgHeight = bufferLayout.Height; for (int row = 0; row < imgHeight; row++) { for (int col = 0; col < imgWidth; col++) { double hue = col * (360 / imgWidth); HSVColor hsv = new HSVColor(hue, 1.0, 1.0); Color color = hsv.GetRGB(); int pixelIndex = bufferLayout.Stride * row + 4 * col; if (dataInBytes[pixelIndex + 3] != 0) { dataInBytes[pixelIndex + 0] = (byte)color.B; dataInBytes[pixelIndex + 1] = (byte)color.G; dataInBytes[pixelIndex + 2] = (byte)color.R; } } } } } }
private unsafe void CreateNewSoftwareBitmap() { // <SnippetCreateNewSoftwareBitmap> softwareBitmap = new SoftwareBitmap(BitmapPixelFormat.Bgra8, 100, 100, BitmapAlphaMode.Premultiplied); using (BitmapBuffer buffer = softwareBitmap.LockBuffer(BitmapBufferAccessMode.Write)) { using (var reference = buffer.CreateReference()) { byte *dataInBytes; uint capacity; ((IMemoryBufferByteAccess)reference).GetBuffer(out dataInBytes, out capacity); // Fill-in the BGRA plane BitmapPlaneDescription bufferLayout = buffer.GetPlaneDescription(0); for (int i = 0; i < bufferLayout.Height; i++) { for (int j = 0; j < bufferLayout.Width; j++) { byte value = (byte)((float)j / bufferLayout.Width * 255); dataInBytes[bufferLayout.StartIndex + bufferLayout.Stride * i + 4 * j + 0] = value; dataInBytes[bufferLayout.StartIndex + bufferLayout.Stride * i + 4 * j + 1] = value; dataInBytes[bufferLayout.StartIndex + bufferLayout.Stride * i + 4 * j + 2] = value; dataInBytes[bufferLayout.StartIndex + bufferLayout.Stride * i + 4 * j + 3] = (byte)255; } } } } // </SnippetCreateNewSoftwareBitmap> }
private unsafe void AssignPixelValues(SoftwareBitmap newSoftwareBitmap, SoftwareBitmap softwareBitmap) { using (BitmapBuffer buffer = softwareBitmap.LockBuffer(BitmapBufferAccessMode.Read)) using (BitmapBuffer newBuffer = newSoftwareBitmap.LockBuffer(BitmapBufferAccessMode.Write)) using (var reference = buffer.CreateReference()) using (var newReference = newBuffer.CreateReference()) { byte *dataInBytes; byte *newDataInBytes; uint capacity; ((IMemoryBufferByteAccess)reference).GetBuffer(out dataInBytes, out capacity); ((IMemoryBufferByteAccess)newReference).GetBuffer(out newDataInBytes, out capacity); // Fill-in the BGRA plane BitmapPlaneDescription bufferLayout = buffer.GetPlaneDescription(0); for (int i = 0; i < bufferLayout.Height; i++) { for (int j = 0; j < bufferLayout.Width; j++) { int pixelIndex = bufferLayout.StartIndex + bufferLayout.Stride * i + 4 * j; if (newDataInBytes[pixelIndex + 3] != 0) { newDataInBytes[pixelIndex + 0] = dataInBytes[pixelIndex + 0]; newDataInBytes[pixelIndex + 1] = dataInBytes[pixelIndex + 1]; newDataInBytes[pixelIndex + 2] = dataInBytes[pixelIndex + 2]; } } } } }
/// <summary> /// PreProcessing. /// Converts image data in SoftwareBitmap to TensorFloat. /// </summary> public static TensorFloat SoftwareBitmapToTensorFloat(SoftwareBitmap image) { int width = image.PixelWidth; int height = image.PixelHeight; using (BitmapBuffer buffer = image.LockBuffer(BitmapBufferAccessMode.Read)) { using (var reference = buffer.CreateReference()) { // Implementation Reference: // https://github.com/Microsoft/Windows-Machine-Learning/issues/22 unsafe { ((IMemoryBufferByteAccess)reference).GetBuffer(out byte *dataInBytes, out uint capacity); long[] shape = { 1, 3, height, width }; float[] pCPUTensor = new float[3 * width * height]; for (int i = 0; i < capacity; i += 4) { int pixelInd = i / 4; pCPUTensor[pixelInd] = (float)dataInBytes[i]; pCPUTensor[(height * width) + pixelInd] = (float)dataInBytes[i + 1]; pCPUTensor[(height * width * 2) + pixelInd] = (float)dataInBytes[i + 2]; } float[] processedTensor = NormalizeFloatArray(pCPUTensor); TensorFloat tensorFloats = TensorFloat.CreateFromArray(shape, processedTensor); return(tensorFloats); } } } }
static unsafe SoftwareBitmap ColorizeMelspectrogram(SoftwareBitmap bwSpectrogram) { using (BitmapBuffer buffer = bwSpectrogram.LockBuffer(BitmapBufferAccessMode.Write)) { using var reference = buffer.CreateReference(); IMemoryBufferByteAccess memoryBuffer = reference.As <IMemoryBufferByteAccess>(); memoryBuffer.GetBuffer(out byte *dataInBytes, out uint capacity); // Edit the BGRA Plane BitmapPlaneDescription bufferLayout = buffer.GetPlaneDescription(0); for (int i = 0; i < bufferLayout.Height; i++) { for (int j = 0; j < bufferLayout.Width; j++) { int pixel = bufferLayout.StartIndex + bufferLayout.Stride * i + 4 * j; //Lines below can be tweaked for different custom color filters //Blue dataInBytes[pixel + 0] = (byte)((255 - dataInBytes[pixel + 0]) / 2); //Green dataInBytes[pixel + 1] = (byte)(dataInBytes[pixel + 1] / 2); //Red //dataInBytes[pixel + 2] = (byte)(dataInBytes[pixel + 2]); //Alpha - must leave each pixel at max dataInBytes[bufferLayout.StartIndex + bufferLayout.Stride * i + 4 * j + 3] = (byte)255; } } } return(bwSpectrogram); }
public static unsafe void Mat2SoftwareBitmap(Mat input, SoftwareBitmap output) { using (BitmapBuffer buffer = output.LockBuffer(BitmapBufferAccessMode.ReadWrite)) { using (var reference = buffer.CreateReference()) { ((IMemoryBufferByteAccess)reference).GetBuffer(out var dataInBytes, out var capacity); BitmapPlaneDescription bufferLayout = buffer.GetPlaneDescription(0); for (int i = 0; i < bufferLayout.Height; i++) { for (int j = 0; j < bufferLayout.Width; j++) { dataInBytes[bufferLayout.StartIndex + bufferLayout.Stride * i + 4 * j + 0] = input.DataPointer[bufferLayout.StartIndex + bufferLayout.Stride * i + 4 * j + 0]; dataInBytes[bufferLayout.StartIndex + bufferLayout.Stride * i + 4 * j + 1] = input.DataPointer[bufferLayout.StartIndex + bufferLayout.Stride * i + 4 * j + 1]; dataInBytes[bufferLayout.StartIndex + bufferLayout.Stride * i + 4 * j + 2] = input.DataPointer[bufferLayout.StartIndex + bufferLayout.Stride * i + 4 * j + 2]; dataInBytes[bufferLayout.StartIndex + bufferLayout.Stride * i + 4 * j + 3] = input.DataPointer[bufferLayout.StartIndex + bufferLayout.Stride * i + 4 * j + 3]; } } } } }
public unsafe object Detect(SoftwareBitmap image) { using (BitmapBuffer buffer = image.LockBuffer(BitmapBufferAccessMode.Write)) { using (var reference = buffer.CreateReference()) { byte* dataInBytes; uint capacity; ((IMemoryBufferByteAccess)reference).GetBuffer(out dataInBytes, out capacity); // Fill-in the BGRA plane BitmapPlaneDescription bufferLayout = buffer.GetPlaneDescription(0); for (int i = 0; i < bufferLayout.Height; i++) { for (int j = 0; j < bufferLayout.Width; j++) { int b = dataInBytes[bufferLayout.StartIndex + bufferLayout.Stride * i + 4 * j + 0] - Color.B; int g = dataInBytes[bufferLayout.StartIndex + bufferLayout.Stride * i + 4 * j + 1] - Color.G; int r = dataInBytes[bufferLayout.StartIndex + bufferLayout.Stride * i + 4 * j + 2] - Color.R; if (b * b + g * g + r * r < Delta * Delta) { dataInBytes[bufferLayout.StartIndex + bufferLayout.Stride * i + 4 * j + 0] = 0; dataInBytes[bufferLayout.StartIndex + bufferLayout.Stride * i + 4 * j + 1] = 0; dataInBytes[bufferLayout.StartIndex + bufferLayout.Stride * i + 4 * j + 2] = 255; dataInBytes[bufferLayout.StartIndex + bufferLayout.Stride * i + 4 * j + 3] = (byte)255; } } } } } return null; }
public void ExternalVideoSourceRawSample(uint durationMilliseconds, int width, int height, byte[] sample, VideoPixelFormatsEnum pixelFormat) { if (!_isClosed) { if (_vp8Encoder == null) { _vp8Encoder = new Vp8Codec(); _vp8Encoder.InitialiseEncoder((uint)width, (uint)height); } if (_encodeBmp == null) { _encodeBmp = new SoftwareBitmap(BitmapPixelFormat.Rgba8, width, height); } if (OnVideoSourceEncodedSample != null) { //byte[] i420Buffer = PixelConverter.RGBtoI420(rgb24Sample, width, height); //byte[] encodedBuffer = _vp8Encoder.Encode(i420Buffer, _forceKeyFrame); SetBitmapData(sample, _encodeBmp, pixelFormat); var nv12bmp = SoftwareBitmap.Convert(_encodeBmp, BitmapPixelFormat.Nv12); byte[] nv12Buffer = null; using (BitmapBuffer buffer = nv12bmp.LockBuffer(BitmapBufferAccessMode.Read)) { using (var reference = buffer.CreateReference()) { unsafe { byte *dataInBytes; uint capacity; ((IMemoryBufferByteAccess)reference).GetBuffer(out dataInBytes, out capacity); nv12Buffer = new byte[capacity]; Marshal.Copy((IntPtr)dataInBytes, nv12Buffer, 0, (int)capacity); } } } byte[] encodedBuffer = _vp8Encoder.Encode(nv12Buffer, _forceKeyFrame); if (encodedBuffer != null) { //Console.WriteLine($"encoded buffer: {encodedBuffer.HexStr()}"); uint fps = (durationMilliseconds > 0) ? 1000 / durationMilliseconds : DEFAULT_FRAMES_PER_SECOND; uint durationRtpTS = VIDEO_SAMPLING_RATE / fps; OnVideoSourceEncodedSample.Invoke(durationRtpTS, encodedBuffer); } if (_forceKeyFrame) { _forceKeyFrame = false; } } } }
public static unsafe SoftwareBitmap getImageAs8bitsBitmap(ref ushort[] data, uint height, uint width, int colorDepth, object[] curve, ref int[] value, bool histo, bool bgr) { SoftwareBitmap image = new SoftwareBitmap(BitmapPixelFormat.Rgba8, (int)width, (int)height, BitmapAlphaMode.Ignore); using (BitmapBuffer buffer = image.LockBuffer(BitmapBufferAccessMode.Write)) { using (var reference = buffer.CreateReference()) { byte *tempByteArray; uint capacity; ((IMemoryBufferByteAccess)reference).GetBuffer(out tempByteArray, out capacity); // Fill-in the BGRA plane BitmapPlaneDescription bufferLayout = buffer.GetPlaneDescription(0); //calculte diff between colordepth and 8 int diff = (colorDepth) - 8; for (int i = 0; i < bufferLayout.Width * bufferLayout.Height; i++) { //get the pixel ushort red = (ushort)(data[(i * 3)] >> diff), green = (ushort)(data[(i * 3) + 1] >> diff), blue = (ushort)(data[(i * 3) + 2] >> (diff)); if (blue > 255) { blue = 255; } if (red > 255) { red = 255; } if (green > 255) { green = 255; } if (histo) { value[(ushort)((red + green + blue) / 3)]++; } if (bgr) { tempByteArray[bufferLayout.StartIndex + (i * 4)] = (byte)blue; tempByteArray[bufferLayout.StartIndex + (i * 4) + 1] = (byte)green; tempByteArray[bufferLayout.StartIndex + (i * 4) + 2] = (byte)red; } else { tempByteArray[bufferLayout.StartIndex + (i * 4)] = (byte)red; tempByteArray[bufferLayout.StartIndex + (i * 4) + 1] = (byte)green; tempByteArray[bufferLayout.StartIndex + (i * 4) + 2] = (byte)blue; } tempByteArray[bufferLayout.StartIndex + (i * 4) + 3] = 255; } } } return(image); }
/// <summary> /// Copies data from an RGB buffer to a software bitmap. /// </summary> /// <param name="rgb24Buffer">The RGB buffer to copy from.</param> /// <param name="sbmp">The software bitmap to copy the data to.</param> private void SetBitmapData(byte[] buffer, SoftwareBitmap sbmp, VideoPixelFormatsEnum pixelFormat) { using (BitmapBuffer bmpBuffer = sbmp.LockBuffer(BitmapBufferAccessMode.Write)) { using (var reference = bmpBuffer.CreateReference()) { unsafe { byte *dataInBytes; uint capacity; ((IMemoryBufferByteAccess)reference).GetBuffer(out dataInBytes, out capacity); int posn = 0; // Fill-in the RGBA plane BitmapPlaneDescription bufferLayout = bmpBuffer.GetPlaneDescription(0); for (int i = 0; i < bufferLayout.Height; i++) { for (int j = 0; j < bufferLayout.Width; j++) { // NOTE: Same as for System.Drawing.Bitmap pixel formats that have "rgb" in their name, such as // BitmapPixelFormat.Rgba8, use a buffer format of BGR. Many issues on StackOverflow regarding this, // e.g. https://stackoverflow.com/questions/5106505/converting-gdi-pixelformat-to-wpf-pixelformat. // Notice the switch of the Blue and Red pixels below. if (pixelFormat == VideoPixelFormatsEnum.Rgb) { dataInBytes[bufferLayout.StartIndex + bufferLayout.Stride * i + 4 * j + 0] = buffer[posn++]; dataInBytes[bufferLayout.StartIndex + bufferLayout.Stride * i + 4 * j + 1] = buffer[posn++]; dataInBytes[bufferLayout.StartIndex + bufferLayout.Stride * i + 4 * j + 2] = buffer[posn++]; dataInBytes[bufferLayout.StartIndex + bufferLayout.Stride * i + 4 * j + 3] = (byte)255; } else if (pixelFormat == VideoPixelFormatsEnum.Bgr) { dataInBytes[bufferLayout.StartIndex + bufferLayout.Stride * i + 4 * j + 2] = buffer[posn++]; dataInBytes[bufferLayout.StartIndex + bufferLayout.Stride * i + 4 * j + 1] = buffer[posn++]; dataInBytes[bufferLayout.StartIndex + bufferLayout.Stride * i + 4 * j + 0] = buffer[posn++]; dataInBytes[bufferLayout.StartIndex + bufferLayout.Stride * i + 4 * j + 3] = (byte)255; } //if (pixelFormat == VideoPixelFormatsEnum.Rgba) //{ // dataInBytes[bufferLayout.StartIndex + bufferLayout.Stride * i + 4 * j + 0] = buffer[posn++]; // dataInBytes[bufferLayout.StartIndex + bufferLayout.Stride * i + 4 * j + 1] = buffer[posn++]; // dataInBytes[bufferLayout.StartIndex + bufferLayout.Stride * i + 4 * j + 2] = buffer[posn++]; // dataInBytes[bufferLayout.StartIndex + bufferLayout.Stride * i + 4 * j + 3] = buffer[posn++]; //} else if (pixelFormat == VideoPixelFormatsEnum.Bgra) { dataInBytes[bufferLayout.StartIndex + bufferLayout.Stride * i + 4 * j + 2] = buffer[posn++]; dataInBytes[bufferLayout.StartIndex + bufferLayout.Stride * i + 4 * j + 1] = buffer[posn++]; dataInBytes[bufferLayout.StartIndex + bufferLayout.Stride * i + 4 * j + 0] = buffer[posn++]; dataInBytes[bufferLayout.StartIndex + bufferLayout.Stride * i + 4 * j + 3] = buffer[posn++]; } } } } } } }
private unsafe SoftwareBitmap Blur_image(SoftwareBitmap softwareBitmape, int blurRatio) { var softwareBitmap = SoftwareBitmap.Convert(softwareBitmape, BitmapPixelFormat.Bgra8, BitmapAlphaMode.Premultiplied); using (BitmapBuffer buffer = softwareBitmap.LockBuffer(BitmapBufferAccessMode.Write)) { using (var reference = buffer.CreateReference()) { byte *dataInBytes; uint capacity; ((IMemoryBufferByteAccess)reference).GetBuffer(out dataInBytes, out capacity); // Fill-in the BGRA plane BitmapPlaneDescription bufferLayout = buffer.GetPlaneDescription(0); for (int i = 0; i < bufferLayout.Height; i++) { for (int j = 0; j < bufferLayout.Width; j++) { float avg1 = 0; float avg2 = 0; float avg3 = 0; float avg4 = 0; float l = 0; for (int k = j - blurRatio; k <= j + blurRatio; k++) { for (int h = i - blurRatio; h <= i + blurRatio; h++) { if (k >= 0 && h >= 0 && k < bufferLayout.Width && h < bufferLayout.Height) { avg1 += dataInBytes[bufferLayout.StartIndex + bufferLayout.Stride * h + 4 * k + 0]; avg2 += dataInBytes[bufferLayout.StartIndex + bufferLayout.Stride * h + 4 * k + 1]; avg3 += dataInBytes[bufferLayout.StartIndex + bufferLayout.Stride * h + 4 * k + 2]; avg4 += dataInBytes[bufferLayout.StartIndex + bufferLayout.Stride * h + 4 * k + 3]; var test = dataInBytes[bufferLayout.StartIndex + bufferLayout.Stride * h + 4 * k + 0]; l++; } } } dataInBytes[bufferLayout.StartIndex + bufferLayout.Stride * i + 4 * j + 0] = (byte)(avg1 / l); dataInBytes[bufferLayout.StartIndex + bufferLayout.Stride * i + 4 * j + 1] = (byte)(avg2 / l); dataInBytes[bufferLayout.StartIndex + bufferLayout.Stride * i + 4 * j + 2] = (byte)(avg3 / l); dataInBytes[bufferLayout.StartIndex + bufferLayout.Stride * i + 4 * j + 3] = (byte)(avg4 / l); } } } } return(softwareBitmap); }
// Graphics / Buffer helper functions public unsafe int[,] GetColorDistribution(SoftwareBitmap haystack, Color needle, int[] tolerance, int sparcity = 2) { int w = haystack.PixelWidth; int h = haystack.PixelHeight; int r = needle.R; int g = needle.G; int b = needle.B; int[,] dA = new int[w * h, 2]; int dAPointer = 0; BitmapBuffer buffer = haystack.LockBuffer(BitmapBufferAccessMode.Read); BitmapPlaneDescription bufferLayout = buffer.GetPlaneDescription(0); IMemoryBufferReference reference = buffer.CreateReference(); ((IMemoryBufferByteAccess)reference).GetBuffer(out byte *bufferData, out uint capacity); int pos; for (int x = 0; x < w; x += sparcity) { for (int y = 0; y < h; y += sparcity) { pos = bufferLayout.StartIndex + bufferLayout.Stride * y + 4 * x; int rC = bufferData[pos + 2]; int gC = bufferData[pos + 1]; int bC = bufferData[pos]; if (Math.Abs(rC - r) <= tolerance[0] && Math.Abs(gC - g) <= tolerance[1] && Math.Abs(bC - b) <= tolerance[2]) { dA[dAPointer, 0] = x; dA[dAPointer, 1] = y; dAPointer++; } } } dAPointer--; reference.Dispose(); buffer.Dispose(); int[,] distributionArray = new int[dAPointer + 1, 2]; for (int p = dAPointer; p >= 0; p--) { distributionArray[p, 0] = dA[p, 0]; distributionArray[p, 1] = dA[p, 1]; } return(distributionArray); }
public static unsafe Mat SoftwareBitmap2Mat(SoftwareBitmap softwareBitmap) { using (BitmapBuffer buffer = softwareBitmap.LockBuffer(BitmapBufferAccessMode.Write)) { using (var reference = buffer.CreateReference()) { ((IMemoryBufferByteAccess)reference).GetBuffer(out var dataInBytes, out var capacity); Mat outputMat = new Mat(softwareBitmap.PixelHeight, softwareBitmap.PixelWidth, MatType.CV_8UC4, (IntPtr)dataInBytes); return(outputMat); } } }
private unsafe SoftwareBitmap Edit_image_Middle(SoftwareBitmap softwareBitmape, int PixelationRatio) { var softwareBitmap = SoftwareBitmap.Convert(softwareBitmape, BitmapPixelFormat.Bgra8, BitmapAlphaMode.Premultiplied); using (BitmapBuffer buffer = softwareBitmap.LockBuffer(BitmapBufferAccessMode.Write)) { using (var reference = buffer.CreateReference()) { byte *dataInBytes; uint capacity; ((IMemoryBufferByteAccess)reference).GetBuffer(out dataInBytes, out capacity); // Fill-in the BGRA plane BitmapPlaneDescription bufferLayout = buffer.GetPlaneDescription(0); for (int i = 0; i < bufferLayout.Height + PixelationRatio; i = i + PixelationRatio * 2) { for (int j = 0; j < bufferLayout.Width + PixelationRatio; j = j + PixelationRatio * 2) { for (int k = j - PixelationRatio; k <= j + PixelationRatio; k++) { for (int h = i - PixelationRatio; h <= i + PixelationRatio; h++) { if (k >= 0 && h >= 0 && k < bufferLayout.Width && h < bufferLayout.Height) { var iIndex = i >= bufferLayout.Height ? bufferLayout.Height - 1 : i; var jIndex = j >= bufferLayout.Width ? bufferLayout.Width - 1 : j; byte value1 = dataInBytes[bufferLayout.StartIndex + bufferLayout.Stride * iIndex + 4 * jIndex + 0]; byte value2 = dataInBytes[bufferLayout.StartIndex + bufferLayout.Stride * iIndex + 4 * jIndex + 1]; byte value3 = dataInBytes[bufferLayout.StartIndex + bufferLayout.Stride * iIndex + 4 * jIndex + 2]; byte value4 = dataInBytes[bufferLayout.StartIndex + bufferLayout.Stride * iIndex + 4 * jIndex + 3]; dataInBytes[bufferLayout.StartIndex + bufferLayout.Stride * h + 4 * k + 0] = value1; dataInBytes[bufferLayout.StartIndex + bufferLayout.Stride * h + 4 * k + 1] = value2; dataInBytes[bufferLayout.StartIndex + bufferLayout.Stride * h + 4 * k + 2] = value3; dataInBytes[bufferLayout.StartIndex + bufferLayout.Stride * h + 4 * k + 3] = value4; } } } } } } } return(softwareBitmap); }
public static unsafe SoftwareBitmap GetImage(Document doc) { var softwareBitmap = new SoftwareBitmap(BitmapPixelFormat.Gray8, 100, 100); using (BitmapBuffer buffer = softwareBitmap.LockBuffer(BitmapBufferAccessMode.Write)) { using (var reference = buffer.CreateReference()) { // Fill-in the Gray plane BitmapPlaneDescription bufferLayout = buffer.GetPlaneDescription(0); //doc.test(reference, 100, 100, bufferLayout.StartIndex, bufferLayout.Stride); } } return(SoftwareBitmap.Convert(softwareBitmap, BitmapPixelFormat.Bgra8, BitmapAlphaMode.Premultiplied)); }
public unsafe void ProcessFrame(ProcessVideoFrameContext context) { frameCount++; Debug.WriteLine("Frame count: " + frameCount); using (BitmapBuffer buffer = context.InputFrame.SoftwareBitmap.LockBuffer(BitmapBufferAccessMode.Read)) using (BitmapBuffer targetBuffer = context.OutputFrame.SoftwareBitmap.LockBuffer(BitmapBufferAccessMode.Write)) { using (var reference = buffer.CreateReference()) using (var targetReference = targetBuffer.CreateReference()) { byte *dataInBytes; uint capacity; ((IMemoryBufferByteAccess)reference).GetBuffer(out dataInBytes, out capacity); byte *targetDataInBytes; uint targetCapacity; ((IMemoryBufferByteAccess)targetReference).GetBuffer(out targetDataInBytes, out targetCapacity); var fadeValue = FadeValue; // Fill-in the BGRA plane BitmapPlaneDescription bufferLayout = buffer.GetPlaneDescription(0); for (int i = 0; i < bufferLayout.Height; i++) { for (int j = 0; j < bufferLayout.Width; j++) { byte value = (byte)((float)j / bufferLayout.Width * 255); int bytesPerPixel = 4; if (encodingProperties.Subtype != "ARGB32") { // If you support other encodings, adjust index into the buffer accordingly } int idx = bufferLayout.StartIndex + bufferLayout.Stride * i + bytesPerPixel * j; targetDataInBytes[idx + 0] = (byte)(fadeValue * (float)dataInBytes[idx + 0]); //targetDataInBytes[idx + 1] = (byte)(fadeValue * (float)dataInBytes[idx + 1]); //targetDataInBytes[idx + 2] = (byte)(fadeValue * (float)dataInBytes[idx + 2]); //targetDataInBytes[idx + 3] = dataInBytes[idx + 3]; } } } } }
public unsafe HistoRaw ApplyTo16Bits(ImageComponent <ushort> image, SoftwareBitmap bitmap, bool histogram) { Debug.Assert(bitmap.BitmapPixelFormat == BitmapPixelFormat.Rgba16); var buffer = Apply(image); //Clip the image //Luminance.Clip(buffer, 16); HistoRaw histo = null; //calculate the new histogram (create a 8 bits histogram) if (histogram) { histo = HistogramHelper.CalculateHistogram(buffer); } //copy the buffer to the image with clipping //calculte the shift between colordepth input and output int shift = image.ColorDepth - 16; using (BitmapBuffer buff = bitmap.LockBuffer(BitmapBufferAccessMode.Write)) using (var reference = buff.CreateReference()) { ((IMemoryBufferByteAccess)reference).GetBuffer(out var temp, out uint capacity); Parallel.For(0, buffer.dim.height, y => { long realY = y * buffer.dim.width; for (int x = 0; x < buffer.dim.width; x++) { long realPix = realY + x; long bufferPix = realPix * 8; temp[bufferPix] = (byte)(buffer.red[realPix] >> 8); temp[bufferPix + 1] = (byte)(buffer.red[realPix]); temp[bufferPix + 2] = (byte)(buffer.green[realPix] >> 8); temp[bufferPix + 3] = (byte)(buffer.green[realPix]); temp[bufferPix + 4] = (byte)(buffer.blue[realPix] >> 8); temp[bufferPix + 5] = (byte)(buffer.blue[realPix]); temp[bufferPix + 6] = 255; //set transparency to 255 else image will be blank temp[bufferPix + 7] = 255; //set transparency to 255 else image will be blank } }); } return(histo); }
private static unsafe Mat SoftwareBitmapToMat(this SoftwareBitmap softwareBitmap) { try { using (BitmapBuffer Buffer = softwareBitmap.LockBuffer(BitmapBufferAccessMode.Write)) using (Windows.Foundation.IMemoryBufferReference Reference = Buffer.CreateReference()) { ((IMemoryBufferByteAccess)Reference).GetBuffer(out byte *dataInBytes, out uint capacity); return(new Mat(softwareBitmap.PixelHeight, softwareBitmap.PixelWidth, MatType.CV_8UC4, (IntPtr)dataInBytes)); } } catch { return(new Mat(softwareBitmap.PixelHeight, softwareBitmap.PixelWidth, MatType.CV_8UC4)); } }
private unsafe bool[,] GetBluePixelArray(SoftwareBitmap softwareBitmap) { int widthPixels; int heightPixels; bool[,] pixelBoolArray; using (BitmapBuffer buffer = softwareBitmap.LockBuffer(BitmapBufferAccessMode.Write)) { using (var reference = buffer.CreateReference()) { byte *dataInBytes; uint capacity; ((IMemoryBufferByteAccess)reference).GetBuffer(out dataInBytes, out capacity); // Fill-in the BGRA plane BitmapPlaneDescription bufferLayout = buffer.GetPlaneDescription(0); widthPixels = bufferLayout.Width; heightPixels = bufferLayout.Height; pixelBoolArray = new bool[heightPixels, widthPixels]; for (int y = 0; y < heightPixels; y++) { for (int x = 0; x < widthPixels; x++) { int pixelIndex = bufferLayout.StartIndex + bufferLayout.Stride * y + 4 * x; if (dataInBytes[pixelIndex + 0] == 255 && dataInBytes[pixelIndex + 1] == 0 && dataInBytes[pixelIndex + 2] == 0) { pixelBoolArray[y, x] = true; } else { pixelBoolArray[y, x] = false; } } } } } return(pixelBoolArray); }
/// <summary> /// Get the size in bytes of the underlying bitmap. /// </summary> /// <param name="bitmap">Bitmap.</param> /// <returns>Size in bytes of the underlying bitmap.</returns> public static unsafe uint GetAllocationByteCount(SoftwareBitmap bitmap) { uint capacity = default(int); if (bitmap != null) { using (BitmapBuffer buffer = bitmap.LockBuffer(BitmapBufferAccessMode.Read)) using (var reference = buffer.CreateReference()) { byte *dataInBytes; ((IMemoryBufferByteAccess)reference).GetBuffer( out dataInBytes, out capacity); } } return(capacity); }
private unsafe void GetHasPixelArray() { SoftwareBitmap softwareBitmap = g_CurrentImageInfo.SoftwareBitmap; int widthPixels; int heightPixels; bool[,] hasPixelArray; using (BitmapBuffer buffer = softwareBitmap.LockBuffer(BitmapBufferAccessMode.Write)) { using (var reference = buffer.CreateReference()) { byte *dataInBytes; uint capacity; ((IMemoryBufferByteAccess)reference).GetBuffer(out dataInBytes, out capacity); // Fill-in the BGRA plane BitmapPlaneDescription bufferLayout = buffer.GetPlaneDescription(0); widthPixels = bufferLayout.Width; heightPixels = bufferLayout.Height; hasPixelArray = new bool[heightPixels, widthPixels]; for (int y = 0; y < heightPixels; y++) { for (int x = 0; x < widthPixels; x++) { int pixelIndex = bufferLayout.StartIndex + bufferLayout.Stride * y + 4 * x; if (dataInBytes[pixelIndex + 3] != 0) { hasPixelArray[y, x] = true; } else { hasPixelArray[y, x] = false; } } } } } g_CurrentImageInfo.HasPixelArray = hasPixelArray; }
private unsafe SoftwareBitmap CropImageByRect(SoftwareBitmap inputBitmap, FaceRectangle rect) { var outputBitmap = new SoftwareBitmap(inputBitmap.BitmapPixelFormat, rect.Width, rect.Height, inputBitmap.BitmapAlphaMode); using (BitmapBuffer inputBuffer = inputBitmap.LockBuffer(BitmapBufferAccessMode.Read)) { using (BitmapBuffer outputBuffer = outputBitmap.LockBuffer(BitmapBufferAccessMode.Write)) { using (var inputReference = inputBuffer.CreateReference()) { byte *dataInBytes; uint dataInCapacity; ((IMemoryBufferByteAccess)inputReference).GetBuffer(out dataInBytes, out dataInCapacity); BitmapPlaneDescription inputBufferLayout = inputBuffer.GetPlaneDescription(0); using (var outputReference = outputBuffer.CreateReference()) { byte *dataOutBytes; uint dataOutCapacity; ((IMemoryBufferByteAccess)outputReference).GetBuffer(out dataOutBytes, out dataOutCapacity); // Fill-in the BGRA plane BitmapPlaneDescription outputBufferLayout = outputBuffer.GetPlaneDescription(0); for (int i = 0; i < outputBufferLayout.Height; i++) { for (int j = 0; j < outputBufferLayout.Width; j++) { dataOutBytes[outputBufferLayout.StartIndex + outputBufferLayout.Stride * i + 4 * j + 0] = dataInBytes[inputBufferLayout.StartIndex + inputBufferLayout.Stride * (rect.Top + i) + 4 * (rect.Left + j) + 0]; dataOutBytes[outputBufferLayout.StartIndex + outputBufferLayout.Stride * i + 4 * j + 1] = dataInBytes[inputBufferLayout.StartIndex + inputBufferLayout.Stride * (rect.Top + i) + 4 * (rect.Left + j) + 1]; dataOutBytes[outputBufferLayout.StartIndex + outputBufferLayout.Stride * i + 4 * j + 2] = dataInBytes[inputBufferLayout.StartIndex + inputBufferLayout.Stride * (rect.Top + i) + 4 * (rect.Left + j) + 2]; dataOutBytes[outputBufferLayout.StartIndex + outputBufferLayout.Stride * i + 4 * j + 3] = dataInBytes[inputBufferLayout.StartIndex + inputBufferLayout.Stride * (rect.Top + i) + 4 * (rect.Left + j) + 3]; } } } } } } return(outputBitmap); }
public unsafe void SetPixelColor(SoftwareBitmap swBmp, int x, int y, Color color) { BitmapBuffer buffer = swBmp.LockBuffer(BitmapBufferAccessMode.Write); BitmapPlaneDescription bufferLayout = buffer.GetPlaneDescription(0); if (x >= 0 && x < bufferLayout.Width && y >= 0 && y < bufferLayout.Height) { using (IMemoryBufferReference reference = buffer.CreateReference()) { ((IMemoryBufferByteAccess)reference).GetBuffer(out byte *bufferData, out uint capacity); bufferData[bufferLayout.StartIndex + bufferLayout.Stride * y + 4 * x + 0] = color.B; bufferData[bufferLayout.StartIndex + bufferLayout.Stride * y + 4 * x + 1] = color.G; bufferData[bufferLayout.StartIndex + bufferLayout.Stride * y + 4 * x + 2] = color.R; bufferData[bufferLayout.StartIndex + bufferLayout.Stride * y + 4 * x + 3] = color.A; } } buffer.Dispose(); }
public unsafe HistoRaw ApplyTo8Bits(ImageComponent <ushort> image, SoftwareBitmap bitmap, bool histogram) { Debug.Assert(image.red != null); Debug.Assert(image.blue != null); Debug.Assert(image.green != null); Debug.Assert(image.dim.Area >= 4); Debug.Assert(bitmap != null); Debug.Assert(image.dim.Area == bitmap.PixelHeight * bitmap.PixelWidth); var buffer = Apply(image); //Clip the image Luminance.Clip(buffer, 8); //calculate the new histogram (create a 8 bits histogram) HistoRaw histo = null; if (histogram) { histo = HistogramHelper.CalculateHistogram(buffer); } //copy the buffer to the image with clipping using (BitmapBuffer buff = bitmap.LockBuffer(BitmapBufferAccessMode.Write)) using (var reference = buff.CreateReference()) { ((IMemoryBufferByteAccess)reference).GetBuffer(out var temp, out uint capacity); Parallel.For(0, buffer.dim.height, y => { long realY = y * buffer.dim.width; for (int x = 0; x < buffer.dim.width; x++) { long realPix = realY + x; long bufferPix = realPix * 4; temp[bufferPix] = (byte)(buffer.blue[realPix]); temp[bufferPix + 1] = (byte)(buffer.green[realPix]); temp[bufferPix + 2] = (byte)(buffer.red[realPix]); temp[bufferPix + 3] = 255; //set transparency to 255 else image will be blank } }); } return(histo); }
protected override RawImage decodeRawInternal() { mRaw.colorDepth = 8; mRaw.cpp = 3; mRaw.bpp = 8; var decoder = BitmapDecoder.CreateAsync(BitmapDecoder.JpegDecoderId, mFile.BaseStream.AsRandomAccessStream()).AsTask(); decoder.Wait(); var bitmapasync = decoder.Result.GetSoftwareBitmapAsync().AsTask(); bitmapasync.Wait(); var image = bitmapasync.Result; using (BitmapBuffer buffer = image.LockBuffer(BitmapBufferAccessMode.Write)) { using (var reference = buffer.CreateReference()) { BitmapPlaneDescription bufferLayout = buffer.GetPlaneDescription(0); mRaw.dim = new iPoint2D(bufferLayout.Width, bufferLayout.Height); mRaw.Init(); unsafe { ((IMemoryBufferByteAccess)reference).GetBuffer(out var temp, out uint capacity); for (int y = 0; y < mRaw.dim.y; y++) { int realY = y * mRaw.dim.x * 3; int bufferY = y * mRaw.dim.x * 4 + +bufferLayout.StartIndex; for (int x = 0; x < mRaw.dim.x; x++) { int realPix = realY + (3 * x); int bufferPix = bufferY + (4 * x); mRaw.rawData[realPix] = temp[bufferPix + 2]; mRaw.rawData[realPix + 1] = temp[bufferPix + 1]; mRaw.rawData[realPix + 2] = temp[bufferPix]; } } } } } return(mRaw); }
public unsafe IResult Detect(SoftwareBitmap image) { CounterResult result = new CounterResult() { Count = 0, Image = new SoftwareBitmap(image.BitmapPixelFormat, image.PixelWidth, image.PixelHeight) }; image.CopyTo(result.Image); using (BitmapBuffer buffer = result.Image.LockBuffer(BitmapBufferAccessMode.Write)) { using (var reference = buffer.CreateReference()) { ((IMemoryBufferByteAccess)reference).GetBuffer(out byte *dataInBytes, out uint capacity); // Fill-in the BGRA plane BitmapPlaneDescription bufferLayout = buffer.GetPlaneDescription(0); for (int i = 0; i < bufferLayout.Height; i++) { for (int j = 0; j < bufferLayout.Width; j++) { int b = dataInBytes[bufferLayout.StartIndex + bufferLayout.Stride * i + 4 * j + 0] - Color.B; int g = dataInBytes[bufferLayout.StartIndex + bufferLayout.Stride * i + 4 * j + 1] - Color.G; int r = dataInBytes[bufferLayout.StartIndex + bufferLayout.Stride * i + 4 * j + 2] - Color.R; if (b * b + g * g + r * r < Delta * Delta) { result.Count++; dataInBytes[bufferLayout.StartIndex + bufferLayout.Stride * i + 4 * j + 0] = 0; dataInBytes[bufferLayout.StartIndex + bufferLayout.Stride * i + 4 * j + 1] = 0; dataInBytes[bufferLayout.StartIndex + bufferLayout.Stride * i + 4 * j + 2] = 255; dataInBytes[bufferLayout.StartIndex + bufferLayout.Stride * i + 4 * j + 3] = (byte)255; } } } } } return(result); }