我正在处理以下代码:
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <iostream>
using namespace std;
using namespace cv;
MatND detectContent(Mat image);
MatND getHistogram(Mat image);
int main()
{
Mat image;
try
{
image = imread("C:/Users/Public/Pictures/Sample Pictures/Penguins.jpg",CV_LOAD_IMAGE_GRAYSCALE);
cv::rectangle(image,Rect(670,150,60,60),Scalar(255));
if(!image.data)
{
throw 1;
}
}
catch(int a)
{
cout << "Image is unable to read" << endl;
}
//Following will show the selected location of the image for the histogram
namedWindow("Image Selected Location");
imshow("Image Selected Location",image);
//Following will display the edites image
namedWindow("Image");
imshow("Image",image);
imshow("Image",detectContent(image));
waitKey(0);
return 0;
}
MatND detectContent(Mat image)
{
//Regiion of interest
Mat imageROI = image(Rect(900,40,60,100));
//Get the histogram
MatND hist = getHistogram(imageROI);
//Normalizing the histogram
cv::normalize(hist,hist,1.0);
//Backprojecting the image
MatND result;
float *rangeArray;
rangeArray[0] = 0.0;
rangeArray[1] = 255.0;
const float *rans[1];
rans[0] = rangeArray;
int *channels[1];
channels[0] = 0;
cv::calcBackProject(&image,1,0,hist,result,rans,255.0);
return result;
}
MatND getHistogram(Mat image)
{
MatND hist;
int histSize[1];//Number of bins
float hRanges[2];//Max and Min pixel values
const float *ranges[1];
int channels[1];//Only one channel will be used
histSize[0] = 256;
hRanges[0] = 0.0;
hRanges[1] = 255.0;
ranges[0] = hRanges;
channels[0] = 0;
cv::calcHist(&image,1,channels,Mat(),hist,1,histSize,ranges);
return hist;
}
当我运行这段代码时,“反投影”图像是 100% 黑色的!另一个名为“Image Selected Location”的图像将在图像的选定位置周围绘制一个白色矩形用于直方图。我将在下面显示该图像
为什么我的背投图像是 100% 黑色的?