请看下面的代码
#include <iostream>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
using namespace std;
using namespace cv;
Mat image,image_clone,testImage,testImageHSV,imageHSV,testImageHue,imageHue;
int ch[] = {0,0};
int bins=10;
char *windowName = "BackProjection";
void histAndBackProjection(int,void*);
int main()
{
//Load the image
image = imread("E:/Trip/DSC01894.jpg");
image_clone ;
image.copyTo(image_clone);
//Break a small piece from the image
testImage = image(Rect(450,470,20,20));
cv::rectangle(image_clone,Point(450,470),Point(500,500),Scalar(255,0,0),2);
//Transfor to HSV
cvtColor(testImage,testImageHSV,CV_BGR2HSV);
cvtColor(image,imageHSV,CV_BGR2HSV);
//Seperate the HUE channel
testImageHue.create(testImageHSV.size(),testImageHSV.depth());
imageHue.create(imageHSV.size(),imageHSV.depth());
cv::mixChannels(&testImageHSV,1,&testImageHue,1,ch,1);
cv::mixChannels(&imageHSV,1,&imageHue,1,ch,1);
namedWindow("Image");
imshow("Image",image_clone);
namedWindow("Test Image");
imshow("Test Image",testImage);
//Creating windows for trackbar
namedWindow(windowName);
//Creating the track bar
createTrackbar("Select Bins",windowName,&bins,255,histAndBackProjection);
histAndBackProjection(0,0);
waitKey(0);
return 0;
}
void histAndBackProjection(int, void *)
{
MatND histImage,backProjection;
int histSize = MAX(bins,2);
float range[]={0,180};
const float* rangePtr={range};
//Calculate the histogram
cv::calcHist(&testImageHue,1,0,Mat(),histImage,1,&histSize,&rangePtr,true,false);
cv::normalize(histImage,histImage,1.0);
//Do the backprojection
cv::calcBackProject(&imageHue,1,0,histImage,backProjection,&rangePtr,1,true);
imshow(windowName,backProjection);
}
我正在使用此代码进行反投影。但是,我的反投影输出是 100% 黑色!无论我将滑块移动到哪里,它仍然是黑色的!在我的图像中,我的目标是反向投影一个 100% 白色的位置,因为我需要找到白色区域。
为什么是这样?代码有问题吗?