【问题标题】:How to detect average pixel intensity from live camera preview?如何从实时相机预览中检测平均像素强度?
【发布时间】:2016-03-04 04:49:42
【问题描述】:

我正在构建一个扫描仪应用程序,并试图从相机的预览回调中确定“预览质量”。我想自定义相机的AUTO_FLASH_MODE,如果环境太暗,它会打开。

如何检测是否存在较高的平均暗像素?这意味着(在预览中)我变得黑暗,因此需要打开相机的闪光灯。

【问题讨论】:

  • 其实我想自定义相机的 AUTO_FLASH_MODE 并且我发现环境中有任何黑暗(以像素为单位)然后打开肉体直到我的扫描仪打开。
  • 我删除了 Flash 标签(使用前请阅读该标签的说明)。无论如何,所有像素的强度值都在 0 到 255 之间……所以 128 介于全亮 (255) 和暗 (0) 之间。确定哪个数字对您来说意味着“太暗”,然后在代码中使用 for 循环检查预览图像中的像素值。还要查找直方图,因为这可能比检查每个像素更有用且更快。
  • @VC.One 如何比检查每个像素更快地查找直方图。您认为如何计算直方图?
  • @VC.One 和 @Pgilet 我是 android 新手,我没有更多的想法,我使用了这个 [stackoverflow.com/questions/25721410/…
  • @piglet 我听到了。我不是 Android 开发人员(他标记为 Flash),但通常图形编程系统可以访问直方图。例如,当您看到图像时,它已经知道其直方图,那么当您可以提取已知的图像时,为什么还要重新开始循环。无论如何,Android SDK 很苛刻,没有 camera.getHistogram() 这样的功能,所以 for-loop 毕竟是解决方案..

标签: android image-processing mobile video-processing


【解决方案1】:

要么了解如何访问图像的像素值并自己计算平均强度,要么使用任何图像处理库来这样做。

暗像素具有低值,亮像素具有高值。 您想要计算所有红色、绿色和蓝色值的平均值除以像素数的三倍。 定义何时打开闪光灯的阈值,但请记住,您必须获得新的曝光时间。 优先使用闪光而不是增加曝光时间,因为较长的曝光时间会产生较高的图像噪点。

【讨论】:

    【解决方案2】:

    我尝试了这种方法,但我认为处理位图并获得平均屏幕颜色会花费不必要的时间,

        @Override
        public void onPreviewFrame(byte[] data, Camera camera) {
            Size cameraResolution = resolution;
            PreviewCallback callback = this.callback;
            if (cameraResolution != null && callback != null)
            {
                int format = camera.getParameters().getPreviewFormat();
                SourceData source = new SourceData(data, cameraResolution.width, cameraResolution.height, format, getCameraRotation());
                callback.onPreview(source);
                final int[] rgb = decodeYUV420SP(data, cameraResolution.width, cameraResolution.height);
    
                //Bitmap bmp =  decodeBitmap(source.getData());
                Bitmap bmp = Bitmap.createBitmap(rgb, cameraResolution.width, cameraResolution.height, Bitmap.Config.ARGB_8888);
                if (bmp != null)
                {
                    //bmp = decodeBitmap(source.getData());
                    ByteArrayOutputStream bytes = new ByteArrayOutputStream();
                    // bmp.compress(Bitmap.CompressFormat.JPEG, 70, bytes);
                    Bitmap resizebitmap = Bitmap.createBitmap(bmp,
                            bmp.getWidth() / 2, bmp.getHeight() / 2, 60, 60);
    
                    int color = getAverageColor(resizebitmap);
                    Log.i("Color Int", color + "");
                    // int color = resizebitmap.getPixel(resizebitmap.getWidth()/2,resizebitmap.getHeight()/2);
    
                    String strColor = String.format("#%06X", 0xFFFFFF & color);
                    //String colorname = sColorNameMap.get(strColor);
    
                    Log.d("strColor", strColor);
                    Log.i("strColor", color + "");
                    if(!mIsOn)
                    {
                        if (color == -16777216 || color < -16777216)//minimum color code (full dark)
                        {
    
                            mIsOn = true;
                            setTorch(true);
                            Log.d("Yahooooo", "" + color);
                        }
                    }
    
                    Log.i("Pixel Value",
                            "Top Left pixel: " + Integer.toHexString(color));
                }
            }
            else
            {
                Log.d(TAG, "Got preview callback, but no handler or resolution available");
            }
        }
    }
        private int[] decodeYUV420SP(byte[] yuv420sp, int width, int height)
       {
          final int frameSize = width * height;
    
        int rgb[]=new int[width*height];
        for (int j = 0, yp = 0; j < height; j++) {
            int uvp = frameSize + (j >> 1) * width, u = 0, v = 0;
            for (int i = 0; i < width; i++, yp++) {
                int y = (0xff & ((int) yuv420sp[yp])) - 16;
                if (y < 0) y = 0;
                if ((i & 1) == 0) {
                    v = (0xff & yuv420sp[uvp++]) - 128;
                    u = (0xff & yuv420sp[uvp++]) - 128;
                }
    
                int y1192 = 1192 * y;
                int r = (y1192 + 1634 * v);
                int g = (y1192 - 833 * v - 400 * u);
                int b = (y1192 + 2066 * u);
    
                if (r < 0) r = 0; else if (r > 262143) r = 262143;
                if (g < 0) g = 0; else if (g > 262143) g = 262143;
                if (b < 0) b = 0; else if (b > 262143) b = 262143;
    
                rgb[yp] = 0xff000000 | ((r << 6) & 0xff0000) | ((g >> 2) &
                        0xff00) | ((b >> 10) & 0xff);
    
    
            }
        }
        return rgb;
    }
    
        private int getAverageColor(Bitmap bitmap)
        {
        int redBucket = 0;
        int greenBucket = 0;
        int blueBucket = 0;
        int pixelCount = 0;
    
        for (int y = 0; y < bitmap.getHeight(); y++) {
            for (int x = 0; x < bitmap.getWidth(); x++) {
                int c = bitmap.getPixel(x, y);
    
                pixelCount++;
                redBucket += Color.red(c);
                greenBucket += Color.green(c);
                blueBucket += Color.blue(c);
                // does alpha matter?
            }
        }
    
        int averageColor = Color.rgb(redBucket / pixelCount, greenBucket
                / pixelCount, blueBucket / pixelCount);
        return averageColor;
    }
    

    【讨论】:

    • 我不是安卓专家。我确信有一种方法可以获取预览图像的字节数据。无论如何,这不是你的字节数组“数据”吗?您将其用作位图解码的输入...。如果不是,我建议您在这里创建一个新的简单问题:How to get pixel values from Android camera, or google for it...我猜您的标题有点含糊.
    猜你喜欢
    • 2023-03-18
    • 2015-08-11
    • 1970-01-01
    • 1970-01-01
    • 2012-07-04
    • 1970-01-01
    • 1970-01-01
    • 2013-02-02
    • 2013-07-29
    相关资源
    最近更新 更多