opencv实现灰度图像的直方图点计算以及灰度直方图显示
来源:互联网 发布:浙江国税网络发票系统 编辑:程序博客网 时间:2024/05/17 17:59
#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;
}
0 0
- opencv实现灰度图像的直方图点计算以及灰度直方图显示
- OpenCv 灰度直方图显示
- OpenCV图像处理 图像的点运算 ( 灰度直方图 )
- 灰度图像的直方图
- 灰度图像的直方图
- 图像的灰度直方图
- 图像的灰度直方图
- 用opencv创建图像的灰度直方图
- Opencv中图像的灰度直方图
- Android Jni OpenCV-灰度图像的直方图
- 灰度直方图的显示
- 计算灰度图像的归一化直方图
- opencv获取灰度图像直方图
- 显示一张图像的灰度直方图RGB
- OpenCV实现灰度直方图和直方图拉伸
- 图像处理opencv2-灰度直方图以及灰度直方图均衡化
- matlab求灰度图像的灰度直方图
- java实现图像的直方图均衡以及灰度线性变化,灰度拉伸
- poi根据导入的excel数据自动封装成bean对象集合
- 极品飞车(并查集+枚举)
- Linux C系统编程:信号与定时器的使用
- shell学习笔记
- Visual Studio 2013 、Matlab 2014产品密钥
- opencv实现灰度图像的直方图点计算以及灰度直方图显示
- Runtime.getRuntime().exec()出现阻塞,导致后续程序无法运行!
- Storm集群安装部署步骤【详细版】
- Rabbitmq 学习笔记(四)HA
- MySQL数据库SYS CPU高的可能性分析
- 我的Android进阶之旅------>关于使用Android Studio替换App的launcher图标之后仍然显示默认的ic_launcher图标的解决方法
- rxJava------>看我72变
- eclipse下git的配置和使用
- Error creating context 'spring.root': InputStream is null from Resource