OpenCV RAW到RGB的转换导致不正确的elemSize和深度



使用OpenCV RAW到RGB转换(CV_BayerBG2BGR(时,图像显示正确,但图像基本类型不正确(elemSize&depth(。

即使将转换后的Mat写入文件并加载(下面代码中的rgb_path(,加载的rgb图像和转换后的rgb图片之间也存在差异,尽管两者都显示良好。

这导致了一个下游问题,我从Mat转换为uint8_t*,因为转换后的rgb图像中的缓冲区大小较大。

这是转换本身的问题,还是我对转换/OpenCV基本数据类型的理解?我使用的是OpenCV 341。

int main() {
Mat img = imread(rgb_path);
ifstream ifd(raw_path, ios::binary | ios::ate);
int size = ifd.tellg();
ifd.seekg(0, ios::beg);
vector<char> buffer;
buffer.resize(size);
ifd.read(buffer.data(), size);
Mat rgb_image;
Mat raw_image(600, 800, CV_16UC1, buffer.data());
cvtColor(raw_image, rgb_image, CV_BayerBG2BGR);
cout << "elemSize() orig: " << img.elemSize() << endl;
cout << "elemSize() conv: " << rgb_image.elemSize() << endl;
cout << "channels() conv: " << rgb_image.channels() << endl;
cout << "channels() orig: " << img.channels() << endl;
cout << "depth() conv: " << rgb_image.depth() << endl;
cout << "depth() orig: " << img.depth() << endl;
cout << "total() orig: " << img.total() << endl;
cout << "total() conv: " << rgb_image.total() << endl;
return 0;
}
Output:
elemSize() orig: 3
elemSize() conv: 6
channels() conv: 3
channels() orig: 3
depth() conv: 2
depth() orig: 0

与转换后的图像的比特度有关的问题,该图像仍为16位格式,而我预期为8位。通过添加解决:

Mat rgb_image8u;
rgb_image.convertTo(rgb_image8u, CV_8UC3, 1.0/255);

后行:

cvtColor(raw_image, rgb_image, CV_BayerBG2BGR);

最新更新