I am having trouble with displaying opencv Video on a Qlabel.
I'm new using opencv and qt, and these week I was trying to make a tiny exercise using a qt button event to display a video capture from opencv to a qlabel of my widget. But weirdly the program says "The program has unexpectedly finished", when I run the code attached below. Please help me cuz for me nothing seems to be wrong. Thanks for your time and greetings from Costa Rica.
P.S. When I simply try to run the openCv code without any gui, I mean just using the code inside the buttonClicked event and cvShowImage("Video", frame); to display the video the program run good but strip an error and several warnings like this.
HIGHGUI ERROR: V4L/V4L2: VIDIOC_S_CROP
libpng warning: Application built with libpng-1.2.49 but running with 1.5.12
libpng warning: Application built with libpng-1.2.49 but running with 1.5.12
libpng warning: Application built with libpng-1.2.49 but running with 1.5.12
libpng warning: Application built with libpng-1.2.49 but running with 1.5.12
libpng warning: Application built with libpng-1.2.49 but running with 1.5.12
libpng warning: Application built with libpng-1.2.49 but running with 1.5.12
libpng warning: Application built with libpng-1.2.49 but running with 1.5.12
libpng warning: Application built with libpng-1.2.49 but running with 1.5.12
libpng warning: Application built with libpng-1.2.49 but running with 1.5.12
libpng warning: Application built with libpng-1.2.49 but running with 1.5.12
Attached code
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <cv.h>
#include <highgui.h>
using namespace std;
using namespace cv;
IplImage* imgTracking=0;
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_pushButton_clicked()
{
CvCapture *cap; cap = cvCaptureFromCAM(1);
IplImage *frame; frame = cvQueryFrame(cap);
bool play = true;
while(frame && play){
cvWaitKey(10); IplImage *img = cvCloneImage(frame);
if (img->origin) {
cvFlip(img);
img->origin= 0;
}
QImage qimg;
qimg = IplImage2QImage(img);
//cvShowImage("Video", frame);
ui->labVideo->setPixmap(QPixmap::fromImage(qimg));
cvReleaseImage(&img);
frame = cvQueryFrame(cap);
}
cvReleaseCapture(&cap);
}
QImage MainWindow::IplImage2QImage(const IplImage *iplImage)
{
int height = iplImage->height;
int width = iplImage->width;
const uchar *qImageBuffer =(const uchar*)iplImage->imageData;
QImage img(qImageBuffer, width, height, QImage::Format_RGB888);
return img.rgbSwapped();
}
It is so wrong. When you use Qt you shouldn't use cvWaitKey and have own loop. This function is just addition in openCv for testing or when you don't have ui freamework. When you use Qt you have UI framework and openCV should be used to image processing only (this is the purpose of this library)!
Replace this loop with QTimer and let QEventLoop do its job.
CvCapture *cap must be field of class.
void MainWindow::on_timerTimeout()
{
IplImage *frame = cvQueryFrame(cap);
if (!frame) {
stopPlay();
return;
}
IplImage *img = cvCloneImage(frame);
if (img->origin) {
cvFlip(img);
img->origin= 0;
}
QImage qimg = IplImage2QImage(img);
ui->labVideo->setPixmap(QPixmap::fromImage(qimg)); // possible replace with signal emit newFrame(QPixmap::fromImage(qimg));
cvReleaseImage(&img);
}
void MainWindow::stopPlay() {
timer->stop();
cvReleaseCapture(&cap);
}
void MainWindow::on_pushButton_clicked() {
timer->start();
cap = cvCaptureFromCAM(1);
}
Related
I have an interesting problem when i switched to OpenCV 4.3.0 version.
I built OpenCV 4.3.0 with Qt using Cmake 3.16.0 and mingw730_64 and build done in Release mode on Windows 10 machine
When i compile a simple code including VideoCapture, i can build it without errors but when i run it in Release mode exe simply crashes..
#include <opencv2/core.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/imgcodecs.hpp>
..
void MainWindow::on_pushButton_2_clicked()
{
..
VideoCapture capWebcam(0);
..
}
There is no problem when reading and displaying images..!
void MainWindow::on_pushButton_clicked()
{
Mat Test_Image;
QString fileName = QFileDialog::getOpenFileName(this, "Open Image File", QDir::currentPath(), "Image (*.*)");
if(QFile::exists(fileName))
{
Test_Image = imread(fileName.toStdString(), IMREAD_COLOR);
if(Test_Image.empty()){
QMessageBox msg;
msg.setText("File couldn't load!");
msg.exec();
}
else {
namedWindow("Test Window", WINDOW_AUTOSIZE);
flip(Test_Image, Test_Image,0);
cvtColor(Test_Image, Test_Image, COLOR_BGR2GRAY);
imshow("Test Window", Test_Image);
waitKey(1);
}
}
Can anyone help me understand why the program crashes immediately when i instantiate VideoCapture object?
C:\Qt_Projects\build-Deneme1-Desktop_Qt_5_14_2_MinGW_64_bit-Release\release\Deneme1.exe crashed.
Thank you in advance.
I am trying to display a video in QLabet in QT Creator. I am reading video using openCV. Here is my code:
mainwindow.cpp
#include "includes.h"
#include "vidreadthread.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
VidReadThread *thread1 = new VidReadThread("Video read thread");
thread1->start();
}
MainWindow::~MainWindow()
{
delete ui;
}
vidreadthread.cpp
#include "vidreadthread.h"
#include "includes.h"
using namespace cv;
extern MainWindow *mainPtr;
VidReadThread::VidReadThread(QString s) : name(s)
{
}
void VidReadThread::run()
{
QThread::msleep(100);
VideoCapture cap;
cap.open("helicopter_with_stickers.mp4");
while(1)
{
Mat image1;
// Capture frame-by-frame
cap >> image1;
// If the frame is empty, break immediately
if (image1.empty())
break;
QImage image2 = QImage((uchar*) image1.data, image1.cols, image1.rows, image1.step, QImage::Format_RGB888);
mainPtr->ui->label1->setPixmap(QPixmap::fromImage(image2));
}
}
I am able to display the video but I can't set frame rate. Whole 60sec of video gets over in 4-5 frames. With only OpenCV I have command on frame rate with cvWaitkey(), but here msleep() doesn't seem to be working for a similar application. Please suggest a way to do so without frame skipping. I made a vidreadthread so that GUI doesn't get hanged while video is being read.
If there is any other way by which I can display OpenCV window inside my QT UI, then please recommend that as well.
try moveto thread may works better
.cpp
for (int i = 0; i<Camera::getCameraCount();)
ui->comboBox->addItem(QString::number(i++)); //name camera
camera = new Camera();
camera->moveToThread(&thread);
connect(this, SIGNAL(cameraOperate(int)), camera, SLOT(Operate(int)));
connect(camera, SIGNAL(updateImage(QImage)), this, SLOT(updateImage(QImage)));
void app0::updateImage(QImage image)
{
ui->videoviewer->setPixmap(QPixmap::fromImage(image));
}
camera thread:
void Camera::Operate(int _index)
{
if (open(_index)) { qDebug() << "Camera open success!";}
else { qDebug() << "Camera open failed!"; return; }
if (capture.get(28) == -1) { cout << "get 28 -1" << "\n";emit }
while (1)
{
qApp->processEvents();
Mat matin = read(); //read mat
matnow = matin;
QImage image = Mat2QImage(matin);
emit updateImage(image);
}
}
link:https://blog.csdn.net/Sun_tian/article/details/104236327
I have Windows 8 on "DELL Inspiron 5537" installed and i create a virtual machine from "Virtual box" and installed Ubuntu 13.04 in it. And then I installed Opencv Version=2.4.9 through commands in Ubuntu's terminal now I want to do camera interfacing in opencv which is very necessary for my project to move forward..my code contains no error still it is not working the way i want.
My code is given below:
#include <iostream>
#include <cv.h>
#include <highgui.h>
using namespace std;
char key;
int main()
{
cvNamedWindow("Camera_Output", 1); //Create window
CvCapture* capture = cvCaptureFromCAM(CV_CAP_ANY);
//Capture using any camera connected to your system
while(1){ //Create infinte loop for live streaming
IplImage* frame = cvQueryFrame(capture); //Create image
frames from capture
cvShowImage("Camera_Output", frame); //Show image
frames on created window
key = cvWaitKey(10); //Capture Keyboard stroke
if (char(key) == 27){
//cvSaveImage("webcam_frame.png",frame);
}
}
cvReleaseCapture(&capture); //Release capture.
cvDestroyWindow("Camera_Output"); //Destroy Window
return 0;
}
when I run this code in my terminal it is not capturing the image rather it shows a blank window. I have attached a photo of output of this code.
This code to display a video using opencv with Visual studio
i have been looking everywhere for a tutorial how to use Qt with opencv to display video
but i couldn't find any :/
is there anyone here knows how to do that?
#include <opencv\highgui.h>
#include <opencv\cv.h>
int main(int argc, char** argv)
{
CvCapture* capture1 = cvCreateFileCapture("c:\\VideoSamples\\song.avi");
IplImage* frame1;
cvNamedWindow( "display video1", CV_WINDOW_AUTOSIZE );
while(1)
{
frame1 = cvQueryFrame( capture1 );
cvSmooth( frame1, out, CV_GAUSSIAN, 17, 17 );
if( !frame1 ) break;
cvShowImage( "display video1", frame1 );
char c = cvWaitKey(33);
if( c == 27 ) break;
}
cvReleaseCapture( &capture1 );
cvDestroyWindow( "display video1" );
}
You can easily display a cv::Mat in a QLabel:
Assuming frame is your current RGB-videoframe with 8bit depth as cv::Mat-object and label is a pointer to your QLabel:
//convert to QPixmap:
QPixmap pixmap = QPixmap::fromImage(QImage((uchar*)frame.data, frame.cols, frame.rows, frame.step, QImage::Format_RGB888));
//set scaled pixmap as content:
label->setPixmap(pixmap.scaled(frame.cols, frame.rows, Qt::KeepAspectRatio));
For starters, you've got to make sure that the OpenCV libraries you are using have been built with Qt support.
You will probably need to download the source code (available on Github), configure the build using CMake, and re-build them yourself. Here is the link to the guide on how to build the OpenCV libraries from source.
Once that is done, this is an example of how to capture frames from a camera (just swap camera with file for your case) and display the frames to a window, making use of the Qt framework.
Hope this helps you.
I'm capturing the webcam image with OpenCV. That works fine. But if I want to close the OpenCV when a button is pressed, it does not work (tried both cvDestroyWindow("NameOfWindow")and cvDestroyAllWindows()). The window stays open and the application is still running.
The OpenCV was initialized on separate thread from the main GUI.
I'm using the Juce Framework with C++ on my Mac. But the same problem occurs also on Windows with Qt and Windows Forms, when the OpenCV Window has it's own cvNamedWindow.
Here is the basic code of the VST plugin editor class:
PluginEditor.cpp
#include <opencv2/opencv.hpp>
#include <opencv2/highgui/highgui.hpp>
#include "PluginProcessor.h"
#include "PluginEditor.h"
//
TestAudioProcessorEditor::TestAudioProcessorEditor (TestAudioProcessor* ownerFilter)
: AudioProcessorEditor (ownerFilter)
{
// This is where our plugin's editor size is set.
setSize (500, 500);
// open the tracker
openTracker();
}
// code for opencv handling
TestAudioProcessorEditor::openTracker() {
// KEY LINE: Start the window thread
cvStartWindowThread();
// Create a window in which the captured images will be presented
cvNamedWindow( "Webcam", CV_WINDOW_AUTOSIZE );
cvWaitKey(0);
cvDestroyWindow( "Webcam" );
// window should disappear!
}
TestAudioProcessorEditor::~TestAudioProcessorEditor()
{
}
// paint stuff on the vst plugin surface
void TestAudioProcessorEditor::paint (Graphics& g) {
}
The piece you may be missing is a call to the cvStartWindowThread function.
On Linux, using the GTK HighGUI, this example reproduced your problem, until I put in the call to cvStartWindowThread.
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc_c.h"
#include <iostream>
using namespace std;
int main( int argc, char** argv )
{
// KEY LINE: Start the window thread
cvStartWindowThread();
// Open a window
cvNamedWindow("original", 0);
// Wait until a key gets pressed inside the window
cvWaitKey(0);
// Close the window
cvDestroyWindow("original");
// Verify that the window is closed
cout<<"The window should be closed now. (Press ENTER to continue.)"<<endl;
string line;
getline(cin, line);
cout<<"Exiting..."<<endl;
}
If cvStartWindowThread doesn't help, try doing an extra call to cvWaitKey after your cvDestroy call.
To run the example, compile it with GCC:
g++ destroy_window.cpp -o destroy_window -lopencv_core -lopencv_highgui