Adjust brightness and contrast OpenCV C++

Viewed 269

I am trying to adjust the brightness and contrast of an RGB image but the output is not what I expect.

The function is callbacked from createTrackbar() function with values from 0 to 100.

Please check the image below. I would appreciate some help. Thanks.

enter image description here

void brightness_callback(int brightness, void *userdata)
{
  int height = image_input.rows, width = image_input.cols;
  image_output = Mat::zeros(image_input.size(), image_input.type());

  int widthStep = image_input.step;
  int nChannels = 3;

  uchar *pDataInput = (uchar *)image_input.data;
  uchar *pDataOutput = (uchar *)image_output.data;

  for (int x = 0; x < height; x++, pDataInput += widthStep, pDataOutput += widthStep) {
    uchar *pRowInput = pDataInput;
    uchar *pRowOutput = pDataOutput;
    for (int y = 0; y < width; y++, pRowInput += nChannels, pRowOutput += nChannels) {
      uchar B = pRowInput[0];
      uchar G = pRowInput[1];
      uchar R = pRowInput[2];

      pRowOutput[0] = truncate((uchar)(B + brightness));
            pRowOutput[1] = truncate((uchar)(G + brightness));
            pRowOutput[2] = truncate((uchar)(R + brightness));
    }
  }

    imshow(window_original, image_output);
}


uchar truncate(uchar value) {
  if (value < 0) return 0;
  else if (value > 255) return 255;

  return value;
}
0 Answers
Related