Has a problem. I have an tiff image (that have 4 layers). My tasks is to make small changes in pixel's color to make image better. In this case I use GDAL library. My source is:

GDALDataset  *poDataset;
poDataset = (GDALDataset *) GDALOpen(fileName.toStdString().c_str(), GA_ReadOnly);
if (poDataset == NULL) {
    QMessageBox::information(0, "error", "We have problems");
} else {
    QMessageBox::information(0, "Message", "All is ok");
int rasterCount = poDataset->GetRasterCount(); // Here is 4 raster images
GDALRasterBand *band = poDataset->GetRasterBand(1);
int width = band->GetXSize();
int height = band->GetYSize();

for (int i = 0; i < width; i++) {
    for (int j = 0; j < height; j++) {
        // cross all pixels 
        // How to get pixel color here?

So I dont know how to get pixel color in cycle. Can you give me advice pleasae?


You need to request needed bands and extent into the buffer using GDALRasterIO method (see http://www.gdal.org/classGDALRasterBand.html#a30786c81246455321e96d73047b8edf1). The usage example here: http://www.gdal.org/gdal_tutorial.html

char* pabBuff = (char *) CPLMalloc(nBufXSize * nBufYSize);

band->RasterIO(GF_Read, nXOff, nYOff, nXSize, nYSize, pabBuff, nBufXSize, nBufYSize, GDT_Byte, 0, 0, NULL);

for(int i = 0; i < nBufXSize; ++i) {
    for (j = 0; j < nBufYSize; ++j) {
       char pixel = pabBuff[i * nBufXSize + j];
  • Thanks for help, Dmitry, but I'm still not fully understand how to retrieve information that I need. Don't think that I lazy, I tried this incide cycle: band->RasterIO(GF_Read, i, j, 1, 1, buff, 1, 1, GDT_UInt16, 0, 0); Please contact with me, because I have a deadline and this problem is very important to me. Thanks for your help once more.
    – lazexe
    Sep 15 '16 at 18:40
  • Add code example to the post Sep 15 '16 at 19:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.