Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Geospatial Imagery Viewer using GDAL, openCV, QT



  • 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


  • Lifetime Qt Champion

    Hi,

    Can you show what you are getting ?




Log in to reply