I am new to OpenCV and learning to draw the histogram of a single channel image. When I run the program, I only get a white line. Please tell me what's wrong with this code.
我是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;
}
3 个解决方案
#1
1
For single channel (and thats too from hue channel), the following code can be used:
对于单个通道(这也来自于顺化通道),可以使用以下代码:
{
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() );
}
#2
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);
}
#3
0
you have declared the wrong range for hue channel in your second line
您在第二行中声明了色调通道的错误范围
float hranges[2] = {0.0, 255.0};
It is wrong, the hue-channel range is 0 to 180. So, as the hue-channel range is 0 to 180 therefore it would be good if you take less/or equal to 180 bins i.e. "histSize".
错误的是,hue-channel的范围是0到180。因此,由于颜色通道的范围是0到180,所以如果你取更小的/或等于180个箱子就好了。“histSize”。
#1
1
For single channel (and thats too from hue channel), the following code can be used:
对于单个通道(这也来自于顺化通道),可以使用以下代码:
{
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() );
}
#2
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);
}
#3
0
you have declared the wrong range for hue channel in your second line
您在第二行中声明了色调通道的错误范围
float hranges[2] = {0.0, 255.0};
It is wrong, the hue-channel range is 0 to 180. So, as the hue-channel range is 0 to 180 therefore it would be good if you take less/or equal to 180 bins i.e. "histSize".
错误的是,hue-channel的范围是0到180。因此,由于颜色通道的范围是0到180,所以如果你取更小的/或等于180个箱子就好了。“histSize”。