Raspicam and C++: camera not repoening properly after a release() call

220 views Asked by At

I am using c++ and raspicam to take an image when certain conditions are hit in the image pixel values. When the conditions are hit, I release the camera in the c++ code, call a bash script that invokes raspistill, then reopen the camera once the bash script has completed.

Unfortunately, when I reopen the camera, the data that I retrieve from the camera is stuck in the same state that it was in my last frame before releasing the camera. I even tried jogging the camera for a few frames in my init method, but no dice. All of my computed image values are stuck on the same vaule after it works once.

Has anybody run into this issue before? I know I could work around it by having the c++ program exit when it finds what I'm looking for and wrapping it in a bash script while loop, but this is bothering me.

For reference, I am on Raspbian 8 (jessie) and using A Raspberry Pi NoIR Camera V2.

Here is my main method including the main processing loop:

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

raspicam::RaspiCam Camera = init_camera();
// Allocate memory for camera buffer
int bytes = Camera.getImageTypeSize ( raspicam::RASPICAM_FORMAT_GRAY );
unsigned char *data=new unsigned char[bytes];

unsigned int sum = 0;
int consec_pass = 0;
int finger_cnt = 0;

// main processing loop
while (true)
{
    // Capture frame
    Camera.grab();

    // Extract the image
    Camera.retrieve (data,raspicam::RASPICAM_FORMAT_IGNORE);

    int dmin = 255;
    int dmax = 0;
    int nmin = 0;
    int nmax = 0;
    for (int i = 0; i < bytes; i++)
    {
        if (data[i] < dmin)
            dmin = data[i];
        if (data[i] > dmax)
            dmax = data[i];
    }
    for (int i = 0; i < bytes; i++)
    {
        if (data[i] < dmin + 5)
            nmin++;
        if(data[i] > dmax - 5)
            nmax++;
    }

    cout << "nmin, nmax, dmin, dmax " << nmin << nmax << dmin << dmax << "\n";
    if ((nmin > NMIN_THRESH) && (nmax > NMAX_THRESH) && (dmin < MIN_THRESH) && (dmax < MAX_THRESH))
        consec_pass++;
    else
        consec_pass = 0;
    if (consec_pass > CONSEC_THRESH)
    {
        finger_cnt++;
        cout << "FOUND FINGER! " << finger_cnt << "\n";
        cout<<"Closing camera and calling bash script to take image... \n";
        Camera.release();
        std::string script_str ( "~/GetImage.sh ");
            std::string num_str(std::to_string(finger_cnt));
        system((script_str + num_str).c_str());
        cout<<"Done! Reinitializing camera";
        Camera = init_camera();
        consec_pass = 0;
    }   
}

And here is the init_camera() method referenced

raspicam::RaspiCam init_camera(){

raspicam::RaspiCam cam;
cam.setFormat(raspicam::RASPICAM_FORMAT_GRAY);
cam.setExposure(raspicam::RASPICAM_EXPOSURE_NIGHT);

// Open camera 
cout<<"Opening Camera..."<<endl;
if ( !cam.open()) {cerr<<"Error opening camera"<<endl;}

// Wait until camera stabilizes
cout<<"Sleeping for 1 secs"<<endl;
usleep(1000000);

cout<<"Jogging the camera";

int bytes = cam.getImageTypeSize ( raspicam::RASPICAM_FORMAT_GRAY );
unsigned char *data=new unsigned char[bytes];

for (int i = 0; i < 5; i++)
{
    // Capture frame
    cam.grab();

    // Extract the image
    cam.retrieve (data,raspicam::RASPICAM_FORMAT_IGNORE);
}
return cam;}

Finally here is the bash script I'm using (just a one-liner)

raspistill -w 400 -h 300 -co 75 -sh 100 -e png -roi 0.0,0.0,0.85,0.85 -t 2000 -o fingerprint$1.png 

Sorry for the messy formatting, can't get it to copy in quite right from gedit

0

There are 0 answers