1

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;
GDALAllRegister();
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?

lazexe
  • 367
  • 4
  • 19

2 Answers2

0

I don't have an examples in GDAL API but i have done in python library. You can do the similar thing following a similar logic by getting the image value to array and loop through it while apply some conditional change to it.

import numpy as np
from osgeo import gdal
ds = gdal.Open("test.tif")
myarray = np.array(ds.GetRasterBand(1).ReadAsArray())
... 
Teng Ma
  • 353
  • 2
  • 9
0

First you need to know the data type:

GDALDataType dataType=band->GetRasterDataType();

That will give you a number that is define as:

typedef enum {
    /*! Unknown or unspecified type */          GDT_Unknown = 0,
    /*! Eight bit unsigned integer */           GDT_Byte = 1,
    /*! Sixteen bit unsigned integer */         GDT_UInt16 = 2,
    /*! Sixteen bit signed integer */           GDT_Int16 = 3,
    /*! Thirty two bit unsigned integer */      GDT_UInt32 = 4,
    /*! Thirty two bit signed integer */        GDT_Int32 = 5,
    /*! Thirty two bit floating point */        GDT_Float32 = 6,
    /*! Sixty four bit floating point */        GDT_Float64 = 7,
    /*! Complex Int16 */                        GDT_CInt16 = 8,
    /*! Complex Int32 */                        GDT_CInt32 = 9,
    /*! Complex Float32 */                      GDT_CFloat32 = 10,
    /*! Complex Float64 */                      GDT_CFloat64 = 11,
    GDT_TypeCount = 12          /* maximum type # + 1 */
} GDALDataType;

So if your GDALDataType is a 6 then your data type is a float so then you can read a pixel value like so:

float value=0;
band->RasterIO(GF_Read,i,j,1,1,&value,1,1,band->GetRasterDataType(),1,1,nullptr);
std::cout << "value = " << value << std::endl;
user1145922
  • 339
  • 3
  • 13