/// <summary> /// Sets a value of all cells in a buffer /// </summary> /// <param name="queue">cloo command queue</param> /// <param name="source">image source</param> /// <param name="value">value to set</param> public void SetValue(ClooCommandQueue queue, ClooBuffer<uint> source, uint value) { if (queue == null) throw new ArgumentNullException("queue"); if (source == null) throw new ArgumentNullException("source"); KernelCoreUIntSetValue.SetArguments(source, value); queue.Execute(KernelCoreUIntSetValue, null, new long[] { source.HostBuffer.Length }, null, null); source.Modified = true; }
void initializeDevice() { try { _selectedDevice = comboBoxDevices.SelectedItem as ClooDevice; if (_context != null) { _startProcessing = false; // dispose previous context _context.Dispose(); _kernels = null; _context = null; _sampler = null; _queue = null; image2.Source = null; image3.Source = null; image4.Source = null; } if (_selectedDevice != null) { // create context _context = _selectedDevice.CreateContext(); _queue = _context.CreateCommandQueue(); _sampler = new ClooSampler(_context, false, ComputeImageAddressing.ClampToEdge, ComputeImageFiltering.Linear); _kernels = ClooProgramViolaJones.Create(_context); _haarObjectDetector = ClooHaarObjectDetector.CreateFaceDetector(_context, _queue, 640, 480); _haarObjectDetector.ScalingFactor = 1.25f; _haarObjectDetector.ScalingMode = ScalingMode.SmallerToLarger; _haarObjectDetector.MinSize = new System.Drawing.Size(30, 30); _haarObjectDetector.MaxSize = new System.Drawing.Size(100, 100); Stopwatch stopwatch = new Stopwatch(); stopwatch.Reset(); stopwatch.Start(); _histogram = new ClooBuffer<uint>(_context, ComputeMemoryFlags.ReadWrite | ComputeMemoryFlags.AllocateHostPointer, 32 * 32 * 32); _histogram2 = new ClooBuffer<uint>(_context, ComputeMemoryFlags.ReadWrite | ComputeMemoryFlags.AllocateHostPointer, 32 * 32 * 32); _clooImageByteOriginal = ClooImage2DByteRgbA.CreateFromBitmap(_context, ComputeMemoryFlags.ReadWrite | ComputeMemoryFlags.UseHostPointer, _bitmapImage1); _clooImageByteOriginal.WriteToDevice(_queue); _clooImageByteGrayOriginal = ClooImage2DByteA.CreateFromBitmap(_context, ComputeMemoryFlags.ReadWrite | ComputeMemoryFlags.UseHostPointer, _bitmapImage1); _clooImageByteGrayOriginal.WriteToDevice(_queue); _clooImageByteResult = ClooImage2DByteRgbA.Create(_context, ComputeMemoryFlags.ReadWrite | ComputeMemoryFlags.UseHostPointer, _bitmapImage1.Width, _bitmapImage1.Height); _clooImageByteResultA = ClooImage2DByteA.Create(_context, ComputeMemoryFlags.ReadWrite | ComputeMemoryFlags.UseHostPointer, _bitmapImage1.Width, _bitmapImage1.Height); _clooImageFloatOriginal = ClooImage2DFloatRgbA.Create(_context, ComputeMemoryFlags.ReadWrite | ComputeMemoryFlags.UseHostPointer, _bitmapImage1.Width, _bitmapImage1.Height); _clooImageFloatGrayOriginal = ClooImage2DFloatA.Create(_context, ComputeMemoryFlags.ReadWrite | ComputeMemoryFlags.UseHostPointer, _bitmapImage1.Width, _bitmapImage1.Height); _clooImageFloatTemp1 = ClooImage2DFloatRgbA.Create(_context, ComputeMemoryFlags.ReadWrite | ComputeMemoryFlags.UseHostPointer, _bitmapImage1.Width, _bitmapImage1.Height); _clooImageFloatTemp2 = ClooImage2DFloatRgbA.Create(_context, ComputeMemoryFlags.ReadWrite | ComputeMemoryFlags.UseHostPointer, _bitmapImage1.Width, _bitmapImage1.Height); _clooImageFloatATemp1 = ClooImage2DFloatA.Create(_context, ComputeMemoryFlags.ReadWrite | ComputeMemoryFlags.UseHostPointer, _bitmapImage1.Width, _bitmapImage1.Height); _clooImageFloatATemp2 = ClooImage2DFloatA.Create(_context, ComputeMemoryFlags.ReadWrite | ComputeMemoryFlags.UseHostPointer, _bitmapImage1.Width, _bitmapImage1.Height); _clooImageFloatIntegral = ClooImage2DFloatA.Create(_context, ComputeMemoryFlags.ReadWrite | ComputeMemoryFlags.UseHostPointer, _bitmapImage1.Width + 1, _bitmapImage1.Height + 1); _clooImageUIntIntegral = ClooImage2DUIntA.Create(_context, ComputeMemoryFlags.ReadWrite | ComputeMemoryFlags.UseHostPointer, _bitmapImage1.Width + 1, _bitmapImage1.Height + 1); _clooImageUIntIntegralSquare = ClooImage2DUIntA.Create(_context, ComputeMemoryFlags.ReadWrite | ComputeMemoryFlags.UseHostPointer, _bitmapImage1.Width + 1, _bitmapImage1.Height + 1); _queue.Finish(); label1.Content = stopwatch.ElapsedMilliseconds + " ms - original " + _bitmapImage1.Width + "x" + _bitmapImage1.Height; _startProcessing = true; Update(); Update(); } } catch (Exception ex) { // show exception MessageBox.Show(ex.Message, ex.GetType().ToString(), MessageBoxButton.OK, MessageBoxImage.Stop); } }
public void ClooExecuteHistogramBackprojection() { var gpuHist = new ClooBuffer<uint>(Context, Cloo.ComputeMemoryFlags.ReadWrite | ComputeMemoryFlags.AllocateHostPointer, 64); var srcImage = CreateClooImageByteRgbA(100, 100); var dstImage = CreateClooImageByteA(100, 100); srcImage.HostBuffer.Clear(); srcImage.WriteToDevice(Queue); ClooImaging.HistogramN(Queue, srcImage, gpuHist, 4); gpuHist.ReadFromDevice(Queue); ClooImaging.HistogramBackprojection(Queue, srcImage, dstImage, gpuHist, gpuHist, 4); dstImage.ReadFromDevice(Queue); // check each value for (int i = 0; i < dstImage.HostBuffer.Length; i++) Assert.AreEqual((byte)255, dstImage.HostBuffer[i], "Destination image mismatched probability at index " + i); // TODO: add more tests here }
public void ClooExecuteHistogramN() { var gpuHist = new ClooBuffer<uint>(Context, Cloo.ComputeMemoryFlags.ReadWrite | ComputeMemoryFlags.AllocateHostPointer, 64); var cpuHist = new CpuBuffer<uint>(64); var gpuImage = CreateClooImageByteRgbA(100, 100); ClooImaging.HistogramN(Queue, gpuImage, gpuHist, 4); gpuHist.ReadFromDevice(Queue); // compare GPU histogram with CPU version CpuImaging.HistogramN(gpuImage, cpuHist, 4); Assert.AreNotEqual(0, cpuHist.HostBuffer.Sum(x => x)); Assert.AreEqual(cpuHist.HostBuffer.Sum(x => x), gpuHist.HostBuffer.Sum(x => x)); // check each value for (int i = 0; i < cpuHist.HostBuffer.Length; i++) Assert.AreEqual(cpuHist.HostBuffer[i], gpuHist.HostBuffer[i], "Histogram comparison failed at index " + i); }
public void ProcessViolaJonesFrame(ClooCommandQueue queue, ClooBuffer<HaarFeatureNode> nodes, int stagesCount, IBuffer<int> stageNodeCounts, IBuffer<float> stageThresholds, ClooImage2DUIntA integral, ClooImage2DUIntA integralSquare, IBuffer<Rectangle> resRectangles, float scale, int countX, int countY, int stepX, int stepY, int windowWidth, int windowHeight) { if (queue == null) throw new ArgumentNullException("queue"); if (nodes == null) throw new ArgumentNullException("nodes"); if (integral == null) throw new ArgumentNullException("integral"); if (integralSquare == null) throw new ArgumentNullException("integralSquare"); if (resRectangles == null) throw new ArgumentNullException("resRectangles"); KernelImageViolaJonesProcess.SetArguments(nodes, stagesCount, stageNodeCounts, stageThresholds, integral, integralSquare, resRectangles, resRectangles.HostBuffer.Length, scale, stepX, stepY, windowWidth, windowHeight); queue.Execute(KernelImageViolaJonesProcess, null, new long[] { countX, countY }, null, null); }
/// <summary> /// Creates a ClooBuffer and fills it with random values /// </summary> /// <param name="length">buffer length</param> /// <returns></returns> public virtual ClooBuffer<HaarFeatureNode> CreateClooBufferHaarFeatureNode(int length = 9999) { ResetRandomSeed(); var buffer = new ClooBuffer<HaarFeatureNode>(Context, ComputeMemoryFlags.ReadWrite | ComputeMemoryFlags.UseHostPointer, length); Assert.AreNotEqual(0, buffer.HostBuffer.Length); Assert.AreEqual(buffer.HostBuffer.Length * Marshal.SizeOf(typeof(HaarFeatureNode)), buffer.Size); buffer.WriteToDevice(Queue); return buffer; }
public void ClearViolaJonesRectangles(ClooCommandQueue queue, ClooBuffer<Rectangle> resRectangles) { if (queue == null) throw new ArgumentNullException("queue"); if (resRectangles == null) throw new ArgumentNullException("resRectangles"); KernelImageViolaJonesClear.SetArguments(resRectangles); queue.Execute(KernelImageViolaJonesClear, null, new long[] { resRectangles.HostBuffer.Length }, null, null); }
/// <summary> /// Creates a ClooBuffer and fills it with random values /// </summary> /// <param name="length">buffer length</param> /// <returns></returns> public virtual ClooBuffer<uint> CreateClooBufferUInt(int length = 9999) { ResetRandomSeed(); var buffer = new ClooBuffer<uint>(Context, ComputeMemoryFlags.ReadWrite | ComputeMemoryFlags.AllocateHostPointer, length); Assert.AreNotEqual(0, buffer.HostBuffer.Length); Assert.AreEqual(4 * buffer.HostBuffer.Length, buffer.Size); for (int i = 0; i < length; i++) buffer.HostBuffer[i] = (uint)Random.Next(256); buffer.WriteToDevice(Queue); Assert.AreNotEqual(0, buffer.HostBuffer.Sum(x => x)); return buffer; }
/// <summary> /// Create color histogram with N bins /// </summary> /// <param name="queue">cloo command queue</param> /// <param name="source">image source</param> /// <param name="histogram">uint buffer (must be at least bins^3 in length)</param> /// <param name="bins">number of bins</param> /// <param name="startX">start from X coordinate</param> /// <param name="startY">start from Y coordinate</param> /// <param name="width">width</param> /// <param name="height">height</param> public void HistogramN(ClooCommandQueue queue, ClooImage2DByteRgbA source, ClooBuffer<uint> histogram, byte bins, int startX = 0, int startY = 0, int width = 0, int height = 0) { if (queue == null) throw new ArgumentNullException("queue"); if (source == null) throw new ArgumentNullException("source"); if (histogram == null) throw new ArgumentNullException("histogram"); if (bins < 2) throw new ArgumentException("bins must be at least 2", "bins"); int length = bins * bins * bins; if (histogram.Count < length) throw new ArgumentException("Buffer length for histogram must be at least " + length, "histogram"); if (width == 0) width = source.Width - startX; if (height == 0) height = source.Height - startY; if (width < 0) throw new ArgumentOutOfRangeException("width"); if (height < 0) throw new ArgumentOutOfRangeException("height"); KernelCoreUIntSetValue.SetArguments(histogram, (uint)0); queue.Execute(KernelCoreUIntSetValue, null, new long[] { 256 }, null, null); KernelImageByteRgbHistogramN.SetArguments(source, histogram, bins, (uint)startX, (uint)startY); queue.Execute(KernelImageByteRgbHistogramN, null, new long[] { width, height }, null, null); histogram.Modified = true; }
/// <summary> /// Create histogram backprojection for RGB float image /// </summary> /// <param name="queue">cloo command queue</param> /// <param name="source">image source</param> /// <param name="dest">image destination (for propability map)</param> /// <param name="srcHistogram">source histogram uint buffer (must be at least bins^3 in length)</param> /// <param name="frameHistogram">frame histogram uint buffer (must be at least bins^3 in length)</param> /// <param name="bins">number of bins</param> /// <param name="startX">start from X coordinate</param> /// <param name="startY">start from Y coordinate</param> /// <param name="width">width</param> /// <param name="height">height</param> public void HistogramBackprojection(ClooCommandQueue queue, ClooImage2DByteRgbA source, ClooImage2DFloatRgbA dest, ClooBuffer<uint> srcHistogram, ClooBuffer<uint> frameHistogram, byte bins, int startX = 0, int startY = 0, int width = 0, int height = 0) { if (queue == null) throw new ArgumentNullException("queue"); if (source == null) throw new ArgumentNullException("source"); if (dest == null) throw new ArgumentNullException("dest"); if (srcHistogram == null) throw new ArgumentNullException("srcHistogram"); if (frameHistogram == null) throw new ArgumentNullException("frameHistogram"); if (bins < 2) throw new ArgumentException("bins must be at least 2", "bins"); int length = bins * bins * bins; if (srcHistogram.Count < length) throw new ArgumentException("Buffer length for histogram must be at least " + length, "srcHistogram"); if (frameHistogram.Count < length) throw new ArgumentException("Buffer length for histogram must be at least " + length, "frameHistogram"); if (width == 0) width = source.Width - startX; if (height == 0) height = source.Height - startY; if (width < 0) throw new ArgumentOutOfRangeException("width"); if (height < 0) throw new ArgumentOutOfRangeException("height"); KernelImageByteRgbHistogramFloatBP.SetArguments(source, dest, srcHistogram, frameHistogram, bins, (uint)startX, (uint)startY); queue.Execute(KernelImageByteRgbHistogramFloatBP, null, new long[] { width, height }, null, null); dest.Modified = true; dest.Normalized = true; }
/// <summary> /// Create histogram /// </summary> /// <param name="queue">cloo command queue</param> /// <param name="source">image source</param> /// <param name="histogram">byte buffer (256 bytes)</param> /// <param name="startX">start from X coordinate</param> /// <param name="startY">start from Y coordinate</param> /// <param name="width">width</param> /// <param name="height">height</param> public void Histogram256(ClooCommandQueue queue, ClooImage2DByteA source, ClooBuffer<uint> histogram, int startX = 0, int startY = 0, int width = 0, int height = 0) { if (queue == null) throw new ArgumentNullException("queue"); if (source == null) throw new ArgumentNullException("source"); if (histogram == null) throw new ArgumentNullException("histogram"); if (histogram.Size < 256) throw new ArgumentException("Buffer size for histogram must be at least 256 bytes", "histogram"); if (width == 0) width = source.Width - startX; if (height == 0) height = source.Height - startY; if (width < 0) throw new ArgumentOutOfRangeException("width"); if (height < 0) throw new ArgumentOutOfRangeException("height"); KernelCoreUIntSetValue.SetArguments(histogram, (uint)0); queue.Execute(KernelCoreUIntSetValue, null, new long[] { 256 }, null, null); KernelImageByteHistogram256.SetArguments(source, histogram, (uint)startX, (uint)startY); queue.Execute(KernelImageByteHistogram256, null, new long[] { width, height }, null, null); histogram.Modified = true; }