Solved Qlabel not updated when using opencv + QT to play video on Qlabel,
-
I use opencv to capture one frame from a video, dispay it on qt lable, code is below
void camView::showVideo_1Frm(std::string filePath, int iFrm)
{
cv::Mat frame, result_frame;
cv::VideoCapture capture(filePath); // read local vedio
if (capture.isOpened())
{
capture.set(cv::CAP_PROP_FRAME_COUNT, iFrm);
capture >> frame;
int num_totalVedio_frm = capture.get(cv::CAP_PROP_FRAME_COUNT); //test
double length = int(capture.get(cv::CAP_PROP_FRAME_COUNT)); // test
double frm_fps = capture.get(cv::CAP_PROP_FPS); // test
if (!frame.empty())
{
ui->LVideo->clear();
cvtColor(frame, result_frame, CV_BGR2RGB);
cv::resize(result_frame, result_frame, cv::Size(640, 480));
QImage image((const uchar*)result_frame.data, result_frame.cols, result_frame.rows, QImage::Format_RGB888);
ui->LVideo->setPixmap(QPixmap::fromImage(image));
ui->LVideo->resize(ui->LVideo->pixmap()->size());
}
}
else
std::cout << "loading error,please check the path" << std::endl;
}Now the code just show the first frame of picture , why? iFrm is increased from 1 to 2000 normally,ui->LVideo is QLabel type
-
Hi
Its hardly something with QLabel.
So check you do read new frames etc.
Maybe just save image to file for 10 images and check they are different. -
@hitbuyi said in Qlabel not updated when using opencv + QT to play video on Qlabel,:
showVideo_1Frm
how do you call
showVideo_1Frm
? with a timer or something? -
@J-Hilk with a timer,every 100ms, my opencv is 4.10, below code works for opencv 2.4,
void camView::showVideo_1Frm(std::string filePath, int iFrm) {
cv::Mat frame;
cv::VideoCapture capture(filePath); // read video indicated by filePath
cv::VideoCapture *pV = NULL;
if (capture.isOpened()) {
pV = &capture;
(*pV).set(cv::CAP_PROP_FRAME_COUNT, iFrm);
(*pV) >> frame;
if (!frame.empty()) {
cvtColor(frame, frame, CV_BGR2RGB);
cv::resize(frame, frame, cv::Size(640, 480));QImage image((const uchar*)frame.data, frame.cols, frame.rows, QImage::Format_RGB888); ui->LVideo->setPixmap(QPixmap::fromImage(image)); ui->LVideo->resize(ui->LVideo->pixmap()->size()); ui->LVideo->show(); } } else std::cout << "loading error,please check the path" << std::endl;
}
BUT NOT work for opencv 4.10, i doubt my lib has problem
-
Hi,
What is the use of pV ?
Beside that, did you check that your frame variable contains something valid ?
-
pV is pointer to video
The problem is solved, the fault is that
(*pV).set(cv::CAP_PROP_FRAME_COUNT, iFrm);
should be
(*pV).set(cv::CAP_PROP_POS_FRAMES, iFrm ); -
I know it's a pointer. The problem is that in your code, it's completely useless. Just use the capture object directly rather than what you do with pV.
-
@SGaist your are right,