title: write a RGB 16 bits image with GDAL and OpenCV
body: <p>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.</p>
  
  <pre><code>metadata_info=(GDALDataset*)GDALOpen(fname.c_str(),GA_ReadOnly);
  metadata_info-&gt;GetGeoTransform(adfGeoTransform);
  
  target = poDriver-&gt;Create(fname_t.c_str(),sX,sY,3,GDT_UInt16,NULL);
  target-&gt;SetGeoTransform(GT);
  target-&gt;SetProjection( metadata_info-&gt;GetProjectionRef() );
  
  band = target-&gt;GetRasterBand(1);
  band-&gt;RasterIO(GF_Write,0,0,sX,sY,(void *)R.data,sX,sY,GDT_UInt16,0,0);
  
  band = target-&gt;GetRasterBand(2);
  band-&gt;RasterIO(GF_Write,0,0,sX,sY,(void *)G.data,sX,sY,GDT_UInt16,0,0);
  
  band = target-&gt;GetRasterBand(3);
  band-&gt;RasterIO(GF_Write,0,0,sX,sY,(void *)B.data,sX,sY,GDT_UInt16,0,0);
  </code></pre>
  
  <p>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.</p>
  
  <p>*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)</p>
  
  <pre><code>img=imread(fname_B.c_str(),CV_LOAD_IMAGE_UNCHANGED|CV_LOAD_IMAGE_ANYDEPTH);
  B = Mat(img);
  </code></pre>
  
  <p>I have also tried this code to read R, G and B:</p>
  
  <pre><code>poDataset=(GDALDataset*) GDALOpen(fname.c_str(),GA_ReadOnly);
  poBand=poDataset-&gt;GetRasterBand(1);
  sX=poBand-&gt;GetXSize(); sY= poBand-&gt;GetYSize();
  B=Mat(nYSize, nXSize, CV_16UC1);
  poBand-&gt;RasterIO(GF_Read,0,0,sX,sY,(void*)B.data,sX,sY,GDT_UInt16,0,0);
  </code></pre>
  
  <p>Also, the result of GDALINFO is:</p>
  
  <pre><code>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
  </code></pre>
  
  <p><strong>I noticed that the problem is only interpretation because, when I open the RGB file created, I can separate the bands.</strong></p>