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); } }
/// <summary> /// Sets a constant value to all cells in an image /// </summary> /// <param name="queue">cloo command queue</param> /// <param name="image">image</param> /// <param name="value">value to set</param> public void SetValue(ClooCommandQueue queue, ClooImage2DUIntA image, uint value) { if (queue == null) throw new ArgumentNullException("queue"); if (image == null) throw new ArgumentNullException("dest"); KernelImageUIntSetValueA.SetArguments(image, value); queue.Execute(KernelImageUIntSetValueA, null, new long[] { image.Width, image.Height }, null, null); image.Modified = true; }
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> /// Convert an float image to a squared integral image /// </summary> /// <remarks> /// We skip the last line to keep the original size (better performance) /// </remarks> /// <param name="queue">cloo command queue</param> /// <param name="source">image source</param> /// <param name="dest">image destination</param> public void IntegralSquare(ClooCommandQueue queue, ClooImage2DByteA source, ClooImage2DUIntA dest) { if (queue == null) throw new ArgumentNullException("queue"); if (source == null) throw new ArgumentNullException("source"); if (dest == null) throw new ArgumentNullException("dest"); if ((source.Width > dest.Width) || (source.Height > dest.Height)) throw new ArgumentException("Destination image (" + dest.Width + "x" + dest.Height + ") must have at least the source image width and height but is only " + source.Width + "x" + source.Height); // execute step 1 SetValue(queue, dest, 0); KernelImageUIntIntegralSquareStep1.SetArguments(source, dest); queue.Execute(KernelImageUIntIntegralSquareStep1, null, new long[] { source.Height - 1 }, null, null); dest.Modified = true; // execute step 2 KernelImageUIntIntegralSquare.SetArguments(dest, dest, source.Height); queue.Execute(KernelImageUIntIntegralSquare, null, new long[] { source.Width - 1 }, null, null); dest.Modified = true; }