Camera2 byteArray到BGR uint8 mat的转换



我正在尝试将byteArray从camera2 onImageAvailable侦听器转换为Mat对象,然后将其传递给c++中的一个算法以进行去雾处理。我尝试了不同的方法来将byteArray转换为Mat通道3对象,但每当我将Mat对象拆分为3个通道时,所有通道都会被垃圾数据填充,这会进一步导致崩溃。

以下是用于将字节数组转换为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)

使用imread

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

使用解码字节阵列

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

这是JNICALL 的c代码

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

最后是一小段c++代码

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]);

如有任何建议/帮助,我们将不胜感激。

在OpenCV论坛上进行了大量讨论后,将ARGB转换为BRG的解决方案出现了

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

讨论链接在这里

除此之外,实际上对我有效的是,以这种方式丢弃4通道MAT对象的最后一个通道

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

最新更新