Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android convert byte array from Camera API to color Mat object openCV

I'm trying to take data captured off the camera API and do color postprocessing. My issue is that while I can convert a byte[] object to a Mat object, I cannot get the color channels without receiving an error. I started with the following post: How to get the Mat object from the Byte[] in openCV android? but that implementation does not work.

Here is the relevant code:

@Override
public void onPictureTaken(byte[] data, Camera camera) {
    // The camera preview was automatically stopped. Start it again.
    mCamera.startPreview();
    mCamera.setPreviewCallback(this);

    // Write the image in a file (in jpeg format)
    File pictureFile_interp = getOutputMediaFile(MEDIA_TYPE_IMAGE);

    try {

        //Now try to convert and save.
        Parameters params = camera.getParameters();
        Size sz = params.getPictureSize();

        //ERROR IN THE LINE BELOW!!!!
        Mat raw = new Mat(sz.height,sz.width,CvType.CV_8UC4);
        raw.put(0, 0, data);
        Mat targ = Highgui.imdecode(raw, 0);

        orig = new Mat();
        Imgproc.cvtColor(targ, orig, Imgproc.COLOR_GRAY2RGB);
        //Imgproc.cvtColor(targ, fixed, Imgproc.COLOR_BGR2RGB);

        //now we have the target we want...let's interpolate.
        interp = interpExperiment(orig,interpBy);
        Highgui.imwrite(pictureFile_interp.toString(), interp);

    } finally{

    }

}

When I try that code, I get an exception:

Provided data element number should be multiple of the Mat channels count (3)

Instead I replace the problematic line with

Mat raw = new Mat(sz.height,sz.width,CvType.CV_8U);
raw.put(0, 0, data);
Mat targ = Highgui.imdecode(raw, 0);

I can get a grayscale image. What am I doing wrong with

Mat raw = new Mat(sz.height,sz.width,CvType.CV_8UC3);

? I've looked at quite a few stackoverflow posts addressing this and none work for the color matrix. Any help is much appreciated.

like image 294
user3502402 Avatar asked Feb 13 '23 03:02

user3502402


2 Answers

I found a workaround to this: using Bitmapfactory, you can convert the byte[] array to a bitmap, which you can in turn correctly convert into a Mat.

Bitmap bmp = BitmapFactory.decodeByteArray(data , 0, data.length);
Mat orig = new Mat(bmp.getHeight(),bmp.getWidth(),CvType.CV_8UC3);
Bitmap myBitmap32 = bmp.copy(Bitmap.Config.ARGB_8888, true);
Utils.bitmapToMat(myBitmap32, orig);

Imgproc.cvtColor(orig, orig, Imgproc.COLOR_BGR2RGB,4);

Seems there should be a better way but this works.

like image 168
user3502402 Avatar answered Feb 15 '23 16:02

user3502402


public Mat Byte_to_Mat(byte[] data) {

    Mat jpegData = new Mat(1, data.length, CvType.CV_8UC1);
    jpegData.put(0, 0, data);


    Mat bgrMat = new Mat();
    bgrMat = Highgui.imdecode(jpegData, Highgui.IMREAD_COLOR);
like image 33
Idol Avatar answered Feb 15 '23 17:02

Idol