opencv实现灰度图像的直方图点计算以及灰度直方图显示
2016-07-11 15:56
666 查看
#include<opencv2/core/core.hpp>
#include<opencv2/highgui/highgui.hpp>
#include<opencv2/opencv.hpp>
#include"iostream"
using namespace std;
using namespace cv;
class Histogram1D
{
private:
int histSize[1];//the number of
float hranges[2];//the maximum and min of the pixel
const float*ranges[1];
int channels[1];//only use one channel
public:
Histogram1D(){
// the parementer of the 1D histogram
histSize[0]=256;
hranges[0]=0.0;
hranges[1]=255.0;
ranges[0]=hranges;
channels[0]=0;//the normal
}
//calculate the distribution of the 1d
cv::MatND getHistogram(const cv::Mat &image)
{
cv::MatND hist;
//calculate the histogram
cv::calcHist(&image,
1,//calculate the single image's histogram
channels,//the number of channels
cv::Mat(),// not use the image as mask;
hist,//return hist
1,//one-di
histSize,//the number of
ranges//the range of pixel
);
return hist;
}
cv::Mat getHistogramImage(const cv::Mat &image)
{
//first ,calculate the histogram
cv::MatND hist = getHistogram(image);
//receive the max and min number
double maxVal = 0;
double minVal = 0;
cv::minMaxLoc(hist,&minVal,&maxVal,0,0);
//imshow the image of histogram
cv::Mat histImg(histSize[0],histSize[0],
CV_8U,cv::Scalar(255));
//set up the highest point is the 90 precent of the nbins
int hpt = static_cast<int>(0.9*histSize[0]);
// every level,draw a vertical line
for (int h =0;h<histSize[0];h++)
{
float binVal = hist.at<float>(h);
int intensity = static_cast<int>(binVal*hpt/maxVal);
//draw a line with two point
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("D:\\ben3.jpg");
// the histogram
Histogram1D h;
//calculate hist
cv::MatND histo=h.getHistogram(image);
//scan the every level
for(int i=0;i<256;i++)
cout<<"Value"<<i<<"="<<
histo.at<float>(i)<<endl;
cv::namedWindow("Histogram");
cv::imshow("Histogram",
h.getHistogramImage(image));
cv::Mat thresholded;
cv::threshold(image,thresholded,80,255,cv::THRESH_BINARY);
cv::namedWindow("binary image");
cv::imshow("binary image",thresholded);
cv::waitKey(0);
system("pause");
return 0;
}
#include<opencv2/highgui/highgui.hpp>
#include<opencv2/opencv.hpp>
#include"iostream"
using namespace std;
using namespace cv;
class Histogram1D
{
private:
int histSize[1];//the number of
float hranges[2];//the maximum and min of the pixel
const float*ranges[1];
int channels[1];//only use one channel
public:
Histogram1D(){
// the parementer of the 1D histogram
histSize[0]=256;
hranges[0]=0.0;
hranges[1]=255.0;
ranges[0]=hranges;
channels[0]=0;//the normal
}
//calculate the distribution of the 1d
cv::MatND getHistogram(const cv::Mat &image)
{
cv::MatND hist;
//calculate the histogram
cv::calcHist(&image,
1,//calculate the single image's histogram
channels,//the number of channels
cv::Mat(),// not use the image as mask;
hist,//return hist
1,//one-di
histSize,//the number of
ranges//the range of pixel
);
return hist;
}
cv::Mat getHistogramImage(const cv::Mat &image)
{
//first ,calculate the histogram
cv::MatND hist = getHistogram(image);
//receive the max and min number
double maxVal = 0;
double minVal = 0;
cv::minMaxLoc(hist,&minVal,&maxVal,0,0);
//imshow the image of histogram
cv::Mat histImg(histSize[0],histSize[0],
CV_8U,cv::Scalar(255));
//set up the highest point is the 90 precent of the nbins
int hpt = static_cast<int>(0.9*histSize[0]);
// every level,draw a vertical line
for (int h =0;h<histSize[0];h++)
{
float binVal = hist.at<float>(h);
int intensity = static_cast<int>(binVal*hpt/maxVal);
//draw a line with two point
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("D:\\ben3.jpg");
// the histogram
Histogram1D h;
//calculate hist
cv::MatND histo=h.getHistogram(image);
//scan the every level
for(int i=0;i<256;i++)
cout<<"Value"<<i<<"="<<
histo.at<float>(i)<<endl;
cv::namedWindow("Histogram");
cv::imshow("Histogram",
h.getHistogramImage(image));
cv::Mat thresholded;
cv::threshold(image,thresholded,80,255,cv::THRESH_BINARY);
cv::namedWindow("binary image");
cv::imshow("binary image",thresholded);
cv::waitKey(0);
system("pause");
return 0;
}
相关文章推荐
- PHP GD 图像处理组件的常用函数总结
- PHP图像处理之imagecreate、imagedestroy函数介绍
- jsvascript图像处理―(计算机视觉应用)图像金字塔
- Javascript图像处理思路及实现代码
- python中使用OpenCV进行人脸检测的例子
- opencv 做人脸识别 opencv 人脸匹配分析
- 使用opencv拉伸图像扩大分辨率示例
- PHP图像处理之使用imagecolorallocate()函数设置颜色例子
- java数字图像处理基础使用imageio写图像文件示例
- 使用Java进行图像处理的一些基础操作
- javascript图像处理―边缘梯度计算函数
- Javascript图像处理―阈值函数实例应用
- Javascript图像处理―虚拟边缘介绍及使用方法
- Android Studio中配置OpenCV库开发环境的教程
- 基于C++实现kinect+opencv 获取深度及彩色数据
- visual studio 2012安装配置方法图文教程 附opencv配置教程
- OpenCV 2.4.3 C++ 平滑处理分析
- PHP图像处理类库及演示分享
- CI框架文件上传类及图像处理类用法分析
- php图像处理函数大全(推荐收藏)