Gamma Correction

This tutorial demonstrates how to capture a 2D image with a configurable gamma correction.

First, we connect to the camera.

Go to source

source

auto camera = zivid.connectCamera();

Then we get the gamma as command line argument from user.

Go to source

source

const double gamma = readGamma(argc, argv);
std::cout << "Capturing with gamma correction: " << gamma << std::endl;

We then capture the image with no gamma correction (gamma = 1.0) and with given gamma correction (in this case, gamma = 0.6).

Go to source

source

cv::Mat bgrOriginal = captureBGRImage(camera, 1.0);
cv::Mat bgrAdjusted = captureBGRImage(camera, gamma);

The complete implementation of the function to capture a color image is given below.

Go to source

source

cv::Mat captureBGRImage(Zivid::Camera &camera, const double gamma)
{
    std::cout << "Configuring settings" << std::endl;
    const auto settings2D = Zivid::Settings2D{ Zivid::Settings2D::Acquisitions{ Zivid::Settings2D::Acquisition{} },
                                               Zivid::Settings2D::Processing::Color::Gamma{ gamma } };

    std::cout << "Capturing 2D frame" << std::endl;
    const auto frame2D = camera.capture(settings2D);
    const auto image = frame2D.imageRGBA();
    auto bgr = imageToBGR(image);

    return bgr;
}

For comparing the images with and without gamma correction, a combined image is created.

Go to source

source

cv::Mat combineImages(const cv::Mat &imageOne, const cv::Mat &imageTwo)
{
    cv::Mat combinedImage;
    int height = imageOne.rows;
    int width = imageOne.cols;
    cv::hconcat(
        imageOne(cv::Range(0, height), cv::Range(0, width / 2)),
        imageTwo(cv::Range(0, height), cv::Range(width / 2, width)),
        combinedImage);

    return combinedImage;
}

The images can then be displayed as one image.

Go to source

source

void displayBGR(const cv::Mat &bgr, const std::string &bgrName)
{
    cv::namedWindow(bgrName, cv::WINDOW_NORMAL);
    cv::imshow(bgrName, bgr);
    std::cout << "Press any key to continue" << std::endl;
    cv::waitKey(0);
}

The resulting combined image is shown below, with the original half on the left side and the gamma corrected half on the right side.

Go to source

source

std::cout << "Displaying color image before and after gamma correction: " << gamma << std::endl;
cv::Mat combinedImage = combineImages(bgrOriginal, bgrAdjusted);
displayBGR(combinedImage, "Original on left, adjusted on right");
Gamma correction: original on left, adjusted on right