用GDAL和OpenCV写一个RGB 16位图像

时间:2014-07-10 03:20:21

标签: c++ opencv visual-c++ gdal

我需要用R,G,B组件(Mat Type of OpenCV)写一个RGB图像。我有一个与GDT_Byte一起使用的代码,但如果我改为GDT_UInt16则不再工作。

metadata_info=(GDALDataset*)GDALOpen(fname.c_str(),GA_ReadOnly);
metadata_info->GetGeoTransform(adfGeoTransform);

target = poDriver->Create(fname_t.c_str(),sX,sY,3,GDT_UInt16,NULL);
target->SetGeoTransform(GT);
target->SetProjection( metadata_info->GetProjectionRef() );

band = target->GetRasterBand(1);
band->RasterIO(GF_Write,0,0,sX,sY,(void *)R.data,sX,sY,GDT_UInt16,0,0);

band = target->GetRasterBand(2);
band->RasterIO(GF_Write,0,0,sX,sY,(void *)G.data,sX,sY,GDT_UInt16,0,0);

band = target->GetRasterBand(3);
band->RasterIO(GF_Write,0,0,sX,sY,(void *)B.data,sX,sY,GDT_UInt16,0,0);

如果我处理8位图像并将GDT_UInt16更改为GDT_Byte并将GUInt16更改为GByte,则表示我没有问题。

*注意:问题不在于图像的负载(我认为),因为我用opencv先写了RGB图像。我用以下内容阅读图像的组件:(我在架构x64中工作)

img=imread(fname_B.c_str(),CV_LOAD_IMAGE_UNCHANGED|CV_LOAD_IMAGE_ANYDEPTH);
B = Mat(img);

我也试过这段代码来读R,G和B:

poDataset=(GDALDataset*) GDALOpen(fname.c_str(),GA_ReadOnly);
poBand=poDataset->GetRasterBand(1);
sX=poBand->GetXSize(); sY= poBand->GetYSize();
B=Mat(nYSize, nXSize, CV_16UC1);
poBand->RasterIO(GF_Read,0,0,sX,sY,(void*)B.data,sX,sY,GDT_UInt16,0,0);

此外,GDALINFO的结果是:

Driver: GTiff/GeoTIFF
Files: test_gdal.tif
Size is 7611, 7761
...
Metadata:
  AREA_OR_POINT=Area
Image Structure Metadata:
  INTERLEAVE=PIXEL
Corner Coordinates:
...
Band 1 Block=7611x1 Type=UInt16, ColorInterp=Gray
Band 2 Block=7611x1 Type=UInt16, ColorInterp=Undefined
Band 3 Block=7611x1 Type=UInt16, ColorInterp=Undefined

我注意到问题只是解释,因为当我打开创建的RGB文件时,我可以分开乐队。

1 个答案:

答案 0 :(得分:1)

需要注意的是,OpenCV 3.0 beta支持使用GDAL绑定直接使用imread加载图像。

https://github.com/Itseez/opencv/blob/master/samples/cpp/tutorial_code/HighGUI/GDAL_IO/gdal-image.cpp

/// Load generic image as 8-bit color
cv::Mat image = cv::imread(argv[1], cv::IMREAD_LOAD_GDAL | cv::IMREAD_COLOR ); 

/// load a 16-bit image grayscale
cv::Mat dem = cv::imread(argv[2], cv::IMREAD_LOAD_GDAL | cv::IMREAD_ANYDEPTH );