Monday 24 April 2017

c++ - Correct way to extract pixel data using GDAL. Current method gives repeated but changing value.


I am trying to open a spectral file and move bands of data from GDAL to OpenCV's Mat data structures. As of now I am just trying to make sure that I can correctly access each band using GDAL and pull the pixel data.


So I want to explain what I think the following code is doing and if I am wrong(must be) then please correct me. It opens the file and loads the dataset. Then I get raster band 1 assigned to currentBand. The float pointer is then made to point at enough memory for one line of pixel data. RasterIO gets this data and places it in the newly allocated space. I then traverse the array printing the value that has been placed in each element. (Code in c++)


GDALDataset  *dataset;
GDALRasterBand *currentBand;
int blockXSize, blockYSize;
GDALAllRegister();
dataset = (GDALDataset *) GDALOpen( specImag->filename.c_str(), GA_ReadOnly );

currentBand = dataset->GetRasterBand(1);

currentBand->GetBlockSize(&blockXSize, &blockYSize);

float *fScanline;
fScanline = (float *) CPLMalloc(sizeof(float)*specImag->widthx);

currentBand->RasterIO( GF_Read, 0, 0, specImag->widthx, 1,
fScanline, specImag->widthx, 1, GDT_Float32, 0, 0 );


for(int i=0; iwidthx; i++){

std::cout << "Data Point ";
printf("%d", i);
std::cout << " is: ";
printf("%d", fScanline[i]);
std::cout << std::endl;
}

(the code is not complete and will not run as is)


When I run the code I get the same number (ex. 421076 or 4210432) for every element in the array. If I wait a little bit and run the program again I get a different number than the prevous run but still the same in each element.


What am i missing when it comes to accessing the pixel/data in each band?





No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...