簡體   English   中英

使用OpenCV從相機捕獲灰屏

[英]Capturing grey screen from camera using OpenCV

從網絡攝像頭(內置hp dv5)捕獲圖像時出現問題。 唯一的結果是灰屏。 這是代碼:

#include "StdAfx.h"
#include "cv.h"
#include "highgui.h"
#include <stdio.h>  // A Simple Camera Capture Framework
int main() { 
    CvCapture* capture;
    for (int i = -1;i < 100;i++) {
        capture = cvCaptureFromCAM( i );   
        if( !capture ) {
            fprintf( stderr, "ERROR: capture is NULL \n" ); 

        } else {
            break;
        }
    }
    //cvSetCaptureProperty( capture, CV_CAP_PROP_FPS,15);
    //cvSetCaptureProperty( capture, CV_CAP_PROP_FRAME_WIDTH, 160 );
    //cvSetCaptureProperty( capture, CV_CAP_PROP_FRAME_HEIGHT, 120 );
    // Create a window in which the captured images will be presented   
    cvNamedWindow( "mywindow", CV_WINDOW_AUTOSIZE );    // Show the image captured from the camera in the window and repeat   
    while( 1 ) {     // Get one frame     
        IplImage* frame = cvQueryFrame( capture );
        cvGrabFrame(capture);
        frame = cvRetrieveFrame(capture);

        if( !frame ) {
            fprintf( stderr, "ERROR: frame is null...\n" );
            getchar();
            break;
        } else {
            fprintf( stderr, "OK\n" );  
        }
        cvShowImage( "mywindow", frame );     // Do not release the frame!      
        //If ESC key pressed, Key=0x10001B under OpenCV 0.9.7(linux version),     //remove higher bits using AND operator     
        int c = cvWaitKey(100);
    }    // Release the capture device housekeeping   
    cvReleaseCapture( &capture );   
    cvDestroyWindow( "mywindow" );   
    return 0; 
}

它是來自OpenCV Wiki的修改后的代碼。 我知道以這種方式找到相機很瘋狂,但它不適用於-1或0。我添加了一些其他屬性(已注釋),但無論如何都無法正常工作。 在此先感謝:)克里斯,問候

我將圖像捕獲封裝到“捕獲”類中,也許您可​​以嘗試一下。 下面是main.cpp,capture.h和capture.cpp,請盡情享受:)

main.cpp

#include "capture.h"
#include <string>
using namespace std;

    int main(int argc, char* argv[]) {

        // --- read file --- //
        //Capture capture("test.wmv", windowSize, windowSize);

        // --- read camera --- //
        CvSize windowSize = cvSize(640, 480);
        Capture capture(2, windowSize, windowSize);

        while (1) {

            capture.captureNext();

            for (int i = 0; i < capture.channelNum; ++i) {

                ostringstream oss;
                oss << i;
                string winName = "WINDOW-" + (oss.str());

                cvShowImage(winName.c_str(), capture.channelframeList[i]);

            }

            int c = cvWaitKey(30);
            if ( (char) c == 27 ) { // 'Esc' to terminate

                break;
            }

        }

        return 0;

    }

捕獲

#ifndef _CAPTURE_H_
#define _CAPTURE_H_

#include <cv.h>
#include <cxcore.h>
#include <highgui.h>
#include <string>
#include <vector>
#include <iostream>
using namespace std;

enum VIDEO_TYPE {
    CAMMERA = 0,
    VIDEOFILE
};

class Capture {

public:

    Capture(int num, CvSize dispSize, CvSize resolutionSize);
    Capture(string fileName, CvSize dispSize, CvSize resolutionSize);

    int channelNum;
    vector<IplImage*> channelframeList;

    void captureNext();

    ~Capture();

private:

    string m_fileName;
    vector<CvCapture*> m_channelList;

    CvSize m_resolutioSize;
    CvSize m_displaySize;

    void initChannelList(VIDEO_TYPE type);
    void initChannelImgList(CvSize sz);
    IplImage* getNextVideoFrame(CvCapture* pCapture);


};

#endif

capture.cpp

#include "capture.h"

Capture::Capture(int num, CvSize dispSize, CvSize resolutionSize) {

    channelNum = num;
    m_fileName = "";
    m_resolutioSize = resolutionSize;

    m_channelList.resize(channelNum);
    channelframeList.resize(channelNum);

    initChannelList(CAMMERA);
    initChannelImgList(dispSize);

}

Capture::Capture(string fileName, CvSize dispSize, CvSize resolutionSize) {

    channelNum = 1;
    m_fileName = fileName;
    m_resolutioSize = resolutionSize;

    m_channelList.resize(channelNum);
    channelframeList.resize(channelNum);

    initChannelList(VIDEOFILE);
    initChannelImgList(dispSize);

}

void Capture::captureNext() {

    for (int i = 0; i < channelNum; ++i) {

        IplImage* nextFrame = getNextVideoFrame(m_channelList[i]);
        IplImage* channelFrame = channelframeList[i];
        cvResize(nextFrame, channelFrame);

    }
}

void Capture::initChannelList(VIDEO_TYPE type) {

    if (type == CAMMERA) {

        for (int i = 0; i < channelNum; ++i) {

            m_channelList[i] = cvCreateCameraCapture(i);

            //set resolution
            cvSetCaptureProperty(m_channelList[i], CV_CAP_PROP_FRAME_WIDTH, m_resolutioSize.width);
            cvSetCaptureProperty(m_channelList[i], CV_CAP_PROP_FRAME_HEIGHT, m_resolutioSize.height);

            if ( !(m_channelList[i]) ) {
                cout << "failed to initialize video capture" << endl;
                exit(EXIT_FAILURE);

            }
        }

    } else if (type == VIDEOFILE) {

        const char* fileNameChar = m_fileName.c_str();
        m_channelList[0] = cvCreateFileCapture(fileNameChar);

        if ( !(m_channelList[0]) ) {
            cout << "failed to initialize video capture" << endl;
            exit(EXIT_FAILURE);
        }

    }

}

void Capture::initChannelImgList(CvSize sz) {

    for (int i = 0; i < channelNum; ++i)
        channelframeList[i] = cvCreateImage(sz, 8, 3);

}

IplImage* Capture::getNextVideoFrame(CvCapture* pCapture) {

    IplImage* nextFrame = cvQueryFrame(pCapture);

    if (!nextFrame) {
        cout << "failed to get a video frame" << endl;
        exit(EXIT_FAILURE);
    }

    return nextFrame;

}

Capture::~Capture() {

    for (int i = 0; i < channelNum; ++i) {
        cvReleaseImage( &(channelframeList[i]) );
        cvReleaseCapture(&m_channelList[i]);
    }

}

嘗試在Windows內部將相機和Python包裝器用於OpenCV時出現灰屏。

跟蹤調試器,發現它發現了2個攝像頭接口,分別稱為“ Google Camera Adapter 0”和“ Google Camera Adapter 1”。

我可以通過以下方式修復攝像頭輸入:

  1. 轉到“添加/刪除程序”並卸載“ Google Talk插件”
  2. 拔下網絡攝像頭,然后再次將其重新插入,以便安裝新的驅動程序。

現在對我來說很好。

(請注意,我不知道步驟1是否重要,否則可能會導致其他問題中斷,因此建議您首先嘗試步驟2 ...)

那是解決問題的錯誤方法。 所以我開始:首先,隔離當前正在使用的camera_id。 哪一個? -1、0、1?

第二:請檢查返回的功能,求求你! 如果有問題,您將永遠不會知道。

CvCapture* capture = NULL;
if ((capture = cvCaptureFromCAM(-1)) == NULL)
{
    fprintf(stderr, "ERROR: capture is NULL \n"); 
    return -1;
}

cvNamedWindow("mywindow", CV_WINDOW_AUTOSIZE);

cvQueryFrame(capture); // Sometimes needed to get correct data

while (1) 
{     
    IplImage* frame = cvQueryFrame(capture); // check return
    {
        fprintf( stderr, "ERROR: cvQueryFrame failed\n");
        break;
    }

    // At this point you already have the frame! There's no need to
    // repeat the thing 10x with cvGrabFrame and cvRetrieveFrame. 
    // You are probably sabotaging yourself doing this multiple times.

    cvShowImage("mywindow", frame); // Do not release the frame!

    int key = cvWaitKey(10);
    if (key  == 0x1b)
        break;
}    

cvReleaseCapture(&capture);   
cvDestroyWindow("mywindow");   

return 0;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM