简体   繁体   English

如何根据GeoTIFF数据创建QImage(或正确解释)

[英]How to create a QImage from GeoTIFF data (or just interpret it correctly)

I need to create a QImage or something that can be drawn onto a screen from a geotiff image. 我需要创建一个QImage或可以从geotiff图像绘制到屏幕上的东西。 Unfortunately QT's built-in TIFF support chokes on the geotiff structures ... so to achieve this I have used the following code (which is more or less a copy paste from the gdal "tutorial" page ( https://gdal.org/gdal_tutorial.html ) except the image creation part ): 不幸的是,QT的内置TIFF支持Geotiff结构上的扼流圈...因此,为了实现这一点,我使用了以下代码(它或多或少是来自gdal“教程”页面( https://gdal.org/ gdal_tutorial.html )(图像创建部分除外):

GDALRasterBand  *poBand;
int             nBlockXSize, nBlockYSize;
int             bGotMin, bGotMax;
double          adfMinMax[2];
poBand = poDataset->GetRasterBand( 1 );
poBand->GetBlockSize( &nBlockXSize, &nBlockYSize );
adfMinMax[0] = poBand->GetMinimum( &bGotMin );
adfMinMax[1] = poBand->GetMaximum( &bGotMax );
if( ! (bGotMin && bGotMax) )
    GDALComputeRasterMinMax((GDALRasterBandH)poBand, TRUE, adfMinMax);


float *pafScanline;
int   nXSize = poBand->GetXSize();
int   nYSize = poBand->GetYSize();
pafScanline = (float *) CPLMalloc(sizeof(float)*nXSize * nYSize);
poBand->RasterIO( GF_Read, 0, 0, nXSize, nYSize, 
                  pafScanline, nXSize, nYSize, GDT_Float32, 0, 0 );

QImage* image = new QImage((unsigned char*)pafScanline, 
                          nXSize, nYSize,
                          QImage::Format_RGB32);

image->save("blaa.jpg");

Now, the image I try to load is on the left side and the one that gets displayed (and saved by Qt) is on the right side. 现在,我尝试加载的图像在左侧,显示的图像(并由Qt保存)在右侧。

Question : how to create a properly coloured image from the tiff data given that I get in floats, and I have no idea how to create a QImage data from a bunch of floats. 问题 :鉴于我进入了浮点数,如何从tiff数据创建正确着色的图像,而且我不知道如何从一堆浮点数创建QImage数据。

并排

Your input GeoTIFF may not have a single floating point band, but rather 3 (or 4) 8 bit bands. 您的输入GeoTIFF可能没有单个浮点带,而是3(或4)个8位带。

Bands in GeoTIFF are basically the image channels. GeoTIFF中的频段基本上是图像通道。 Unlike other image formats, these channels can also have floating point values. 与其他图像格式不同,这些通道还可以具有浮点值。

You can have a look at the GDAL documentation here to know more about the allowed formats. 您可以在此处查看GDAL文档以了解有关允许的格式的更多信息。

So it is likely (although I can't be 100% sure without looking at it) that your file is just an RGBA GeoTIFF, and hence, has 4 UNIT8 bands. 因此,您的文件很可能是RGBA GeoTIFF,尽管我不能100%地确定它,但是它具有4个UNIT8波段。

Therefore, your call to RasterIO is completely wrong. 因此,您对RasterIO的呼叫是完全错误的。 You should iterate over the 4 bands and copy the with RasterIO to the QImage memory buffer, respecting the bands order. 您应该遍历4个波段,并遵循波段顺序将RasterIO的RasterIO复制到QImage内存缓冲区。

Something like: 就像是:

int nBands = poDataset->GetRasterCount();

for(int b=0; b < nBands; b++)
{
    GDALRasterBand *band = poDataset->GetRasterBand(b);
    if(band != nullptr)
    {
        CPLErr error = band->RasterIO(GF_Write, 0, 0, image.width(), image.height(), image.bits() + b, image.width(), image.height(), GDT_Byte, nBands, 0);
        if(error != CE_None)
        {
            // REPORT ERROR
        }
    }
}

Please note that the code above is missing all the required checks (ensuring band type is Byte, etc), and depending on your file the band order may vary (BGRA, RGBA, ecc). 请注意,上面的代码缺少所有必需的检查(确保波段类型为Byte等),并且取决于文件的波段顺序可能会有所不同(BGRA,RGBA,ecc)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM