Home > Software engineering >  Camera2 byteArray to BGR uint8 mat conversion
Camera2 byteArray to BGR uint8 mat conversion

Time:08-17

I'm trying to convert byteArray from camera2 onImageAvailable listener to Mat object, and then passing it to an algorithm for dehazing in c . I have tried different methods available for converting byteArray to Mat channel 3 object but whenever I split the mat object in 3 channels then all of them get filled with garbage data which further cause crash.

Here're the different methods used for converting byte array to mat

    val bmp = BitmapFactory.decodeByteArray(bytes, 0, bytes.size)
    val orig = Mat(bmp.height, bmp.width, CvType.CV_8UC3)
    val myBitmap32 = bmp.copy(Bitmap.Config.ARGB_8888, true)
    Utils.bitmapToMat(myBitmap32,orig)

Using imread

        val matImage = Imgcodecs.imread(file!!.absolutePath,IMREAD_COLOR)

using decodebyte array

val bitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.size)
Utils.bitmapToMat(bitmap,matImage)

here's c code for the JNICALL

dehazing(JNIEnv *env, jobject, jlong input, jlong output)
{
    cv::Mat& mInput = *((cv::Mat*) input);
    cv::Mat& mOutput = *((cv::Mat*) output);
    dehaze(mInput, mOutput);
}

and finally here's a little chunk of c code

Mat dehazedSplit[3];
Mat ABGR[3];
Mat tBGR[3];
Mat imageBGR[3];
Mat blurredImageBGR[3];
// Normalize, blur image and extract dimensions
W = image.cols;
H = image.rows;

image.convertTo(image, CV_64FC3);
image = image / Scalar(255, 255, 255);

GaussianBlur(image, blurredImage, Size(41, 41), 30, 30);
split(blurredImage, blurredImageBGR);

// Estimate the A matrix
A = Mat(H, W, CV_64FC3, Scalar(1, 1, 1));

split(A, ABGR);

minMaxLoc(blurredImageBGR[0], &minVal, &maxVal, &minLoc, &maxLoc);
ABGR[0] = Scalar(maxVal);
minMaxLoc(blurredImageBGR[1], &minVal, &maxVal, &minLoc, &maxLoc);
ABGR[1] = Scalar(maxVal);
minMaxLoc(blurredImageBGR[2], &minVal, &maxVal, &minLoc, &maxLoc);
ABGR[2] = Scalar(maxVal);

// Estimate the t matrix
t = Mat(H, W, CV_64FC3, Scalar(0, 0, 0));
split(t, tBGR);

tBGR[0] = (Scalar(1) - blurredImageBGR[0].mul(Scalar(twBlue)) / ABGR[0]);
tBGR[1] = (Scalar(1) - blurredImageBGR[1].mul(Scalar(twGreen)) / ABGR[1]);
tBGR[2] = (Scalar(1) - blurredImageBGR[2].mul(Scalar(twRed)) / ABGR[2]);

Any suggestion/help will be appreciated.

CodePudding user response:

After a lot of discussion over OpenCV forum, the solution to convert ARGB to BRG came out as

Mat argb2bgr(const Mat &m) {
Mat _r(m.rows, m.cols, CV_8UC3);
mixChannels({m}, {_r}, {1,2, 2,1, 3,0}); // drop a, swap r,b
return _r;
}

The link to discussion is here

Besides it, what actually worked for me, was to drop the last channel of 4 channel MAT object this way

mixChannels({m}, {_r}, {0,0, 1,1, 2,2});
  • Related