简体   繁体   English

在相机预览中计算色彩

[英]Calculate Color in Camera Preview

Am working on a project that takes Images or Preview Images from the Camera then I must calculate the average count of Blue in the image, I already took the image from the camera rapidly ( As Preview Images ). 我正在一个项目中从Camera拍摄ImagesPreview Images ,然后我必须计算图像中Blue的平均数量,我已经从相机中快速拍摄了图像( As Preview Images )。 the image format is NV21 (really am not expert in image formats, so I don't why they use it) 图像格式为NV21 (真的不是图像格式专家,所以我不为什么他们使用它)

and this is how I read images from the Camera Preview Callback 这就是我从Camera Preview Callback读取图像的方式

public void onPreviewFrame(byte[] data, Camera camera) {

        Camera.Parameters parameters = camera.getParameters();
        Size size = parameters.getPreviewSize();
        YuvImage image = new YuvImage(data, ImageFormat.NV21, size.width,
                size.height, null);
        Rect rectangle = new Rect();
        rectangle.bottom = size.height;
        rectangle.top = 0;
        rectangle.left = 0;
        rectangle.right = size.width;
        ByteArrayOutputStream out2 = new ByteArrayOutputStream();
        image.compressToJpeg(rectangle, 100, out2);

        byte[] d = out2.toByteArray();
        Bitmap bitmap = BitmapFactory.decodeByteArray(d, 0, d.length);
    }

and as you see I have Bitmap now, and I was planning to read determine blue color in image, by reading the Bitmap pixels and then process Pixel by Pixel , but co-workers says that way is heavy and slow in some case and not accurate , and I must get the color blue in the image as matrix from hardware it self, and am not sure where to start with it (btw my co-workers works on iOS version), any help will be appreciated. 如您所见,我现在有Bitmap ,并且我打算读取Bitmap像素,然后Pixel by Pixel处理,从而确定图像中的蓝色,但是同事们说这种方法在某些情况下笨拙且缓慢,而且不准确 ,并且我必须从硬件本身获取图像中的蓝色作为矩阵,并且不确定从哪里开始(顺便说一下,我的同事在iOS版本上工作),我们将不胜感激。

If you are using opencv for android you can have a simple native call (JNICALL) passing the bitmap to the function as jbyte* , then in the function convert it to BGR using Mat myuv ,mbgra; 如果您在Android上使用opencv,则可以进行一个简单的本地调用(JNICALL),将位图作为jbyte *传递给函数,然后在函数中使用Mat myuv,mbgra将其转换为BGR; cvtColor(myuv, mbgra, CV_YUV420sp2BGR, 4); cvtColor(myuv,mbgra,CV_YUV420sp2BGR,4);

then split the matrix mbgra , after this finding no of blue pixel is a binary mask operation on the splitted matrix's, since its native and you are not going to look into each pixel by pixel it might be fast. 然后分割矩阵mbgra,这之后发现没有蓝色像素是对分割后的矩阵的二进制掩码操作,因为它的本机状态并且您不会逐像素查看每个像素,所以它可能很快。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM