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. QT image display is not working without declaring openCV namedWindow in windows 10
QtWS25 Last Chance

QT image display is not working without declaring openCV namedWindow in windows 10

Scheduled Pinned Locked Moved Solved General and Desktop
qt 5.14.2c++ qtopencvwindows 10
17 Posts 5 Posters 3.3k 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.
  • I Offline
    I Offline
    Imran Hassan
    wrote on last edited by
    #1

    I am using windows 10 with QT 5.14 opencv 4.20 with contrib version.

    I am displaying the image captured by webcam to QT label. It was working perfect in Ubuntu but after migrating to windows it is not working. It work only if I create openCV namedWindow("frame",0). It creates extra window of openCV. I installed openCV 4.20 with_FFMPEG open checked.

    I am not able to find the reason? If I tried to make namedWindow() hidden or invisible but there is no option for that. Can anyone help me in this regard?

    Regards

    1 Reply Last reply
    0
    • I Offline
      I Offline
      Imran Hassan
      wrote on last edited by
      #16

      @SGaist @Bonnie @jsulm

      The problem is solved by adding

      qApp->processEvents(); after the

      ui->label->setPixmap(QPixmap::fromImage(QImage(frame.data, frame.cols, frame.rows, frame.step, QImage::Format_RGB888).rgbSwapped()));

      @SGaist as you said this is not very good organized code having loops but for now this is temporary solution we found out. We will reorganize the code and remove the unnecessary loops.

      Anyway Thanks you all for such a great help.

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

        Hi,

        Please show your code.

        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
        • I Offline
          I Offline
          Imran Hassan
          wrote on last edited by
          #3

          Here is the code. If I comment the imshow( "Frame", frame ); its not working and uncommenting this line works but it opens two windows .... Any help will be highly appreciated.

          I tried with many installations of openCV with FFMPEG on and off and GStreamer on and off, adding #define NO_DSHOW_STRSAFE in cap_dshow.cpp file but not getting any solution.

          void MainWindow::on_pushButton_clicked()
          {
          VideoCapture cap(0);
          if(!cap.isOpened()){
          cout << "Error opening video stream or file" << endl;

          }
          
          while(1){
          
            Mat frame;
            cap >> frame;
            if (frame.empty())
              break;
          

          // imshow( "Frame", frame );

            ui->label->setPixmap(QPixmap::fromImage(QImage(frame.data, frame.cols, frame.rows, frame.step, QImage::Format_RGB888).rgbSwapped()));
          
            char c=(char)waitKey(25);
            if(c==27)
              break;
          }
          
          cap.release();
          destroyAllWindows();
          

          }

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

            You're blocking the event loop with your technique hence your issue.

            Use a timer to trigger the read of the next frame.

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

            I 2 Replies Last reply
            1
            • I Offline
              I Offline
              Imran Hassan
              wrote on last edited by
              #5
              This post is deleted!
              1 Reply Last reply
              0
              • SGaistS SGaist

                You're blocking the event loop with your technique hence your issue.

                Use a timer to trigger the read of the next frame.

                I Offline
                I Offline
                Imran Hassan
                wrote on last edited by
                #6

                @SGaist
                Actually this is working fine in Ubuntu but I switched it in windows it started having problem. The same code is working in Ubuntu

                SGaistS 1 Reply Last reply
                0
                • I Imran Hassan

                  @SGaist
                  Actually this is working fine in Ubuntu but I switched it in windows it started having problem. The same code is working in Ubuntu

                  SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by
                  #7

                  @Imran-Hassan yes: it's sheer luck. The wait for key you put there might act differently on Linux than on Windows but in any case the issue remains: you're doing it wrong. Since you wait 25 milliseconds anyway, refactor your code to use a QTimer so it does not block the event loop on any platform.

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

                  I 1 Reply Last reply
                  1
                  • SGaistS SGaist

                    You're blocking the event loop with your technique hence your issue.

                    Use a timer to trigger the read of the next frame.

                    I Offline
                    I Offline
                    Imran Hassan
                    wrote on last edited by
                    #8

                    @SGaist You are right. Let me check this with timer and then I will get back to you.

                    1 Reply Last reply
                    0
                    • SGaistS SGaist

                      @Imran-Hassan yes: it's sheer luck. The wait for key you put there might act differently on Linux than on Windows but in any case the issue remains: you're doing it wrong. Since you wait 25 milliseconds anyway, refactor your code to use a QTimer so it does not block the event loop on any platform.

                      I Offline
                      I Offline
                      Imran Hassan
                      wrote on last edited by
                      #9

                      @SGaist said in QT image display is not working without declaring openCV namedWindow in windows 10:

                      25 milliseconds anyway

                      I did it using timer with 1000 ms and its working fine.

                      There is one issue dear. This was just a sample code, actually I my main code is too big and a lot of things to do in while loop very long while loop and there timers will not work as sync issue will occur. Actually that main code was also working perfect in Linux and when that was switched to windows this all happened. I am also using threads in that code.

                      Dear your guidance and help is required in this matter.
                      Regards

                      jsulmJ 1 Reply Last reply
                      0
                      • I Imran Hassan

                        @SGaist said in QT image display is not working without declaring openCV namedWindow in windows 10:

                        25 milliseconds anyway

                        I did it using timer with 1000 ms and its working fine.

                        There is one issue dear. This was just a sample code, actually I my main code is too big and a lot of things to do in while loop very long while loop and there timers will not work as sync issue will occur. Actually that main code was also working perfect in Linux and when that was switched to windows this all happened. I am also using threads in that code.

                        Dear your guidance and help is required in this matter.
                        Regards

                        jsulmJ Offline
                        jsulmJ Offline
                        jsulm
                        Lifetime Qt Champion
                        wrote on last edited by
                        #10

                        @Imran-Hassan said in QT image display is not working without declaring openCV namedWindow in windows 10:

                        Dear your guidance and help is required in this matter.

                        What guidance and help exactly?

                        https://forum.qt.io/topic/113070/qt-code-of-conduct

                        I 1 Reply Last reply
                        0
                        • jsulmJ jsulm

                          @Imran-Hassan said in QT image display is not working without declaring openCV namedWindow in windows 10:

                          Dear your guidance and help is required in this matter.

                          What guidance and help exactly?

                          I Offline
                          I Offline
                          Imran Hassan
                          wrote on last edited by Imran Hassan
                          #11

                          @jsulm Here about this issue ....

                          I am displaying the image captured by webcam to QT label. It was working perfect in Ubuntu but after migrating to windows it is not working. It work only if I create openCV namedWindow("frame",0). It creates extra window of openCV. I installed openCV 4.20 with_FFMPEG open checked.

                          The code shown in above thread was just a sample code, actually I my main code is too big and a lot of things to do in while loop very long while loop and there timers will not work as sync issue will occur. Actually that main code was also working perfect in Linux and when that was switched to windows this all happened. I am also using threads in that code.

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

                            As the code you show is just an example mostly not related to your actual code:

                            • Do not block the event loop
                            • Do not use tight loops like that in the main thread because it blocks the event loop
                            • Refactor your code
                            • Do not use threads unless it's really really really necessary and before using them check once more
                            • If you really need to use threads: start read the documentation and literature about them. It shall be considered as a nuclear bomb to shoot your foot with. Then if you use the QThread, thoroughly read its latest documentation as it has severely improved over the years and shows the two common ways to use them. If you use signals and slots with QThread and start to specify the connection type then you are extremely likely to be doing something wrong.
                            • Because something works on Linux, it does not mean it will work on any other platform
                            • The fact that FFMPEG is used as backend for OpenCV is unrelated to the issue at hand

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

                            I 1 Reply Last reply
                            2
                            • SGaistS SGaist

                              As the code you show is just an example mostly not related to your actual code:

                              • Do not block the event loop
                              • Do not use tight loops like that in the main thread because it blocks the event loop
                              • Refactor your code
                              • Do not use threads unless it's really really really necessary and before using them check once more
                              • If you really need to use threads: start read the documentation and literature about them. It shall be considered as a nuclear bomb to shoot your foot with. Then if you use the QThread, thoroughly read its latest documentation as it has severely improved over the years and shows the two common ways to use them. If you use signals and slots with QThread and start to specify the connection type then you are extremely likely to be doing something wrong.
                              • Because something works on Linux, it does not mean it will work on any other platform
                              • The fact that FFMPEG is used as backend for OpenCV is unrelated to the issue at hand
                              I Offline
                              I Offline
                              Imran Hassan
                              wrote on last edited by
                              #13

                              @SGaist Yes I have checked the whole code again.

                              I am confused with one thing that If I run the existing code in Linux it works fine. In window also if I call namedWindow() function of openCV it start working. It will show video on two windows one in QT windows and other openCV windows, even after closing the openCV window by pressing cross it is still working. If I do not call namedWindow() function it is not working.

                              namedWindow() funtion is triggering something which makes it work.

                              I even replaced loop with timer in my main code but problem remains same there ....

                              B 1 Reply Last reply
                              0
                              • I Imran Hassan

                                @SGaist Yes I have checked the whole code again.

                                I am confused with one thing that If I run the existing code in Linux it works fine. In window also if I call namedWindow() function of openCV it start working. It will show video on two windows one in QT windows and other openCV windows, even after closing the openCV window by pressing cross it is still working. If I do not call namedWindow() function it is not working.

                                namedWindow() funtion is triggering something which makes it work.

                                I even replaced loop with timer in my main code but problem remains same there ....

                                B Offline
                                B Offline
                                Bonnie
                                wrote on last edited by Bonnie
                                #14

                                @Imran-Hassan
                                Hey, I've tried your code with opencv 4.3.0 vc15 (downloaded from the official website) and it works fine with my cam (except that "waitKey" so I can't exit the loop by pressing Esc).
                                And, wow, that loop doesn't block the UI. So magical.

                                1 Reply Last reply
                                0
                                • I Offline
                                  I Offline
                                  Imran Hassan
                                  wrote on last edited by
                                  #15

                                  @Imran-Hassan said in QT image display is not working without declaring openCV namedWindow in windows 10:

                                  ui->label->setPixmap(QPixmap::fromImage(QImage(frame.data, frame.cols, frame.rows, frame.step, QImage::Format_RGB888).rgbSwapped()));

                                  Is it working in windows?
                                  Ok I will try with openCV 4.3.0 in windows.

                                  1 Reply Last reply
                                  0
                                  • I Offline
                                    I Offline
                                    Imran Hassan
                                    wrote on last edited by
                                    #16

                                    @SGaist @Bonnie @jsulm

                                    The problem is solved by adding

                                    qApp->processEvents(); after the

                                    ui->label->setPixmap(QPixmap::fromImage(QImage(frame.data, frame.cols, frame.rows, frame.step, QImage::Format_RGB888).rgbSwapped()));

                                    @SGaist as you said this is not very good organized code having loops but for now this is temporary solution we found out. We will reorganize the code and remove the unnecessary loops.

                                    Anyway Thanks you all for such a great help.

                                    Pablo J. RoginaP 1 Reply Last reply
                                    0
                                    • I Imran Hassan

                                      @SGaist @Bonnie @jsulm

                                      The problem is solved by adding

                                      qApp->processEvents(); after the

                                      ui->label->setPixmap(QPixmap::fromImage(QImage(frame.data, frame.cols, frame.rows, frame.step, QImage::Format_RGB888).rgbSwapped()));

                                      @SGaist as you said this is not very good organized code having loops but for now this is temporary solution we found out. We will reorganize the code and remove the unnecessary loops.

                                      Anyway Thanks you all for such a great help.

                                      Pablo J. RoginaP Offline
                                      Pablo J. RoginaP Offline
                                      Pablo J. Rogina
                                      wrote on last edited by
                                      #17

                                      @Imran-Hassan said in QT image display is not working without declaring openCV namedWindow in windows 10:

                                      The problem is solved

                                      great, so please don't forget to mark your post as such!

                                      Upvote the answer(s) that helped you solve the issue
                                      Use "Topic Tools" button to mark your post as Solved
                                      Add screenshots via postimage.org
                                      Don't ask support requests via chat/PM. Please use the forum so others can benefit from the solution in the future

                                      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