2

Qt で OpenCv を使用して USB WebCamera に取り組んでいます。コードは、画像の撮影、画像の保存などには正常に機能しますが、ランダムにエラー「VIDIOC_QUERYCTRL: 入力/出力エラー」が生成され、qt アプリケーション全体がシャットダウンします。

実際、私のアプリケーションのシナリオは次のとおりです。

MainWindow.c には、押したときにカメラが起動するプッシュボタンがあります。

void MainWindow::on_pushButton_continue_pressed()
{
    camcapture = new CamCapture( imgResolution, defaultPath, this);
    connect(camcapture, SIGNAL(imgChange(QString)), this, SLOT(changeImg(QString)));
    camcapture->show();
}

そして、ここに私のCamCaptureクラスの一部があります:

CamCapture::CamCapture( int Resolution, QString path, QWidget *parent) :
    QFrame(parent),
    ui(new Ui::CamCapture), timer_(this), counter_(0)
{
    ui->setupUi(this);

    /*----some variable initialization---*/

    camInit();

    timer_.start(100);
    connect(&timer_,SIGNAL(timeout()), this,SLOT(captureLoop()));
}

void CamCapture::camInit()
{
    capture_ = cvCaptureFromCAM(0); 
    if (!capture_)
    {
        qDebug()<<"cannot get webcam...";
        ui->label_image->setText("No webcam!");
        ui->label_image->setAlignment(Qt::AlignCenter);
        buttonActiveStatus( false, false, false );
        return;
    }

    cvGrabFrame(capture_); // Grabs frame from camera or file

    image_ = cvRetrieveFrame(capture_); // Gets the image grabbed with cvGrabFrame

    qDebug() << "image size : " << image_->width << "x" << image_->height;

    qDebug() << "\n" <<  ui->label_image->x();
    qDebug() << "\n" <<  ui->label_image->y();
}


QImage CamCapture::Ipl2QImage(const IplImage *newImage)
{
    QImage qtemp;
    if (newImage && cvGetSize(newImage).width > 0)
    {
        int x;
        int y;
        char* data = newImage->imageData;

        qtemp= QImage(newImage->width, newImage->height,QImage::Format_RGB32 );
        for( y = 0; y < newImage->height; y++, data +=newImage->widthStep )
            for( x = 0; x < newImage->width; x++)
            {
                uint *p = (uint*)qtemp.scanLine (y) + x;
                *p = qRgb(data[x * newImage->nChannels+2],
                          data[x * newImage->nChannels+1],data[x * newImage-  >nChannels]);
            }
    }
    return qtemp;
}

void CamCapture::captureLoop()
{
    if ( !capture_ )
    {
        return;
    }

    cvGrabFrame(capture_);
    image_ = cvRetrieveFrame(capture_);
    if (image_)
    {
        counter_ ++;
        qImage_ = Ipl2QImage(image_);
        ui->label_image-    >setPixmap(QPixmap::fromImage(qImage_.scaled(width(),height()),Qt::AutoColor));
    }
}


void CamCapture::on_pushButton_cancel_pressed()
{

      cvReleaseCapture( &capture_ );
      cvDestroyAllWindows();
      delay(100);
      qDebug() << "nb frames :" << counter_;
      CamCapture::close();
}

void CamCapture::on_pushButton_capture_pressed()
{
    savePath = _imgPath;
    timer_.stop();

    frame = cvQueryFrame(capture_); // Gets the image grabbed with cvGrabFrame
}

void CamCapture::on_pushButton_retake_pressed()
{
    timer_.start(100);
    captureLoop();
}

void CamCapture::on_pushButton_save_pressed()
{
    int iwidth  = _imgWidth  * _imgResolution * scaledFactor;
    int iheight = _imgHeight * _imgResolution * scaledFactor;

    QString str = savePath;

    IplImage *small;

    small = cvCreateImage(cvSize(iwidth,iheight), 8, 3);
    cvResize(frame, small);
    cvSaveImage(savePath.toUtf8().constData(),small);

    delay(100);
    _imgNum++;
    emit imgChange(savePath);
    on_pushButton_cancel_pressed(); 
}

上記のコードは、次のようにランダムにエラーを生成します (アプリケーションのシャットダウンを引き起こします)。

   Debugging starts
   &"warning: GDB: Failed to set controlling terminal: Invalid argument\n"
   Corrupt JPEG data: bad Huffman code
   image size :  640 x 480 

   5 

   10 
   nb frames : 11 
   VIDIOC_QUERYCTRL: Input/output error
   Debugging has finished

通常、MainWindow.c の pushButton_continue を押している間、または CamCapture.c の pushButton_Save を押している間、エラーが生成されます。

Googleで解決策が見つかりません。

この問題を解決するには、提案/ヘルプ/リンクが必要です。

前もって感謝します。

4

0 に答える 0