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.