Qt
シンプルなボタンとラベルで構成される非常にシンプルな UI を作成しました。ボタンのclicked()
信号が発信されると、Web カメラを使用してフレームをキャプチャする関数OpenCV
が呼び出されます。これを達成するために現在使用しているコードは次のとおりです。
cv::Mat MainWindow::captureFrame(int width, int height)
{
//sets the width and height of the frame to be captured
webcam.set(CV_CAP_PROP_FRAME_WIDTH, width);
webcam.set(CV_CAP_PROP_FRAME_HEIGHT, height);
//determine whether or not the webcam video stream was successfully initialized
if(!webcam.isOpened())
{
qDebug() << "Camera initialization failed.";
}
//attempts to grab a frame from the webcam
if (!webcam.grab()) {
qDebug() << "Failed to capture frame.";
}
//attempts to read the grabbed frame and stores it in frame
if (!webcam.read(frame)) {
qDebug() << "Failed to read data from captured frame.";
}
return frame;
}
QImage
フレームがキャプチャされた後、ラベルに表示されるように変換する必要があります。これを実現するために、次の方法を使用します。
QImage MainWindow::getQImageFromFrame(cv::Mat frame) {
//converts the color model of the image from RGB to BGR because OpenCV uses BGR
cv::cvtColor(frame, frame, CV_RGB2BGR);
return QImage((uchar*) (frame.data), frame.cols, frame.rows, frame.step, QImage::Format_RGB888);
}
私のクラスのコンストラクタはMainWaindow
次のようになります。
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
resize(1280, 720);
move(QPoint(200, 200));
webcam.open(0);
fps = 1000/25;
qTimer = new QTimer(this);
qTimer->setInterval(fps);
connect(qTimer, SIGNAL(timeout()), this, SLOT(displayFrame()));
}
QTimer
を呼び出してフレームを表示することになっていますdislayFrame()
void MainWindow::displayFrame() {
//capture a frame from the webcam
frame = captureFrame(640, 360);
image = getQImageFromFrame(frame);
//set the image of the label to be the captured frame and resize the label appropriately
ui->label->setPixmap(QPixmap::fromImage(image));
ui->label->resize(ui->label->pixmap()->size());
}
timeout()
その信号が発せられるたびに。ただし、これはある程度機能しているように見えますが、実際には、Web カメラ ( Logitech Quickcam Pro 9000 ) からのビデオ キャプチャ ストリームが繰り返し開いたり閉じたりします。これは、Web カメラがオンになっていることを示す青いリングが点滅を繰り返していることからもわかります。これにより、Web カメラ ビデオ ストリーム ラベルのリフレッシュ レートが非常に低くなり、望ましくありません。この「ちらつき」の発生を防ぐために、Web カメラ ストリームを開いたままにする方法はありますか?