图像纹理复杂度计算

定义图像的灰度级直方图的二阶矩(方差)描述纹理复杂度:这里写图片描述

其中z表示灰度,p(zi)为相应的直方图,其中L是不同灰度级的数量。m是z的均值:
这里写图片描述

C++实现如下:

    cv::Mat img = cv::imread("test.bmp");
    int height = img.rows;
    int width = img.cols;
    double mean = 0.0;
    double variance = 0.0;
    int temp = 0;

    for (int y = 0; y < height; y++)
    {
        for (int x = 0; x < width; x++)
        {
            mean += img.at(y, x)[0];
        }
    }
    mean /= double(height*width);

    //variance(texture complexity纹理复杂度)
    for (int y = 0; y < height; y++)
    {
        for (int x = 0; x < width; x++)
        {
            temp = img.at(y, x)[0] - mean;
            variance += (temp*temp);
        }
    }
    variance /= double(height*width);
    variance = int(variance / 100);
    std::cout << "variance:" << variance;

你可能感兴趣的:(Opencv,DIP)