2013-07-01 68 views
0

這可能是一個愚蠢的問題,但我真的無法弄清楚。首先:對於模糊的標題感到抱歉,我不確定如何用幾個字來描述我的問題。OpenCV VideoCapture閱讀問題

我在MS Visual Studio,C++中使用OpenCV 2.4.3。我正在使用VideoCapture接口從我的筆記本電腦攝像頭捕獲幀。

什麼我的計劃應該做的是:

環路用戶的不同姿勢,每個姿勢:

  • 等待用戶是在位置(getchar函數()爲輸入等待直至說,「我在位置」,只需按下回車鍵)
  • 讀取當前幀
  • 從該幀
  • 提取intrest的區域保存圖像中的ROI,然後貼上標籤

下面是代碼:

int main() { 

Mat img, face_img, img_start; 
Rect *face; 
VideoCapture cam(0); 
ofstream fout("dataset/dataset.txt"); 

if(!fout) { 
    cout<<"Cannot open dataset file! Aborting"<<endl; 
    return 1; 
} 

int count = 0; // Number of the (last + 1) image in the dataset 

// Orientations are: 0°, +/- 30°, +/- 60°, +/-90° 
// Distances are just two, for now 
// So it is 7x2 images 

cam.read(img_start); 
IplImage image = img_start; 
face = face_detector(image); 


if(!face) { 
    cout<<"No face detected..? Aborting."<<endl; 
    return 2; 
} 

// Double ROI dimensions 
face->x = face->x-face->width/2; 
face->y = face->y-face->height/2; 
face->width *= 2; 
face->height *=2; 

for(unsigned i=0;i<14;++i) { 

    // Wait for the user to get in position 
    getchar(); 

    // Get the face ROI 
    cam.read(img); 

    face_img = Mat(img, *face); 

    // Save it 
    stringstream sstm; 
    string fname; 
    sstm << "dataset/image" << (count+i) << ".jpeg"; 
    fname = sstm.str(); 
    imwrite(fname,face_img); 
    //do some other things.. 

我從中得到期望:

  • 我站在鏡頭前當程序啓動時,它使用face_detector得到ROI矩形()函數
  • 當我準備好了,說在pose0中,我打回車,並拍了一張照片
  • 從那張圖片的子圖像是e xtracted並保存爲image0.jpeg
  • 循環這7次

作用:

  • 我站在鏡頭前的節目開始時,這裏沒有什麼特別
  • 我按回車鍵
  • ROI不是從當時拍攝的照片中提取的,而是從第一個中提取的

起初,我在每個cam.capture()中都使用了img,然後我改變了cam.capture(img_start)中的第一個,但是沒有幫助。 我的代碼的第二次迭代保存應該保存在第一次,第三次迭代中應該保存在第二次的圖像,等等。

我可能會錯過來自VideoCapture的重要內容,但我真的無法弄清楚,所以在這裏。

感謝您的幫助,我真的很感激。

回答

1

您的實施問題是相機不能自由運行並實時捕捉圖像。當您啓動攝像機時,視頻捕獲緩衝區已滿並等待您讀取幀。一旦緩衝區滿了,它不會丟棄舊幀,直到您讀取並釋放空間。

解決方法是除了「進程」線程外,還有一個單獨的捕獲線程。每當新幀進入並將其存儲在「最近幀」圖像對象中時,捕捉線程就會從緩衝區中讀取幀。當進程線程需要最近的幀時(即當你按Enter鍵時),它會鎖定一個互斥體以保證線程安全,將最近的幀複製到另一個對象中,並釋放互斥體,以便捕獲線程繼續在新幀中讀取數據。

+0

我會盡力,謝謝! – powder

0
#include <iostream> 
#include <stdio.h> 
#include <thread> 
#include <mutex> 
#include <opencv2/objdetect/objdetect.hpp> 
#include <opencv2/highgui/highgui.hpp> 
#include <opencv2/imgproc/imgproc.hpp> 
#include <iostream> 

using namespace std; 
using namespace cv; 

void camCapture(VideoCapture cap, Mat* frame, bool* Capture){ 
    while (*Capture==true) { 
     cap >> *frame; 
    } 
    cout << "camCapture finished\n"; 
    return; 
} 

int main() { 
    VideoCapture cap(0); // open the default camera 
    if (!cap.isOpened()) // check if we succeeded 
     return -1; 
    Mat *frame, SFI, Input; 
    frame = new Mat; 
    bool *Capture = new bool; 
    *Capture = true; 
    //your capture thread has started 
    thread captureThread(camCapture, cap, frame, Capture); 
    mtx.lock(); 
    imshow(*frame,current_frame); 
    mtx.unlock(); 
    //Terminate the thread 
    mtx.lock(); 
    *Capture = false; 
    mtx.unlock(); 
    captureThread.join(); 
    return 0; 
} 

這是我從上述建議中編寫的代碼。我希望有人能從中得到幫助。