這可能是一個愚蠢的問題,但我真的無法弄清楚。首先:對於模糊的標題感到抱歉,我不確定如何用幾個字來描述我的問題。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的重要內容,但我真的無法弄清楚,所以在這裏。
感謝您的幫助,我真的很感激。
我會盡力,謝謝! – powder