Repository: opencv/opencv
File: modules/videoio/src/cap_aravis.cpp (C++)

Code before:
{
    if(create(index)) {
        // fetch properties bounds
        arv_camera_get_width_bounds(camera, &widthMin, &widthMax, NULL);
        arv_camera_get_height_bounds(camera, &heightMin, &heightMax, NULL);
        arv_camera_set_region(camera, 0, 0, widthMax, heightMax, NULL);

        if( (fpsAvailable = arv_camera_is_frame_rate_available(camera, NULL)) )
            arv_camera_get_frame_rate_bounds(camera, &fpsMin, &fpsMax, NULL);
        if( (gainAvailable = arv_camera_is_gain_available(camera, NULL)) )
            arv_camera_get_gain_bounds (camera, &gainMin, &gainMax, NULL);
        if( (exposureAvailable = arv_camera_is_exposure_time_available(camera, NULL)) )
            arv_camera_get_exposure_time_bounds (camera, &exposureMin, &exposureMax, NULL);

        // get initial values
        pixelFormat = arv_camera_get_pixel_format(camera, NULL);
        
        // If camera's pixel format is not one of the supported formats, set a default
        if (pixelFormat != ARV_PIXEL_FORMAT_MONO_8 &&
            pixelFormat != ARV_PIXEL_FORMAT_BAYER_GR_8 &&
            pixelFormat != ARV_PIXEL_FORMAT_MONO_12 &&
            pixelFormat != ARV_PIXEL_FORMAT_MONO_16) {
            pixelFormat = ARV_PIXEL_FORMAT_MONO_8;
            arv_camera_set_pixel_format(camera, pixelFormat, NULL);
        }
        
        exposure = exposureAvailable ? arv_camera_get_exposure_time(camera, NULL) : 0;
        gain = gainAvailable ? arv_camera_get_gain(camera, NULL) : 0;
        fps = arv_camera_get_frame_rate(camera, NULL);
        softwareTriggered = (strcmp(arv_camera_get_trigger_source(camera, NULL), "Software") == 0);

        return startCapture();
    }
    return false;
}

bool CvCaptureCAM_Aravis::grabFrame()
{
    // remove content of previous frame
    framebuffer = NULL;

    if(stream) {
        ArvBuffer *arv_buffer = NULL;
        int max_tries = 10;
        int tries = 0;
        if (softwareTriggered && allowAutoTrigger) {
            arv_camera_software_trigger (camera, NULL);
        }
        for(; tries < max_tries; tries ++) {
            arv_buffer = arv_stream_timeout_pop_buffer (stream, 200000);
            if (arv_buffer != NULL && arv_buffer_get_status (arv_buffer) != ARV_BUFFER_STATUS_SUCCESS) {

Review comment (suggestion):
When setting the default pixel format to `ARV_PIXEL_FORMAT_MONO_8`, the `targetGrey` variable should also be set to 128. This variable is used in the auto-exposure control algorithm (see line 374 in `autoExposureControl`). Looking at lines 545-548, when `CAP_PROP_FOURCC` is set to `MODE_GREY` or `MODE_Y800` (both result in `ARV_PIXEL_FORMAT_MONO_8`), `targetGrey` is set to 128. The same should be done here for consistency.

Add this line after setting the pixel format:
```cpp
targetGrey = 128;
```
```suggestion
            arv_camera_set_pixel_format(camera, pixelFormat, NULL);
            targetGrey = 128;
```

Code after:

bool CvCaptureCAM_Aravis::open( int index )
{
    if(create(index)) {
        // fetch properties bounds
        arv_camera_get_width_bounds(camera, &widthMin, &widthMax, NULL);
        arv_camera_get_height_bounds(camera, &heightMin, &heightMax, NULL);
        arv_camera_set_region(camera, 0, 0, widthMax, heightMax, NULL);

        if( (fpsAvailable = arv_camera_is_frame_rate_available(camera, NULL)) )
            arv_camera_get_frame_rate_bounds(camera, &fpsMin, &fpsMax, NULL);
        if( (gainAvailable = arv_camera_is_gain_available(camera, NULL)) )
            arv_camera_get_gain_bounds (camera, &gainMin, &gainMax, NULL);
        if( (exposureAvailable = arv_camera_is_exposure_time_available(camera, NULL)) )
            arv_camera_get_exposure_time_bounds (camera, &exposureMin, &exposureMax, NULL);

        // get initial values
        pixelFormat = arv_camera_get_pixel_format(camera, NULL);

        // If camera's pixel format is not one of the supported formats, set a default
        if (pixelFormat != ARV_PIXEL_FORMAT_MONO_8 &&
            pixelFormat != ARV_PIXEL_FORMAT_BAYER_GR_8 &&
            pixelFormat != ARV_PIXEL_FORMAT_MONO_12 &&
            pixelFormat != ARV_PIXEL_FORMAT_MONO_16) {
            pixelFormat = ARV_PIXEL_FORMAT_MONO_8;
            arv_camera_set_pixel_format(camera, pixelFormat, NULL);
            CV_LOG_WARNING(NULL, "Current camera pixel format is not supported. Failed back to MONO_8.");
        }

        midGrey = getExpectedMidGrey(pixelFormat);

        exposure = exposureAvailable ? arv_camera_get_exposure_time(camera, NULL) : 0;
        gain = gainAvailable ? arv_camera_get_gain(camera, NULL) : 0;
        fps = arv_camera_get_frame_rate(camera, NULL);
        softwareTriggered = (strcmp(arv_camera_get_trigger_source(camera, NULL), "Software") == 0);

        return startCapture();
    }
    return false;
}

bool CvCaptureCAM_Aravis::grabFrame()
{
    // remove content of previous frame
    framebuffer = NULL;

    if(stream) {
        ArvBuffer *arv_buffer = NULL;
        int max_tries = 10;
        int tries = 0;
        if (softwareTriggered && allowAutoTrigger) {