首页 > 解决方案 > 调整亮度和对比度 OpenCV C++

问题描述

我正在尝试调整 RGB 图像的亮度和对比度,但输出不是我所期望的。

该函数从 createTrackbar() 函数回调,其值从 0 到 100。

请检查下面的图片。我会很感激一些帮助。谢谢。

在此处输入图像描述

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;
}

标签: c++opencvbrightnesscontrast

解决方案


推荐阅读