Ask Your Question

msbt's profile - activity

2019-10-07 21:38:42 -0500 received badge  Popular Question (source)
2016-06-06 05:23:17 -0500 received badge  Necromancer (source)
2016-06-06 05:23:17 -0500 received badge  Self-Learner (source)
2016-06-06 05:08:53 -0500 answered a question Read/extract pixel value of a GeoTiff

Ok I finally got it working and of course, all I needed was the original GDAL Tutorial ( and some tweaking.

However, I will have to make a serious workaround to implement the opencv functionality in a regular qt window, because the highgui windows can't do what I need it to.

Here's the complete code fragment if anyone else ever needs it:

void mouseEvent(int evt, int x, int y, int flags, void* ptr)
    cv::Point pt(x, y);

    if (evt == CV_EVENT_LBUTTONDOWN)
        Point*p = (Point*)ptr;
        p->x = x;
        p->y = y;

        GDALDatasetH hDataset = GDALOpen( "C:/path/to/project/image.tif", GA_ReadOnly );
        if( hDataset == NULL )
            qDebug() << "data set is null";

        GDALRasterBandH hBand;
        int             nBlockXSize, nBlockYSize;
        int             bGotMin, bGotMax;
        double          adfMinMax[2];

        hBand = GDALGetRasterBand( hDataset, 1 );
        GDALGetBlockSize( hBand, &nBlockXSize, &nBlockYSize );

        adfMinMax[0] = GDALGetRasterMinimum( hBand, &bGotMin );
        adfMinMax[1] = GDALGetRasterMaximum( hBand, &bGotMax );
        if( ! (bGotMin && bGotMax) )
            GDALComputeRasterMinMax( hBand, TRUE, adfMinMax );

        float *pafScanline;
        int   nXSize = GDALGetRasterBandXSize( hBand );
        pafScanline = (float *) CPLMalloc(sizeof(float)*nXSize);
        GDALRasterIO( hBand, GF_Read, x, y, 1, 1,
                      pafScanline, nXSize, 1, GDT_Float32,
                      0, 0 );

        qDebug() << x << y << pafScanline[0];


2016-04-25 03:21:36 -0500 commented answer Read/extract pixel value of a GeoTiff

That's unfortunate, thanks for your input! I'll try some other things and keep this updated.

2016-04-21 07:30:38 -0500 commented question Read/extract pixel value of a GeoTiff


cv::Mat img = cv::imread("geo.tif", cv::IMREAD_LOAD_GDAL | cv::IMREAD_ANYDEPTH );

2016-04-21 04:38:15 -0500 asked a question Read/extract pixel value of a GeoTiff

Hi there! I'm using Qt with GDAL and OpenCV to convert and display GeoTiffs. Showing works fine, the images open in the opencv popup window and I can pan and zoom around. What I want to do now is click on the image and read out the stored value of the GeoTiff, which differs from altitude/elevation to volume or others calculations I have done before. In GIS applications you have the "identify" tool which shows the layer information ((F)ID, attributes, channels and the values), that's what I want, the value of a specific coordinate by clicking on it.

I tried it with GDAL, but there should be a better way to do it directly with opencv, right?

gdallocationinfo.exe image.tif 10 10

Report: Location: (10P,10L)
Band 1:

Value: 1680.770004272461

Again, I'm not trying to read RGB values, but the ones that are stored in the GeoTiff Channel 1 (which are 32Bit Floating Point Images with values from zero to several million).

Best regards and thanks for pointers in the right direction! M