Skip to content

Access a Connected Camera

Andrés Solís Montero edited this page Oct 18, 2015 · 1 revision

Access a Connected Camera

You can specify the camera device id explicitly by doing the following:

int main(int argc, const char * argv[])
{
    
    auto processFrame = [](size_t frameN, Mat &input, Mat &output){
        GaussianBlur(input, output, Size(7,7), 1.5, 1.5);
        Canny(output, output, 0, 30, 3);
    };
    
    Ptr<Input> input = new CameraInput(0);
  
    Processor processor;
    processor.setInput(input);
    processor.setProcess(processFrame);
    processor.run();
    
    return 0;
}

The CameraInput class takes an int as its first argument, the device – id of the opened video capturing device (i.e. a camera index). If there is a single camera connected, just pass 0.

The setInput method sets the Processor class to use the input instance (i.e. CameraInput).

processor.setInput(input)

You can force the image size of your input by passing a second argument specifying the desired resolution. If you would like to use the device original resolution a value of Size(-1,-1) should be passed (i.e. default value Size(-1,-1)).

Ptr<Input> input = new CameraInput(0, Size(640,480));

A third argument specifies the OpenCV color space conversion code to apply to the images. By default no code conversion is applied to the images generated by the input device (i.e. default value - 1).

Ptr<Input> input = new CameraInput(0, Size(640,480),CV_RGB2GRAY);

This is useful in the case your input device generates images in a different color spectrum to the one you would like to use in your algorithm. (e.g. CV_GRAY2RGB, CV_RGB2HLS, ... etc)