/// <summary> /// Calculates an average pixel value in HSV space for the given image format. /// </summary> /// <param name="image"> Image.</param> /// <param name="pixelGetter">Pixel getter.</param> void GetAveragePixelIntensity(Vuforia.Image image, GetPixelFromBuffer pixelGetter) { float avgH, avgS, avgV; avgH = avgS = avgV = 0; int bytesPerPixel = image.Stride / image.Width; int numPixels = image.Width * image.Height; for (int r = 0; r < NumSampleRows; ++r) { int y = Random.Range(0, image.Height); for (int c = 0; c < NumSamplesPerRow; ++c) { int x = Random.Range(0, image.Width); int i = y * image.Stride + x * bytesPerPixel; Color pixel = pixelGetter(image.Pixels, i); float h, s, v; Color.RGBToHSV(pixel, out h, out s, out v); avgH += h / numPixels; avgS += s / numPixels; avgV += v / numPixels; } } lastIntensity = avgV; lastColor = Color.HSVToRGB(avgH, avgS, avgV); }
/// <summary> /// attempts to setup the right image format, retrieve images from Vuforia, and pass them on /// for processing. /// </summary> protected override void Loop() { if (ImageFormat != lastFormat) { while (ImageFormat != null && !CameraDevice.Instance.SetFrameFormat(ImageFormat.Value, true)) { attemptFormats.Dequeue(); } lastFormat = ImageFormat; pixelGetter = null; if (ImageFormat != null && PixelConverters.ContainsKey(ImageFormat.Value)) { pixelGetter = PixelConverters[ImageFormat.Value]; } } if (ImageFormat != null) { try { var image = CameraDevice.Instance.GetCameraImage(ImageFormat.Value); if (image != null) { imageAnalyzer(image, pixelGetter); } } catch (Exception exp) { ScreenDebugger.Print(exp, "VuforiaImageAnalyzer"); } } }