Mohammad Ersan
Mohammad Ersan

Reputation: 12444

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). the image format is NV21 (really am not expert in image formats, so I don't why they use it)

and this is how I read images from the 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.

Upvotes: 0

Views: 475

Answers (1)

isrish
isrish

Reputation: 692

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; 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.

Upvotes: 1

Related Questions