获取图像中每个像素的对比度值

时间:2019-04-06 10:08:18

标签: c++ opencv

我想获得图像的平均对比度值,并且在处理中找到了有效的代码。我试图像在c ++中一样在qt-creator中应用此代码,但是当我尝试运行代码时,出现此错误:

  

在初始化时无法将“ cv :: Mat”转换为“ uint8_t * {aka unsigned char *}”            uint8_t * rowPtr = foo.row(i); << / p>

在这些部分中:

  

uint8_t * rowPtr = foo.row(i);>

这是我要运行的代码;

    bool normalizeRange = true;    // normalize results to range of 0-1
    float contrast = 0;
    float brightness =0;
    Mat foo = imread("1.png");

    int cn = foo.channels();

    Scalar_<uint8_t> bgrPixel;


    for(int i = 0; i < foo.rows; i++)
    {

        uint8_t* rowPtr = foo.row(i);
        for(int j = 0; j < foo.cols; j++)
        {
            bgrPixel.val[0] = rowPtr[j*cn + 0]; // B
            bgrPixel.val[1] = rowPtr[j*cn + 1]; // G
            bgrPixel.val[2] = rowPtr[j*cn + 2]; // R

            // do something with BGR values...
        }
    }
    brightness += (0.2126 * bgrPixel.val[2]) + (0.7152 * bgrPixel.val[1]) + (0.0722 * bgrPixel.val[0]);                 // scales RGB to perceived brightness
    if (normalizeRange) {
      brightness /= 255.0;                                                    // normalize to 0-1
    }


  brightness /= sizeof(bgrPixel);                                                // average result
  qDebug() << "Average brightness:" << brightness;

  // find contrast by comparing average brightness with current value
  for(int i = 0; i < foo.rows; i++)
  {
      uint8_t* rowPtr = foo.row(i);
      for(int j = 0; j < foo.cols; j++)
      {
          bgrPixel.val[0] = rowPtr[j*cn + 0]; // B
          bgrPixel.val[1] = rowPtr[j*cn + 1]; // G
          bgrPixel.val[2] = rowPtr[j*cn + 2]; // R

          // do something with BGR values...
      }
  }
      float pxIntensity = (0.2126 * bgrPixel.val[2]) + (0.7152 * bgrPixel.val[1]) + (0.0722 * bgrPixel.val[2]);
    if (normalizeRange) {
         pxIntensity /= 255.0;                                                // normalizes to range 0-1
    }
    contrast += pow((brightness - pxIntensity), 2);
    contrast /= sizeof(bgrPixel);
     qDebug() << "Average cotrast:" << contrast;
  }

更新:我添加了我想在C ++中运行的Java代码

boolean normalizeRange = true;    // normalize results to range of 0-1
PImage img;
float brightness = 0;
float contrast = 0;
void setup() {
    img = loadImage(filename);
  size(img.width, img.height);
  image(img, 0,0);
  loadPixels();                   // load pixels into array, iterate!
    // find average brightness across image
  for (color c : pixels) {
    float r = c >> 16 & 0xFF;                                                 // extract RGB values quickly (better than red(), etc)
    float g = c >> 8 & 0xFF;
    float b = c & 0xFF;
    brightness += (0.2126 * r) + (0.7152 * g) + (0.0722 * b);                 // scales RGB to perceived brightness
    if (normalizeRange) {
      brightness /= 255.0;                                                    // normalize to 0-1
    }  }
  brightness /= pixels.length;                                                // average result
  println("Average brightness: " + brightness);
  // find contrast by comparing average brightness with current value
  for (color c : pixels) {
    float r = c >> 16 & 0xFF;
    float g = c >> 8 & 0xFF;
    float b = c & 0xFF;
    float pxIntensity = (0.2126 * r) + (0.7152 * g) + (0.0722 * b);
    if (normalizeRange) {
         pxIntensity /= 255.0;                                                // normalizes to range 0-1  }
    contrast += pow((brightness - pxIntensity), 2);}
  contrast /= pixels.length;
  println("Average contrast:   " + contrast);
}

1 个答案:

答案 0 :(得分:0)

使用cv::Mat::ptr()获取指向指定矩阵行缓冲区的指针。

cv::Mat::row()为子矩阵构造一个新的cv::Mat标头,该标头由该单行组成。

迭代原始cv::Mat缓冲区可能是效率更高但可读性较低的元素迭代方式。了解不同的方式及其比较here