我是OpenCV的新手并且学习绘制单个通道图像的直方图。当我运行程序时,我只得到一个白线。请告诉我这段代码有什么问题。
int histSize[1] = {256};
float hranges[2] = {0.0, 255.0};
const float* ranges[1] = {hranges};
int channels[1] = {0};
cv::MatND getHistogram(const cv::Mat &image) {
cv::MatND hist;
cv::calcHist(&image,1,channels,cv::Mat(),hist,1,histSize,ranges);
return hist;
}
cv::Mat getHistogramImage(const cv::Mat &image) {
cv::MatND hist = getHistogram(image);
double minVal = 0;
double maxVal = 0;
cv::minMaxLoc(hist,&minVal,&maxVal,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<256; ++h){
float binVal = hist.at<float>(h);
int intensity = static_cast<int>(binVal*hpt/maxVal);
cv::line(histImg,cv::Point(h,histSize[0]),
cv::Point(h,histSize[0]-intensity),
cv::Scalar::all(0));
}
return histImg;
}
答案 0 :(得分:1)
对于单通道(也是来自色调通道的那个),可以使用以下代码:
{
Mat input("filepath/image.jpg", 1);
Mat hsv_input;
cvtColor( input, hsv_input, CV_BGR2HSV );
int h_bins = 5;
int histSize[] = { h_bins};
float h_ranges[] = { 0, 180 };
const float* ranges[] = { h_ranges};
int channels[] = { 0 };
MatND original_hist_input;
calcHist( &hsv_input, 1, channels, Mat(), original_hist_input, 1, histSize, ranges, true, false );
normalize( original_hist_input, original_hist_input, 0, 1, NORM_MINMAX, -1, Mat() );
}
答案 1 :(得分:0)
#include <opencv2/opencv.hpp>
#include <iostream>
using namespace cv;
using namespace std;
class Histogram1D
{
public:
int hitsize[1];
float hrange[2];
const float *ranges[1];
int channel[1];
Histogram1D()
{
hitsize[0]=256;
hrange[0]=0.0;
hrange[1]=256.0;
ranges[0]=hrange;
channel[0]=0;
}
MatND gethistogram(Mat &image)
{
MatND hist;
calcHist(&image,1,channel,Mat(),hist,1,hitsize,ranges);
return hist;
}
};
int main()
{
Mat image = imread("1.jpg",0);
Histogram1D h;
double minval=0;
double maxval=0;
MatND result= h.gethistogram(image);
for (int i=0;i<256;i++)
{
cout << "value" << i << "=" << result.at<float>(i)<< endl;
}
minMaxLoc(result,&minval,&maxval,0,0);
Mat histImg(h.hitsize[0],h.hitsize[0],CV_8U,Scalar(255));
int hpt=static_cast<int>(0.9*h.hitsize[0]);
for(int i=0;i<h.hitsize[0];i++)
{
float binval=result.at<float>(i);
int intensity=static_cast<int>(binval*hpt/maxval);
line(histImg,Point(i,h.hitsize[0]),Point (i,h.hitsize[0]-intensity),Scalar::all(0));
}
namedWindow("Histogram");
imshow("Histogram", histImg);
waitKey(0);
}
答案 2 :(得分:0)
您已在第二行声明了色调通道的错误范围
float hranges[2] = {0.0, 255.0};
这是错误的,色调通道范围是0到180.因此,由于色调通道范围是0到180,因此如果你采用少于/或等于180个箱子即“histSize”将是好的。 / p>