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

图像的直方图

时间:2019-09-18 11:42:14      阅读:102      评论:0      收藏:0      [点我收藏+]

标签:float   har   load   mat   mic   namespace   clu   ges   channel   

技术图片

 

 技术图片

 

 技术图片

 

 技术图片

 

 技术图片

 

 技术图片

 

 代码:

 

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

using namespace std;
using namespace cv;

int main(int argc, char** argv) {
    Mat src = imread("L:/4.jpg");
    if (!src.data) {
        printf("could not load image...\n");
        return -1;
    }
    char INPUT_T[] = "input image";
    char OUTPUT_T[] = "histogram demo";
    namedWindow(INPUT_T, CV_WINDOW_AUTOSIZE);
    namedWindow(OUTPUT_T, CV_WINDOW_AUTOSIZE);
    imshow(INPUT_T, src);

    // 分通道显示
    vector<Mat> bgr_planes;
    split(src, bgr_planes);              //把多通道图像分为多个单通道图像
    //imshow("single channel demo", bgr_planes[0]);

    // 计算直方图 
    int histSize = 256;                      //直方图的级数
    float range[] = { 0, 256 };              //直方图的范围
    const float *histRanges = { range };     //范围指针 
    Mat b_hist, g_hist, r_hist;
    calcHist(&bgr_planes[0], 1, 0, Mat(), b_hist, 1, &histSize, &histRanges, true, false);
    calcHist(&bgr_planes[1], 1, 0, Mat(), g_hist, 1, &histSize, &histRanges, true, false);
    calcHist(&bgr_planes[2], 1, 0, Mat(), r_hist, 1, &histSize, &histRanges, true, false);

    // 归一化
    int hist_h = 400;
    int hist_w = 512;
    int bin_w = hist_w / histSize;
    Mat histImage(hist_w, hist_h, CV_8UC3, Scalar(0, 0, 0));
    normalize(b_hist, b_hist, 0, hist_h, NORM_MINMAX, -1, Mat());
    normalize(g_hist, g_hist, 0, hist_h, NORM_MINMAX, -1, Mat());
    normalize(r_hist, r_hist, 0, hist_h, NORM_MINMAX, -1, Mat());

    // render histogram chart
    for (int i = 1; i < histSize; i++) {
        line(histImage, Point((i - 1)*bin_w, hist_h - cvRound(b_hist.at<float>(i - 1))),
            Point((i)*bin_w, hist_h - cvRound(b_hist.at<float>(i))), Scalar(255, 0, 0), 2, LINE_AA);

        line(histImage, Point((i - 1)*bin_w, hist_h - cvRound(g_hist.at<float>(i - 1))),
            Point((i)*bin_w, hist_h - cvRound(g_hist.at<float>(i))), Scalar(0, 255, 0), 2, LINE_AA);

        line(histImage, Point((i - 1)*bin_w, hist_h - cvRound(r_hist.at<float>(i - 1))),
            Point((i)*bin_w, hist_h - cvRound(r_hist.at<float>(i))), Scalar(0, 0, 255), 2, LINE_AA);
    }
    imshow(OUTPUT_T, histImage);

    waitKey(0);
    return 0;
}

 

结果:

技术图片

 

图像的直方图

标签:float   har   load   mat   mic   namespace   clu   ges   channel   

原文地址:https://www.cnblogs.com/Jack-Elvis/p/11528217.html

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