我试图获得许多图像的对比度值。
现在,我编写了代码,获得1张图像的对比度值,但出现错误。

#include "opencv2/highgui/highgui.hpp"
#include <iostream>


using namespace cv;

using namespace std;

int main(int argc, const char** argv)

{

Mat img = imread("MyPic.JPG", CV_LOAD_IMAGE_GRAYSCALE); //open and read the image


if (img.empty())
{
    cout << "Image cannot be loaded..!!" << endl;
    return -1;
}

MatIterator_<uchar> itx, endx, ity, endy, it, end;
Mat src, dstx, dsty;
double sum1 = 0;
for (itx = dstx.begin<uchar>(), endx = dstx.end<uchar>(),
    ity = dsty.begin<uchar>(), endy = dsty.end<uchar>(),
    it = img.begin<uchar>(), end = img.end<uchar>(); itx != endx; ++itx, ++ity, ++it)
{
    sum1 = (int)(sqrt((*itx)*(*itx) + (*ity)*(*ity)));
    cout << "sum1" << sum1;
    //printf("%d", *it);
}




//show the image
imshow("Original Image", img);

waitKey(0); //wait for key press

destroyAllWindows(); //destroy all open windows

return 0;
}

错误是:

我是openCV的新手,所以我无法解决它。

更新

我这样写:
long double sum,sum0,sum1,sum2;
const int channels = img.channels();
switch (channels){
case 1:       // for grayscale image
{
    MatIterator_<uchar> it, end;
    for (it = img.begin<uchar>(), end = img.end<uchar>(); it != end; ++it)
        //dosomething(*it);
        sum += *it;
    // here I want to add all pixels so at the end of loop I will have addition of all pixels. Now I will find max pixel sum of all test images which will tell me best picture.
    // Best picture will be that image which have highest sum.

    break;
}
case 3:          // for color image
{
    MatIterator_<Vec3b> it, end;
    for (it = img.begin<Vec3b>(), end = img.end<Vec3b>(); it != end; ++it)
    {
        //dosomething((*it)[0]);
        sum0 += ((*it)[0]);
        //dosomething((*it)[0]);
        sum1 += ((*it)[1]);
        //dosomething((*it)[2]);
        sum2 += ((*it)[2]);
    }
}
}

但是它仍然无法正常工作。

最佳答案

您应该删除此部分

itx = dstx.begin<uchar>(), endx = dstx.end<uchar>(),
ity = dsty.begin<uchar>(), endy = dsty.end<uchar>(),

因为dstxdsty为空,所以不能断言它们的元素大小等于uchar
或将它们构造为uchar矩阵
Mat dstx(img.size(), CV_8U);

更新
const int channels = I.channels();
switch(channels){
  case 1:       // for grayscale image
    {
        MatIterator_<uchar> it, end;
        for( it = I.begin<uchar>(), end = I.end<uchar>(); it != end; ++it)
            dosomething(*it);
        break;
    }
  case 3:          // for color image
    {
        MatIterator_<Vec3b> it, end;
        for( it = I.begin<Vec3b>(), end = I.end<Vec3b>(); it != end; ++it)
        {
            dosomething((*it)[0]);
            dosomething((*it)[0]);
            dosomething((*it)[2]);
        }
    }
}

更新2 总和不会返回 double 值,而是一个标量,它是3通道图像的3 double 值,因此请像auto sum1, sum_tot;这样定义它
max = 0;
maxIdx;
for (i=0; i<n; i++) {
    sum1 = sum(img[i]);
    sum_tot = sum1[0] + sum1[1] + sum1[2];
    if (sum_tot > max) {
        max = sum_tot;
        maxIdx = i;
    }
}

但是我告诉你这不是强度,为了对比,你应该做这样的事情
contrast = (max(img) - min(img))/(max(img) - min(img));

要么
size1 = img.size();
contrast = (max(img) - min(img))/(sum(img)/size1[0]/size1[1]);

关于c++ - 在opencv和C++中获取图像的对比度值;,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/30169463/

10-09 14:54