Join the Stack Overflow Community
Stack Overflow is a community of 6.6 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

This will probably be a dumb question, but i really can't figure it out. First of all: sorry for the vague title, i'm not really sure about how to describe my problem in a couple of words.

I'm using OpenCV 2.4.3 in MS Visual Studio, C++. I'm using the VideoCapture interface for capturing frames from my laptop webcam.

What my program should do is:

Loop on different poses of the user, for each pose:

  • wait that the user is in position (a getchar() waits for an input that says "i'm in position" by simply hitting enter)
  • read the current frame
  • extract a region of intrest from that frame
  • save the image in the ROI and then label it

Here is the code:

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..

What i expect from it:

  • i stand in front of the camera when the program starts and it gets the ROI rectangle using the face_detector() function
  • when i'm ready, say in pose0, i hit enter and a picture is taken
  • from that picture a subimage is extracted and it is saved as image0.jpeg
  • loop this 7 times

What it does:

  • i stand in front of the camera when the program starts, nothing special here
  • i hit enter
  • the ROI is extracted not from the picture taken in that moment, but from the first one

At first, i used img in every cam.capture(), then i changed the first one in cam.capture(img_start) but that didn't help. The second iteration of my code saves the image that should have been saved in the 1st, the 3rd iteration the one that should have been saved in the 2nd and so on.

I'm probably missing someting important from the VideoCapture, but i really can't figure it out, so here i am.

Thanks for any help, i really appreciate it.

share|improve this question

The problem with your implementation is that the camera is not running freely and capturing images in real time. When you start up the camera, the videocapture buffer is filled up while waiting for you to read in the frames. Once the buffer is full, it doesn't drop old frames for new ones until you read and free up space in it.

The solution would be to have a separate capture thread, in addition to your "process" thread. The capture thread keeps reading in frames from the buffer whenever a new frame comes in and stores it in a "recent frame" image object. When the process thread needs the most recent frame (i.e. when you hit Enter), it locks a mutex for thread safety, copies the most recent frame into another object and frees the mutex so that the capture thread continues reading in new frames.

share|improve this answer
    
I'll try that, thanks! – powder Jul 2 '13 at 10:25
#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;
}

This is the code that I wrote from the above advice. I hope someone can get help from this.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.