Problem in show image in a MainWindow



  • 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!


  • Lifetime Qt Champion

    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.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.