Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/opencv/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ opencv图像的平均值?_C++_Opencv_Mean - Fatal编程技术网

C++ opencv图像的平均值?

C++ opencv图像的平均值?,c++,opencv,mean,C++,Opencv,Mean,我不确定我的平均函数。在Matlab中,使用mean2,我的图像的平均值为135.3565;然而,我的函数给出了140.014,OpenCV内置的cv::mean给出了[137.67152.467115.933,0]。这是我的密码 double _mean(const cv::Mat &image) { double N = image.rows * image.cols; double mean; for (int rows = 0; rows < im

我不确定我的平均函数。在Matlab中,使用mean2,我的图像的平均值为135.3565;然而,我的函数给出了140.014,OpenCV内置的cv::mean给出了[137.67152.467115.933,0]。这是我的密码

double _mean(const cv::Mat &image)
{
    double N = image.rows * image.cols;
    double mean;

    for (int rows = 0; rows < image.rows; ++rows)
    {
        for (int cols = 0; cols < image.cols; ++cols)
        {
            mean += (float)image.at<uchar>(rows, cols);
        }
    }
    mean /= N;

    return mean;
}

我的猜测是,您正在将一种类型的图像提供给Matlab,将另一种类型的图像提供给算法和opencv内置函数。 Matlab的mean2函数采用2D图像灰度。函数假定图像也是无符号字符灰度的2D矩阵,执行此操作时:

mean += (float)image.at<uchar>(rows, cols);
这将在计算平均值之前对图像进行矢量化。比较结果。 opencv函数分别计算图像每个通道的平均值,因此结果是每个通道平均值的向量。
我希望这会有帮助

在循环开始之前,你认为平均值是多少?@OliCharlesworth,所有像素值的总和除以其总数。在循环开始之前。一个未初始化的变量。对;那么这是否回答了问题?
double _mean(const cv::Mat &image)
{
    double N = image.rows * image.cols * image.channels();
    double mean;

    for (int rows = 0; rows < image.rows; ++rows)
    {
        for (int cols = 0; cols < image.cols; ++cols)
        {
            for(int channels = 0; channels < image.channels(); ++channels)
            {
                mean += image.at<cv::Vec3b>(rows, cols)[channels];
            }
        }
    }
    mean /= N;

    return mean;
}
mean(image(:))