Enabling/disabling auto exposure and auto white balance

Image sensor has built-in auto exposure and auto white balance. In this recipe, we are going to show you how we can change the image sensor's settings to change the active state of these features.

Getting ready

Create a project in Visual Studio 2010 and prepare it for working with OpenNI using the Creating a project in Visual Studio 2010 recipe of Chapter 2, OpenNI and C++.

Then, copy the code from the Reading and showing a frame from the image sensor (color/IR) recipe of Chapter 3, Using Low-level Data, to this project.

How to do it...

  1. Locate the following lines of code:
    void gl_KeyboardCallback(unsigned char key, int x, int y)
    {
  2. Replace any code inside this function with the following code snippet:
      if (key == 27) // ESC Key
      {
        selectedSensor.destroy();
        device.close();
        OpenNI::shutdown();
        exit(0);
      }
      else if (key == 'C' || key == 'c')
      {
        if (device.isValid())
        {
          printf("
    -->Setting active sensor to COLOR
    ");
          SetActiveSensor(SENSOR_COLOR, &device);
        }
      }
      else if (key == 'I' || key == 'i')
      {
        if (device.isValid())
        {
          printf("
    -->Setting active sensor to IR
    ");
          SetActiveSensor(SENSOR_IR, &device);
        }
      }
      else if (key == 'E' || key == 'e') // E or e key
      {
        if (selectedSensor.isValid() &&
          selectedSensor.getSensorInfo().getSensorType() ==
            SENSOR_COLOR)
        {
          CameraSettings cs =
            *(selectedSensor.getCameraSettings());
          cs.setAutoExposureEnabled(
            !cs.getAutoExposureEnabled());
          printf("Auto Exposure %s
    ", 
            (cs.getAutoExposureEnabled() ?
            "Activated" : "Deactivated"));
        }
      }
      else if (key == 'W' || key == 'w') // W or w key
      {
        if (selectedSensor.isValid() &&
          selectedSensor.getSensorInfo().getSensorType() ==
            SENSOR_COLOR)
        {
          CameraSettings cs =
            *(selectedSensor.getCameraSettings());
          cs.setAutoWhiteBalanceEnabled(
            !cs.getAutoWhiteBalanceEnabled());
          printf("Auto White Balance %s
    ", 
            (cs.getAutoWhiteBalanceEnabled() ?
            "Activated" : "Deactivated"));
        }
      }
  3. Then locate the following lines of code:
    int _tmain(int argc, _TCHAR* argv[])
    {
  4. Add the preceding lines of code at the end of this function:
    printf("Press E to toggle exposure or W to toggle white balance");

How it works...

As you can see, there are no big changes in this recipe when compared to the Reading and showing a frame from the image sensor (color/IR) recipe of Chapter 3, Using Low-level Data, which we used as the base for this project. The only function that needs to be changed is gl_KeyboardCallback() that is responsible for the keyboard's key press events.

We alter this function to add two more functionalities to it: one is for toggling the camera's auto exposure when the E or e key is pressed and the other is for toggling the camera's auto white balance when the W or w key is pressed.

The first part of enabling and disabling the camera's auto exposure starts with a condition to see if selectedSensor is valid, and if it is a color stream (because this feature is only available for color sensors). If both the conditions were correct, we need to get the current status of the camera's auto exposure and then set it to a negative value. But both of these operations cannot be done directly with the openni::VideoStream object. We need to have openni::CameraSettings of our openni::VideoStream to retrieve and change this feature. This can be done with the openni::VideoStream.getCameraSettings() method:

      CameraSettings cs =
        *(selectedSensor.getCameraSettings());

Now that we have access to openni::CameraSettings, we can toggle this feature using the openni::CameraSettings::getAutoExposureEnabled() and openni::CameraSettings::setAutoExposureEnabled() methods:

      cs.setAutoWhiteBalanceEnabled(
        !cs.getAutoWhiteBalanceEnabled());

Then we print the new state of the camera's auto exposure feature:

      printf("Auto Exposure %s
", 
        (cs.getAutoExposureEnabled() ?
        "Activated" : "Deactivated"));

We do almost the same thing for toggling the auto white balance feature too, checking if everything is valid and the selected sensor is the color sensor, and retrieving openni::CameraSettings of our openni::VideoStream. Then by using the openni::CameraSettings::getAutoWhiteBalanceEnabled() and openni::CameraSettings::setAutoWhiteBalanceEnabled() methods, we get and change the active state of this feature:

    if (selectedSensor.isValid() &&
      selectedSensor.getSensorInfo().getSensorType() ==
        SENSOR_COLOR)
    {
      CameraSettings cs =
        *(selectedSensor.getCameraSettings());
      cs.setAutoWhiteBalanceEnabled(
        !cs.getAutoWhiteBalanceEnabled());
      printf("Auto White Balance %s
", 
        (cs.getAutoWhiteBalanceEnabled() ?
        "Activated" : "Deactivated"));
    }

Some screenshots to compare the difference between the enabled and disabled states of these two camera settings are as follows:

How it works...

There's more...

Starting with OpenNI 2.2, it is possible to set Gain and Exposure values manually. A list of related methods to do so is as follows:

  • openni::CameraSettings::getExposure(): This method returns the current value of Exposure in int.
  • openni::CameraSettings::setExposure(): This method sets a new value for Exposure. It accepts an argument of the type int and returns an openni::Status indicating the success of the operation.
  • openni::CameraSettings::getGain(): This method returns the current value of Gain in int.
  • openni::CameraSettings::setGain(): This method sets a new value for Gain. It accepts an argument of the type int and returns an openni::Status indicating the success of the operation.

See also

  • The Reading and showing a frame from the image Sensor (color / IR) recipe in Chapter 3, Using Low-level Data
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.137.184.102