Question

Here is a link to the type of images my program produces: http://imgur.com/a/vibBx#0

I am trying out a simple capture test program that I have written. I am trying to capture images in a loop and save them onto the board properly numbered. The first capture sometimes is corrupted and the subsequent captures are a mix of two images. I have also observed that sometimes the upper half of the image is from the previous capture and the lower half is the capture from that cycle. I have given the details and the code below.

OpenCV 2.4.2 running on BeagleBone Black which has Ångström installed on it. The camera which is plugged to the USB of BeagleBone Black is Logitech C920. The camera is connected to BeagleBone Black before power up through the 5 V power supply and connecting BeagleBone Black to the laptop. Access is through PuTTY.

Code

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <iostream>
#include <string>
#include <unistd.h>
#include <opencv/cv.h>
#include <opencv/highgui.h>
#include <cxcore.h>
#include "LaserWeed.h"
#include "SimpleGPIO.h"

using namespace cv;

int main(int argc, char* argv[])
{
    int i=0, a;
    string name;
    int length;
    char c, Filename[10000];
    CvCapture* capture = NULL;
    IplImage* img = NULL;

    do
    {
        //I am not sure if this is necessary, but I tried anyway
        //to see if it makes a difference since upper half of
        //image was from previous cycle.
        capture = NULL;
        img = NULL;

        //Converting Numbers to string to save with proper name
        std::stringstream ss;
        ss << i;
        name = ss.str();
        name = name + ".jpg";
        length = name.size();
        for(a=0; a<length; a++)
        {
            Filename[a] = name[a];
        }

        capture = cvCaptureFromCAM(-1);
        if (!capture)
        {
            fprintf( stderr, "ERROR: capture is NULL \n" );
            getchar();
            return -1;
        }
        img = cvQueryFrame(capture);
        if (!img)
        {
            fprintf(stderr, "ERROR: img is null...\n");
            getchar();
            return -1;
        }

        cvSaveImage(Filename,img);
        cvReleaseCapture(&capture);
        cvReleaseImage(&img);

        i++;
        c = getchar();
    }
    while (c!='e')
        ;
    return 0;
}

Where might I be going wrong?

The Stack Overflow question BeagleBone, OpenCV and webcam issue somewhat has a similar problem. But reinstallation of the OS will be my last option.

Was it helpful?

Solution

This is weird, anyway try to take the capture out of the do-while loop, maybe opening and releasing the capture device each time won't give enough time for the camera to prepare it's image buffers.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top