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. Problem in show image in a MainWindow
Forum Updated to NodeBB v4.3 + New Features

Problem in show image in a MainWindow

Scheduled Pinned Locked Moved General and Desktop
2 Posts 2 Posters 2.3k 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.
  • L Offline
    L Offline
    lifedj
    wrote on last edited by
    #1

    I'm trying to create a simple program that load an image from a file and show it in my MainWindow using opencv and Qt!
    The problems are 2:

    1. When I load the image, it show it in the MainWindow only after use imshow! If I try to delete imshow from the list of operation to do, the program crashes!
    2. I inserted the code explained before in a function and I would like to call it continuosly, but only after the pressing of a button!
      The problem is that if I try to call the function by on_pushButton_clicked() the system crashes, instead if I call it at the start (in main() ) it works fine!

    What should be the problem? How can I solve it?

    Here I will copy the code:

    main.cpp
    @
    #include <QtGui/QApplication>

    #include "mainwindow.h"

    #include "ui_mainwindow.h"

    #include <unistd.h>

    int cattura_avviata=0;

    int main(int argc, char *argv[])

    {

    QApplication a(argc, argv);
    
    MainWindow w;
    
    
    
        w.show();
    
        terminale=0;
    
        //cattura_avviata=1;
    
        //w.cattura();
    
    
    
    return a.exec&#40;&#41;;
    

    }

    int MainWindow::get_cattura_avviata()

    {

    return cattura_avviata;
    

    }

    void MainWindow::set_cattura_avviata(int var)

    {

    cattura_avviata=var;
    

    }

    void MainWindow::cattura()

    {

    CvCapture *input;
    
        const char *fileName = "C:/Users/Lifedj/Documents/video_2_converted.mp4";
    
        input = cvCaptureFromFile&#40;fileName&#41;;
    
        Mat frame;
    
        if (!input)
    
        {
    
            std::cout << "Can't open file" << fileName;
    
        }
    
        else
    
        {
    
                frame = cvQueryFrame( input );
    
                if( !frame.empty() )
    
                {
    
                    cvtColor(frame, frame, CV_BGR2RGB);
    
                        QImage immagine_convertita= QImage((uchar*) frame.data, frame.cols, frame.rows, frame.step, QImage::Format_RGB888);
    
                        QImage small = immagine_convertita.scaled(400, 900,Qt::KeepAspectRatio);
    
                        this->ui->label->setPixmap(QPixmap::fromImage(small));
    
                        imshow( window_name, frame ); //this is the instruction that I would like to eliminate
    
                    }
    
                    else
    
                    {
    
                        printf("%s -> %d \n",data_e_ora.toAscii().constData(),num_per);
    
                    }
    
                }
    
                else
    
                {
    
                    printf(" --(!) No captured frame -- Break!");
    
                }
    
                int c = waitKey(1);
    
                if( (char)c == 'c' ) { cattura_avviata=0; }
    
            }
    
      //}
    
       cvReleaseCapture(&input);
    
       if (cattura_avviata==1)
    
       {
    
           cattura();
    
       }
    
       return;
    

    }

    @

    mainwindow.cpp

    @
    void MainWindow::on_pushButton_clicked()

    {

    MainWindow w;
    
    if (w.get_cattura_avviata()==0)
    
    {
    
        ui->pushButton->setText("Ferma riconoscimento");
    
        w.set_cattura_avviata(1);
    
        w.cattura();
    
    }
    
    else
    
    {
    
        ui->pushButton->setText("Avvia riconoscimento");
    
        w.set_cattura_avviata(0);
    
    }
    

    }

    @

    Thank you in advance!

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

      For the crash part, are you sure you have initialized everything ?

      About your code:
      In on_push_button_clicked, you create a new MainWindow and do everything in there that is related to your image reading/showing, without even showing that MainWindow.

      I think the best would be to first, get your user interface clean and running and only then add the OpenCV part.

      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