Opencv小白--直方图显示

基于opencv2413,vs2019,x64,calcHist函数

(RGB三色)直方图

#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <iostream>
#include <stdio.h>
using namespace std;
using namespace cv;
/**
 * @function main
 */
int main(int argc, char** argv)
{
    Mat src, dst;
    /// Load image
    src = imread("E:\\IMG\\cute.jpeg");
    if (!src.data)
    {
        return -1;
    }

    /// Separate the image in 3 places ( B, G and R )
    vector<Mat> bgr_planes;
    split(src, bgr_planes);

    /// Establish the number of bins
    int histSize = 256;
    /// Set the ranges ( for B,G,R) )
    float range[] = { 0, 256 };
    const float* histRange = { range };

    bool uniform = true; bool accumulate = false;

    Mat b_hist, g_hist, r_hist;

    /// Compute the histograms:
    calcHist(&bgr_planes[0], 1, 0, Mat(), b_hist, 1, &histSize, &histRange, uniform, accumulate);
    calcHist(&bgr_planes[1], 1, 0, Mat(), g_hist, 1, &histSize, &histRange, uniform, accumulate);
    calcHist(&bgr_planes[2], 1, 0, Mat(), r_hist, 1, &histSize, &histRange, uniform, accumulate);

    // Draw the histograms for B, G and R
    int hist_w = 512; int hist_h = 400;
    int bin_w = cvRound((double)hist_w / histSize);

    Mat histImage(hist_h, hist_w, CV_8UC3, Scalar(0, 0, 0));
 
    /// Normalize the result to [ 0, histImage.rows ]
    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());
    /// Draw for each channel
    for (int i = 1; i < histSize; 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(g_hist.at<float>(i - 1))),
            Point(bin_w * (i), hist_h - cvRound(g_hist.at<float>(i))),
            Scalar(0, 255, 0), 2, 8, 0);
        line(histImage, Point(bin_w * (i - 1), hist_h - cvRound(r_hist.at<float>(i - 1))),
            Point(bin_w * (i), hist_h - cvRound(r_hist.at<float>(i))),
            Scalar(0, 0, 255), 2, 8, 0);
    }

    /// Display
    namedWindow("calcHist Demo", CV_WINDOW_AUTOSIZE);
    imshow("calcHist Demo", histImage);

    waitKey(0);

    return 0;
}

一般直方图

#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <iostream>
#include <stdio.h>
using namespace std;
using namespace cv;
class Histogram1D {
private:
	int histsize[1];

	float hranges[2];
	const float* ranges[1];
	int channels[1];
    public:
	Histogram1D() {
		histsize[0] = 256;
		hranges[0] = 0.0;
		hranges[1] = 255.0;
		ranges[0] = hranges;
		channels[0] = 0;
	}
	MatND getHistogram(const Mat& image) {
		
		cv::MatND hist;
		cv::calcHist(&image,
			1,
			channels,
			Mat(),
			hist,
			1,
			histsize,
			ranges
		);
		return hist;
	}
	cv::Mat getHistogramImage(const cv::Mat& image)
	{ 
		cv::MatND hist = getHistogram(image);
		double max = 0;
		double min = 0;
		cv::minMaxLoc(hist, &min, &max, 0, 0);
		cv::Mat histImg(histsize[0], histsize[0], CV_8U,cv:: Scalar(255));
		int hpt = static_cast<int>(0.9 * histsize[0]);
		for (int h = 0; h < histsize[0]; h++)
		{
			double  binval = hist.at<float>(h);
			int intensity = static_cast<int>(binval * hpt / max);
			cv::line(histImg, cv::Point(h, histsize[0]),
				cv::Point(h, histsize[0] - intensity),
				cv::Scalar::all(0));
		}
		return histImg;
	}
};

int main() {
	cv::Mat image = cv::imread("E:\\IMG\\VSbkg\\4.jpg");
	Histogram1D h;
	imshow("Display window", image);
	imshow("Histogram",
	h.getHistogramImage(image));
	waitKey(0);

}
发布了2 篇原创文章 · 获赞 0 · 访问量 30
App 阅读领勋章
微信扫码 下载APP
阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 游动-白 设计师: 上身试试

分享到微信朋友圈

×

扫一扫,手机浏览