QT image display is not working without declaring openCV namedWindow in windows 10
-
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
-
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.
-
Hi,
Please show your code.
-
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();
}
-
You're blocking the event loop with your technique hence your issue.
Use a timer to trigger the read of the next frame.
-
This post is deleted!
-
@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 -
@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.
-
@SGaist You are right. Let me check this with timer and then I will get back to you.
-
@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 -
@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?
-
@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.
-
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
-
@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 ....
-
@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. -
@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. -
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.
-
@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!
6/17