gpt4 book ai didi

c++ - 在opencv和C++中获取图像的对比度值;

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:18:22 24 4
gpt4 key购买 nike

我正在尝试获取许 multimap 像的对比度值。现在,我编写了用于获取 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;
}

错误是:

enter image description here

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

update 2 sum 不返回 double,而是返回一个 Scalar,对于 3 channel 图像,它是 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com