Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Geospatial Imagery Viewer using GDAL, openCV, QT
Forum Update on Monday, May 27th 2025

Geospatial Imagery Viewer using GDAL, openCV, QT

Scheduled Pinned Locked Moved Unsolved General and Desktop
3 Posts 2 Posters 592 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    Kyeiv
    wrote on last edited by
    #1

    Hi all,
    I am trying to achieve some simple image viewer for formats like nitf, geotif etc.
    For now I can read some files and display them with QLabel and openCV's imshow().
    The problem I stumbled upon is that some images are displayed corrupted when using QLabel while imshow() displays them correctly. Can someone pinpoint things that i am doing wrong?

    SamplesLINK
    For i_3004g the only good preview is opencv, for i_3301a the Qt is better.

    Here is my code:

    #include <QApplication>
    #include <QFileDialog>
    #include <QLabel>
    
    #include <gdal/gdal.h>
    #include <gdal/gdal_priv.h>
    
    #include <opencv2/opencv.hpp>
    #include <opencv2/imgproc/imgproc.hpp>
    
    using namespace cv;
    
    QSharedPointer<QLabel> create_preview(uchar* data, int cols, int rows, std::string title)
    {
        QImage qt_image(data,
                         cols,
                         rows,
                         QImage::Format_RGB888);
    
        QVector<QRgb> colorTable;
        for(int i=0; i<256; ++i)
           colorTable << qRgb(i,i,i);
    
       qt_image.setColorTable(colorTable);
       QSharedPointer<QLabel> myLabel = QSharedPointer<QLabel>::create();
       myLabel->setPixmap(QPixmap::fromImage(qt_image));
       title += "Format_RGB888";
       QString displayable_title = QString::fromStdString(title);
       myLabel->setWindowTitle(displayable_title);
       return myLabel;
    }
    
    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
    
        QString imagePath = QFileDialog::getOpenFileName(Q_NULLPTR,"Open File","");
    
        const std::string imageName = imagePath.toStdString();
        const std::string output = "output_im.nitf";
    
        std::cout << "Reading png file: " << imageName << std::endl;
        Mat image2 = imread(imageName, cv::IMREAD_LOAD_GDAL | cv::IMREAD_COLOR );
        //cvtColor(image2, image2, CV_BGR2RGB);
        //NOTE: for some reason openCV's window displays in RGB but openCV Mat keeps data in BGR
        //NOTE using this with nor channels == 3 data causes runtime errors
        //imwrite(imageName, image2);
    
        std::cout << "Displaying png file: " << imageName << std::endl;
        namedWindow( imageName, WINDOW_AUTOSIZE );
        imshow( imageName, image2 );
    
        GDALAllRegister();
    
        GDALDataset * pOldDS, *pNewDS;
        GDALDriver *pDriverTiff;
        pDriverTiff = GetGDALDriverManager()->GetDriverByName("NITF");
    
        std::cout << "Reading gdal file: " << imageName << std::endl;
        pOldDS = (GDALDataset*) GDALOpen(imageName.c_str(), GA_ReadOnly);
        std::cout << "Saving to file: " << output << std::endl;
        pNewDS = pDriverTiff->CreateCopy(output.c_str(), pOldDS, FALSE, NULL, NULL, NULL);
    
        std::cout << "Closing datasets..." << std::endl;
        GDALClose(pNewDS);
        GDALClose(pOldDS);
        std::cout << "Completed!" << std::endl;
    
        std::cout << "Reading nitf file: " << output << std::endl;
        Mat image = imread(output, cv::IMREAD_LOAD_GDAL | cv::IMREAD_COLOR );
        //NOTE: for some reason openCV's window displays in RGB but openCV Mat keeps data in BGR
        //cvtColor(image, image, CV_BGR2RGB);
    
        std::cout << "Displaying nitf file: " << output << std::endl;
        namedWindow( output, WINDOW_AUTOSIZE );
        imshow( output, image );
    
        //============QT PREVIEWS============================================
        auto in_preview = create_preview(image2.data, image2.cols, image2.rows, "in_qt");
        auto ou_preview = create_preview(image.data, image.cols, image.rows, "ou_qt");
    
        in_preview->show();
        ou_preview->show();
    
        return a.exec();
    }
    
    

    Regards Kris

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      Can you show what you are getting ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      K 1 Reply Last reply
      0
      • SGaistS SGaist

        Hi,

        Can you show what you are getting ?

        K Offline
        K Offline
        Kyeiv
        wrote on last edited by
        #3

        @SGaist Screenshot from 2019-12-13 07-56-56.png

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved