Reputation: 417
I need write a RGB image with the R,G,B components (Mat Type of OpenCV). I have a code that work with GDT_Byte but If I change to GDT_UInt16 not working anymore.
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);
If I work with images with 8 bits and change GDT_UInt16 to GDT_Byte and GUInt16 to GByte the I don't have problem.
*Note: The problem isn't the load of images (I think) since I write a priori with opencv the image RGB. I read the images' components with: (I'm working in architecture x64)
img=imread(fname_B.c_str(),CV_LOAD_IMAGE_UNCHANGED|CV_LOAD_IMAGE_ANYDEPTH);
B = Mat(img);
I have also tried this code to read R, G and 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);
Also, the result of GDALINFO is:
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
I noticed that the problem is only interpretation because, when I open the RGB file created, I can separate the bands.
Upvotes: 1
Views: 3073
Reputation: 2366
One note is that OpenCV 3.0 beta has support for loading images directly with imread using GDAL bindings.
/// 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 );
Upvotes: 1