2

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;
}
  • 1
    Your whole function is just: `cv::Mat3b image_output = image_input + cv::Vec3b(brightness,brightness,brightness);`. Also your `truncate` function is wrong. Also see [imadjust](https://stackoverflow.com/a/31650693/5008845) – Miki Nov 06 '20 at 11:03
  • Thanks. I figured it out. You are right tho. The truncate function is absolutely wrong. – Đinh Hồ Gia Bảo Nov 06 '20 at 11:39

0 Answers0