Ask Your Question
0

Read/extract pixel value of a GeoTiff

asked 2016-04-21 03:56:39 -0600

msbt gravatar image

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

edit retag flag offensive close merge delete

Comments

Are you using GDAL C++ API to open images or OpenCV with GDAL support?

strann gravatar imagestrann ( 2016-04-21 07:27:37 -0600 )edit

OpenCV + GDAL

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

msbt gravatar imagemsbt ( 2016-04-21 07:30:38 -0600 )edit

2 answers

Sort by ยป oldest newest most voted
1

answered 2016-06-06 05:08:53 -0600

msbt gravatar image

Ok I finally got it working and of course, all I needed was the original GDAL Tutorial (http://www.gdal.org/gdal_tutorial.html) 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;
        cout<<*p;

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

    }

}
edit flag offensive delete link more
1

answered 2016-04-21 10:26:16 -0600

strann gravatar image

updated 2016-04-22 02:22:07 -0600

As far as I know OpenCV doesn't provide methods to retrieve metadata from GeoTiff. Moreover OpenCV can't handle images with band interleaving. So I would suggest loading images with GDAL C++ API or LibTiff and than converting to cv::Mat.

edit flag offensive delete link more

Comments

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

msbt gravatar imagemsbt ( 2016-04-25 03:21:36 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2016-04-21 03:56:39 -0600

Seen: 3,594 times

Last updated: Jun 06 '16