简体   繁体   English

如何使用OpenCV在Android中将原始图像转换为RGB?

[英]How can I convert a raw image to RGB in Android using OpenCV?

I need to make some processing on each raw frame and displaying it in the preview. 我需要对每个原始帧进行一些处理,并将其显示在预览中。 Here's the code I'm using (in Kotlin): 这是我正在使用的代码(在Kotlin中):

fun Image.toLinearRgb(): Mat {
    val bayer16Bit = Mat(height, width, CvType.CV_16UC1, planes[0].buffer)
    val bayer8Bit = Mat().apply {
        bayer16Bit.convertTo(this, CvType.CV_8UC1, 0.0625)
    }
    val rgb8Bit = Mat().apply {
        Imgproc.cvtColor(bayer8Bit, this, Imgproc.COLOR_BayerGR2RGB)
    }
    bayer16Bit.release()
    bayer8Bit.release()
    return rgb8Bit
}

I've ended up getting a strange rgb frame with a lot of green pixels. 我最终得到了一个奇怪的带有许多绿色像素的rgb框架。

According to the docs, the raw image (through ImageFormat.RAW_SENSOR) is a 16 bit, single channel, bayered one. 根据文档,原始图像(通过ImageFormat.RAW_SENSOR)是16位,单通道,拜耳式。 I'm probably missing something because just allocating the byte buffer in a CV_16UC1 mat and converting it to RGB don't work. 我可能会丢失一些东西,因为仅在CV_16UC1垫中分配字节缓冲区并将其转换为RGB不起作用。 Also, it seems that the resulting frame is too dark even for a linear color space. 另外,即使对于线性色彩空间,最终的框架似乎也太暗。 I'd appreciate any help! 我将不胜感激!

There are different bayer pattern layouts, and without knowing the camera's layout, I'd guess you got the wrong one. 拜耳模式有不同的布局,并且在不知道相机布局的情况下,我想您会错了。 Opencv refers to them by the first 2 colors in the upper left hand corner: cv::COLOR_Bayer BG 2BGR cv::COLOR_Bayer左上角的前两种颜色引用它们: cv::COLOR_Bayer BG 2BGR

In opencv, these are supported by using different flags: 在opencv中,使用不同的标志来支持这些标志:

cv::COLOR_BayerRG2BGR
cv::COLOR_BayerGR2BGR
cv::COLOR_BayerGB2RGB

...etc. ...等等。 A full list can be found here: https://docs.opencv.org/3.1.0/de/d25/imgproc_color_conversions.html 完整列表可以在这里找到: https : //docs.opencv.org/3.1.0/de/d25/imgproc_color_conversions.html

You can try to research your platform's bayer layout, or you can just experiment till it works. 您可以尝试研究平台的Bayer布局,也可以尝试直到它起作用。 Good Luck! 祝好运!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM