opencv_19_图像直方图

1)void histogram_demo(Mat& image);

2)void ColorInvert::histogram_demo(Mat& image) {
    std::vector<Mat>bgr_plane;
    split(image, bgr_plane);
    const int channels[1] = { 0 };
    const int bins[1] = { 256 };
    float hranges[2] = { 0,255 };
    const float* ranges[1] = { hranges };

    Mat b_hist;
    Mat g_hist;
    Mat r_hist;

    calcHist(&bgr_plane[0], 1, 0, Mat(), b_hist, 1, bins, ranges);
    calcHist(&bgr_plane[1], 1, 0, Mat(), g_hist, 1, bins, ranges);
    calcHist(&bgr_plane[2], 1, 0, Mat(), r_hist, 1, bins, ranges);

    int hist_w = 512;
    int hist_h = 400;
    int bin_w = cvRound((double)hist_w / bins[0]);

    Mat histImage = Mat::zeros(hist_h, hist_w, CV_8UC3);

    normalize(b_hist, b_hist, 0, histImage.rows, NORM_MINMAX, -1, Mat());
    normalize(g_hist, g_hist, 0, histImage.rows, NORM_MINMAX, -1, Mat());
    normalize(r_hist, r_hist, 0, histImage.rows, NORM_MINMAX, -1, Mat());

    for (int i = 1; i < bins[0]; i++)
    {
        line(histImage, Point(bin_w * (i - 1), hist_h - cvRound(b_hist.at<float>(i - 1))), Point(bin_w * (i),hist_h-cvRound(b_hist.at<float>(i))),Scalar(255,0,0),2,8,0);
        line(histImage, Point(bin_w * (i - 1), hist_h - cvRound(b_hist.at<float>(i - 1))), Point(bin_w * (i), hist_h - cvRound(b_hist.at<float>(i))), Scalar(255, 0, 0), 2, 8, 0);
        line(histImage, Point(bin_w * (i - 1), hist_h - cvRound(b_hist.at<float>(i - 1))), Point(bin_w * (i), hist_h - cvRound(b_hist.at<float>(i))), Scalar(255, 0, 0), 2, 8, 0);
    }
    namedWindow("Histogram Demo", WINDOW_AUTOSIZE);
    imshow("Histogram Demo", histImage);
}

直方图: 

相关推荐

  1. opencv直方图

    2024-04-30 23:50:03       32 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-04-30 23:50:03       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-04-30 23:50:03       100 阅读
  3. 在Django里面运行非项目文件

    2024-04-30 23:50:03       82 阅读
  4. Python语言-面向对象

    2024-04-30 23:50:03       91 阅读

热门阅读

  1. 洛谷 P1055 [NOIP2008 普及组] ISBN 号码

    2024-04-30 23:50:03       37 阅读
  2. Git分支策略与工作流

    2024-04-30 23:50:03       40 阅读
  3. MySQL面试题:经典面试题之“B+树”

    2024-04-30 23:50:03       28 阅读
  4. Spring 如何解决 Bean 循环依赖

    2024-04-30 23:50:03       28 阅读
  5. oracle regexp_replace的用法

    2024-04-30 23:50:03       31 阅读
  6. 制作和合入git补丁

    2024-04-30 23:50:03       26 阅读
  7. Go语言中如何实现协程同步

    2024-04-30 23:50:03       30 阅读
  8. 2 Spring IoC

    2024-04-30 23:50:03       27 阅读
  9. 01 C

    2024-04-30 23:50:03       27 阅读