Ask Your Question
0

process Android image with opencv

asked 2012-12-19 00:56:38 -0600

Smartdog gravatar image

I have this code :

    protected Bitmap processFrame(byte[] data) {
        Mat mat = new Mat(mFrameHeight, mFrameWidth, CvType.CV_8UC3);
        mat.put(0, 0, data);

        //process mat with native code

        Utils.matToBitmap(mat, mBitmap);

        return mBitmap;
    }



private Camera.PreviewCallback previewCallback = new Camera.PreviewCallback() {

    public void onPreviewFrame(byte[] data, Camera camera) {
        Bitmap bmp = processFrame(data);

        if (bmp != null) {
            //draw bmp
        }
    }
};

at some point I define the bitmap as:

mBitmap = Bitmap.createBitmap(mFrameWidth, mFrameHeight, Bitmap.Config.RGB_565);

The result is the same camera frames captured but in gray scale repeated 3 times horizontally, I tried CvType.CV_8UC4 and they are repeated 4 times instead.

I need to display the whole image as is using the same steps

Does anybody catch the mistake ??

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
3

answered 2012-12-19 06:18:48 -0600

Android camera gives you frames in YUV format. You need to convert it to RGB/RGBA image before drawing them on screen. You need something like this:

mRgba = new Mat();
mYuv = new Mat(getFrameHeight() + getFrameHeight() / 2, getFrameWidth(), CvType.CV_8UC1);
mBitmap = Bitmap.createBitmap(previewWidth, previewHeight, Bitmap.Config.ARGB_8888);

protected Bitmap processFrame(byte[] data) {
    mYuv.put(0, 0, data);    
    Imgproc.cvtColor(mYuv, mRgba, Imgproc.COLOR_YUV420sp2RGB, 4);

    //process mat with native code

    Utils.matToBitmap(mRgba, mBitmap);
    return mBitmap;
}

mGraySubmat = mYuv.submat(0, getFrameHeight(), 0, getFrameWidth()) gives you only Y plain of YUV image that is equivalent of grace scale.

edit flag offensive delete link more

Question Tools

Stats

Asked: 2012-12-19 00:56:38 -0600

Seen: 2,905 times

Last updated: Dec 19 '12