opencv直方图显示

Mat calcHistImage(Mat Image)
{
    const int channels[1] = { 0 };
    const int histSize[1] = { 256 };
    float hranges[2] = { 0, 255 };
    const float* ranges[1] = { hranges };
    Mat hist;
    calcHist(&Image, 1, channels, Mat(), hist, 1, histSize, ranges);
    double maxVal = 0;
    double minVal = 0;

    //找到直方图中的最大值和最小值
    minMaxLoc(hist, &minVal, &maxVal, 0, 0);
    int histHeight = hist.rows;
    Mat histImg(histHeight, 256, CV_8U, Scalar(255));
    // 设置最大峰值为图像高度的90%
    int hpt = static_cast<int>(0.9*histHeight);

    for (int h = 0; h < histHeight; h++)
    {
        int intensity = static_cast<int>(hist.at<float>(h)*hpt / maxVal);
        line(histImg, Point(h, histHeight), Point(h, histHeight - intensity), Scalar::all(0));
    }
    return histImg;
}

你可能感兴趣的:(opencv)