码迷,mamicode.com
首页 > 其他好文 > 详细

opencv:绘制图像直方图

时间:2020-01-31 21:16:41      阅读:83      评论:0      收藏:0      [点我收藏+]

标签:could   step   win   inpu   orm   str   value   margin   printf   

#include <opencv2/opencv.hpp>
#include <iostream>

using namespace cv;
using namespace std;

int main(int argc, char** argv)
{
    Mat src = imread("f:/images/butterfly.jpg");
    if (src.empty())
    {
        printf("Could not find the image!\n");
        return -1;
    }

    namedWindow("input", WINDOW_AUTOSIZE);
    imshow("input", src);
    
    vector<Mat> mv;
    split(src, mv);

    // 计算直方图
    int histSize = 256;
    float range[] = { 0, 255 };
    const float* histRanges = { range };
    Mat b_hist, g_hist, r_hist;
    calcHist(&mv[0], 1, 0, Mat(), b_hist, 1, &histSize, &histRanges, true, false);
    calcHist(&mv[1], 1, 0, Mat(), g_hist, 1, &histSize, &histRanges, true, false);
    calcHist(&mv[2], 1, 0, Mat(), r_hist, 1, &histSize, &histRanges, true, false);

    Mat result = Mat::zeros(Size(600, 400), CV_8UC3);
    int margin = 50;
    int maxValue = result.rows - 2 * margin;
    // 归一化
    normalize(b_hist, b_hist, 0, maxValue, NORM_MINMAX, -1, Mat());
    normalize(g_hist, g_hist, 0, maxValue, NORM_MINMAX, -1, Mat());
    normalize(r_hist, r_hist, 0, maxValue, NORM_MINMAX, -1, Mat());

    float step = 500.0 / 256.0;
    // 绘制直方图
    for (int i = 0; i < 255; i++) {
        float bh1 = b_hist.at<float>(i, 0);
        float gh1 = g_hist.at<float>(i, 0);
        float rh1 = r_hist.at<float>(i, 0);

        float bh2 = b_hist.at<float>(i + 1, 0);
        float gh2 = g_hist.at<float>(i + 1, 0);
        float rh2 = r_hist.at<float>(i + 1, 0);

        line(result, 
            Point(step * i + margin, maxValue + 50 - bh1),
            Point(step * (i + 1) + margin, maxValue + 50 - bh2),
            Scalar(255, 0, 0), 1, 8, 0);
        line(result, 
            Point(step * i + margin, maxValue + 50 - gh1),
            Point(step * (i + 1) + margin, maxValue + 50 - gh2),
            Scalar(0, 255, 0), 1, 8, 0);
        line(result, 
            Point(step * i + margin, maxValue + 50 - rh1),
            Point(step * (i + 1) + margin, maxValue + 50 - rh2),
            Scalar(0, 0, 255), 1, 8, 0);
    }

    imshow("result", result);

    waitKey(0);
    destroyAllWindows();
}

opencv:绘制图像直方图

标签:could   step   win   inpu   orm   str   value   margin   printf   

原文地址:https://www.cnblogs.com/wbyixx/p/12246548.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!