OpenCV تابع خاصی نداره به راحتی خودتون می تونید رسمش کنید.
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
using namespace cv;
void get1CHHist(InputArray _src, InputOutputArray &hist, int size, InputArray _mask = noArray()){
int hist_size[] = { size };
float ranges1[] = { 0, float(size - 1) };
const float* ranges[] = { ranges1 };
int channels[] = { 0 };
Mat src = _src.getMat();
calcHist(&src, 1, channels, _mask, hist, 1, hist_size, ranges);
}
void draw1CHHist(InputArray _hist,InputOutputArray _dst, int size,const Scalar& color ){
Mat hist = _hist.getMat();
Size dst_size(hist.rows, size);
Mat dst;
if (_dst.size() != dst_size){
_dst.create(dst_size, CV_8UC3);
dst = _dst.getMat();
dst.setTo(0);
}
else dst = _dst.getMat();
double min_value, max_value;
minMaxIdx(hist, &min_value, &max_value);
Mat norm_hist;
normalize(hist, norm_hist, 0, size, CV_MINMAX);
for (int j = 0; j < hist.rows; j++)
cv::line(dst, Point(j, dst.rows - 1), Point(j, dst.rows - 1 - norm_hist.at<float>(j)), color);
}
using BhHistogram = vector<Mat>;
BhHistogram getHist(InputArray _src, int size = 256, InputArray _mask = noArray()){
BhHistogram hist;
Mat src = _src.getMat();
if (src.channels() == 3){
vector<Mat> channels;
split(_src, channels);
hist.reserve(channels.size());
for (auto& channel : channels){
Mat cur_hist;
get1CHHist(channel, cur_hist, size, _mask);
hist.push_back(cur_hist);
}
}
else {
Mat cur_hist;
get1CHHist(_src, cur_hist, size, _mask);
hist.push_back(cur_hist);
}
return hist;
}
void drawHist(const BhHistogram& hist, vector<Mat>& _dst, int size, const Scalar& color){
for (size_t i = 0; i < hist.size(); i++)
draw1CHHist(hist[i], _dst[i], size, color);
}
void showHist(const string& title, const BhHistogram& hist, int size, const Scalar& color){
}
void showHist(const string& title, const BhHistogram& hist,int size = 200){
Scalar single_color = CV_RGB(255, 255, 255);
vector<Scalar> triple_colors = { CV_RGB(255, 0, 0), CV_RGB(0, 255, 0), CV_RGB(0, 0, 255) };
if (hist.size() == 1){
Mat dst;
draw1CHHist(hist[0], dst, size, single_color);
imshow(title, dst);
}
else {
for (size_t i = 0; i < hist.size(); i++){
Mat dst;
draw1CHHist(hist[i], dst, size, triple_colors[i]);
string str = title + std::to_string(i);
imshow(str, dst);
}
}
}
int _tmain(int argc, _TCHAR* argv[]){
Mat src, dst;
src = imread("d:/kart.jpg", 1);
auto hist = getHist(src);
showHist("hist", hist);
waitKey(0);
}