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. Load From Image Variable to QLabel
Forum Updated to NodeBB v4.3 + New Features

Load From Image Variable to QLabel

Scheduled Pinned Locked Moved Unsolved General and Desktop
8 Posts 3 Posters 1.1k Views 1 Watching
  • 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.
  • D Offline
    D Offline
    dedetuga
    wrote on last edited by dedetuga
    #1

    Hi,
    I'm trying load an image on QLabel but I find any info about it
    this is my code:

        image sized = letterbox_image(im, net->w, net->h);
    ...
    ...
    ...Other code ....
    ...
    show_image(sized,"Threat",10);
    

    I want o make the same show_image(sized,"Threat",10); but instead of open an new window I prefer to load it to my gui

    any idea?
    thanks

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

      Hi,

      What is that image variable ?

      In any case, take a look at QLabel::setPixmap to get started.

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

      1 Reply Last reply
      1
      • D Offline
        D Offline
        dedetuga
        wrote on last edited by
        #3

        Hi @SGaist,
        no matter what I try I can do it.

        @SGaist said in Load From Image Variable to QLabel:

        QLabel::setPixmap

        I try;

        show_image(sized,"Threat",10);
            ui->label_image->setPixmap(QPixmap(sized).scaled(ui->label_image->size(),Qt::KeepAspectRatio));
            ui->label_image->setPixmap(QPixmap::fromImage(Ipl2QImage(sized)).scaled(ui->label_image->size(),Qt::KeepAspectRatio));
            ui->label_image->setPicture(input);
        

        I allways get the error:

        /home/filipe/Documentos/QT_Projects/Qt_Yolo/mainwindow.cpp:107: error: no matching function for call to ‘QPixmap::QPixmap(image&)’
        ui->label_image->setPixmap(QPixmap(sized).scaled(ui->label_image->size(),Qt::KeepAspectRatio));
        ^

        do you Know how to sove this?

        aha_1980A 1 Reply Last reply
        0
        • D dedetuga

          Hi @SGaist,
          no matter what I try I can do it.

          @SGaist said in Load From Image Variable to QLabel:

          QLabel::setPixmap

          I try;

          show_image(sized,"Threat",10);
              ui->label_image->setPixmap(QPixmap(sized).scaled(ui->label_image->size(),Qt::KeepAspectRatio));
              ui->label_image->setPixmap(QPixmap::fromImage(Ipl2QImage(sized)).scaled(ui->label_image->size(),Qt::KeepAspectRatio));
              ui->label_image->setPicture(input);
          

          I allways get the error:

          /home/filipe/Documentos/QT_Projects/Qt_Yolo/mainwindow.cpp:107: error: no matching function for call to ‘QPixmap::QPixmap(image&)’
          ui->label_image->setPixmap(QPixmap(sized).scaled(ui->label_image->size(),Qt::KeepAspectRatio));
          ^

          do you Know how to sove this?

          aha_1980A Offline
          aha_1980A Offline
          aha_1980
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @dedetuga you will need to provide some more context, especially the involved types of the variable.

          From your code fragment we can only guess.

          Qt has to stay free or it will die.

          1 Reply Last reply
          0
          • D Offline
            D Offline
            dedetuga
            wrote on last edited by
            #5

            Sorry @aha_1980 I got the Idea this is just a simple conversion issue.

            Full code on this file:

            #include "mainwindow.h"
            #include "ui_mainwindow.h"
            #include "darknet/include/darknet.h"
            #include <iostream>
            //#include "darknet.h"
            #include <sstream>
            #include <QtCore/QString>
            #include <opencv2/opencv.hpp>
            #include <QImage>
            #include <QLabel>
            #include <QPixmap>
            
            using namespace std;
            
            
            
            MainWindow::MainWindow(QWidget *parent) :
                QMainWindow(parent),
                ui(new Ui::MainWindow)
            {
                ui->setupUi(this);
            
                cout << "Darknet application" << endl;
            
                // -----------------------------------------------------------------------------------------------------------------
                // Define constants that were used when Darknet network was trained.
                // This is pretty much hardcoded code zone, just to give an idea what is needed.
                // -----------------------------------------------------------------------------------------------------------------
            
                // Path to configuration file.
                static char *cfg_file = const_cast<char *>("/home/filipe/Documentos/QT_Projects/darknetapp/darknet/cfg/yolov3-tiny.cfg");
                // Path to weight file.
                static char *weight_file = const_cast<char *>("/home/filipe/Documentos/QT_Projects/darknetapp/darknet/yolov3-tiny.weights");
                // Path to a file describing classes names.
                static char *names_file = const_cast<char *>("/home/filipe/Documentos/QT_Projects/darknetapp/darknet/data/coco.names");
                // This is an image to test.
                static char *input = const_cast<char *>("/home/filipe/Documentos/QT_Projects/darknetapp/darknet/data/dog.jpg");
                // Define thresholds for predicted class.
                float thresh = 0.5;
                float hier_thresh = 0.5;
                // Uncomment this if you need sort predicted result.
            //    float nms = 0.45;
            
                // Number of classes in "obj.names"
                // This is very rude method and in theory there must be much more elegant way.
                size_t classes = 0;
                char **labels = get_labels(names_file);
                while (labels[classes] != nullptr) {
                    classes++;
                }
                cout << "Num of Classes " << classes << endl;
            
                // -----------------------------------------------------------------------------------------------------------------
                // Do actual logic of classes prediction.
                // -----------------------------------------------------------------------------------------------------------------
            
                // Load Darknet network itself.
                network *net = load_network(cfg_file, weight_file, 0);
                // In case of testing (predicting a class), set batch number to 1, exact the way it needs to be set in *.cfg file
                set_batch_network(net, 1);
            
                // Load test image.
                image im = load_image_color(input, 0, 0);
                // And scale it to the parameters define din *.cfg file.
                image sized = letterbox_image(im, net->w, net->h);
                // Uncomment this if you need sort predicted result.
            //    layer l = net->layers[net->n - 1];
            
                // Get actual data associated with test image.
                float *frame_data = sized.data;
            
                // Do prediction.
                double time = what_time_is_it_now();
                network_predict(net, frame_data);
                cout << "'" << input << "' predicted in " << (what_time_is_it_now() - time) << " sec." << endl;
            
                // Get number fo predicted classes (objects).
                int num_boxes = 0;
                detection *detections = get_network_boxes(net, im.w, im.h, thresh, hier_thresh, nullptr, 1, &num_boxes);
                cout << "Detected " << num_boxes << " obj, class " << detections->classes << endl;
            
                // Uncomment this if you need sort predicted result.
            //    do_nms_sort(detections, num_boxes, l.classes, nms);
            
                // -----------------------------------------------------------------------------------------------------------------
                // Print results.
                // -----------------------------------------------------------------------------------------------------------------
            
                // Iterate over predicted classes and print information.
                for (int8_t i = 0; i < num_boxes; ++i) {
                    for (uint8_t j = 0; j < classes; ++j) {
                        if (detections[i].prob[j] > thresh) {
                            // More information is in each detections[i] item.
                            std::stringstream stds;
                            stds << labels[j] << " " << (int16_t) (detections[i].prob[j] * 100) << endl;
                            cout << labels[j] << " " << (int16_t) (detections[i].prob[j] * 100) << endl;
                            QString outPut = ui->label_output->text() + QString::fromStdString(stds.str());
                            ui->label_output->setText(outPut);
                        }
                    }
                }
            
                // -----------------------------------------------------------------------------------------------------------------
                // Free resources.
                // -----------------------------------------------------------------------------------------------------------------
                show_image(sized,"Threat",10);
                ui->label_image->setPixmap(QPixmap(sized).scaled(ui->label_image->size(),Qt::KeepAspectRatio));
                ui->label_image->setPixmap(QPixmap::fromImage(Ipl2QImage(sized)).scaled(ui->label_image->size(),Qt::KeepAspectRatio));
                ui->label_image->setPicture(input);
                /*QImage Image(im.data, im.c, im.h, im.w, QImage::Format_RGB888);
                ui->label_image->resize(Image.size);
                ui->label_image->setPixmap(QPixmap::fromImage(Image));
            */
            
            
                //QPixmap Qimg = QPixmap::fromImage(img);
                free_detections(detections, num_boxes);
                free_image(im);
                free_image(sized);
                free(labels);
            }
            
            MainWindow::~MainWindow()
            {
                delete ui;
            }
            
            

            Source: ldarknetapp

            Thanks

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

              @dedetuga said in Load From Image Variable to QLabel:

              sized

              What is the exact type of that variable ? Include all the relevant namespaces.

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

              D 1 Reply Last reply
              0
              • SGaistS SGaist

                @dedetuga said in Load From Image Variable to QLabel:

                sized

                What is the exact type of that variable ? Include all the relevant namespaces.

                D Offline
                D Offline
                dedetuga
                wrote on last edited by dedetuga
                #7

                hi @SGaist,

                @SGaist said in Load From Image Variable to QLabel:

                What is the exact type of that variable ? Include all the relevant namespaces.

                   // Load test image.
                    image im = load_image_color(input, 0, 0);
                    // And scale it to the parameters define din *.cfg file.
                    image sized = letterbox_image(im, net->w, net->h);
                

                I this the image original is loaded to the var image im the the image is resized and saved in the var image sized

                I'm able to open this images with the line show_image(sized,"Threat",10); but open in a net window and I want try open the image on some widget from qt creator.

                I conclude image is a image var like string var but for images, but i dont find nothing saying how to manipulate this kind of var in qtcreator or load from an image variable to a label or graphicsView

                thanks

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

                  So image is in fact an OpenCV Image classe object ?

                  If so, you have to first convert it to a QImage object.

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

                  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