Mayank
Mayank

Reputation: 45

Get contrast values of images in opencv and C++;

I am trying to get contrast value of many images. Right now, I wrote code for, getting contrast value of 1 image but I am getting the error.

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

error is:

enter image description here

I am new to openCV so I am not able to solve it.

Update

I wrote it like this:

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

But it is still not working.

Upvotes: 2

Views: 2550

Answers (1)

Azad
Azad

Reputation: 1120

You should remove this part

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

because dstx and dsty are empty it can not assert that their element size is equal to uchar

or construct them as uchar matrix

Mat dstx(img.size(), CV_8U);

update

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

update 2 sum does not return a double but a Scalar which is 3 double for 3 channel image so define it like this 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;
    }
}

but I told you this is intensity not contrast for contrast you should do something like this

contrast = (max(img) - min(img))/(max(img) - min(img));

or

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

Upvotes: 2

Related Questions