Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

OpenCV Backprojection inaccurate

I'm following the official openCV tutorial (can't post link or image, newbie!) to identify a hand in the scene however my code is giving very poor results. Does anyone know why I'm identifying the background more than finger tips, which are relatively similar values of H & S? My code so far is:

Mat imageHSV, roi;
int bins = 25;
cvtColor(image, imageHSV, CV_BGR2HSV);
roi = imageHSV(cv::Rect((image.cols/2)-10, (image.rows/2)-10, 20, 20));
//Process our input (some code adapted from OpenCV backprojection article
hue.create(imageHSV.size(), imageHSV.depth());
int channels[] = {0,0};
mixChannels(&imageHSV, 1, &hue, 1, channels, 1);
MatND backproj;
int histSize = MAX(bins,2);
float hue_range[] = {0,180};
const float* ranges = {hue_range};
calcHist(&hue, 1, 0, Mat(), roi, 1, &histSize, &ranges, true, false);
normalize(roi, roi, 0, 255, NORM_MINMAX, -1, Mat());
calcBackProject(&hue, 1, 0, roi, backproj, &ranges, 1, true);
image = backproj;

Here is an example screenshot:

OpenCV Backprojection inaccurate

I'm following the official openCV tutorial (can't post link or image, newbie!) to identify a hand in the scene however my code is giving very poor results. Does anyone know why I'm identifying the background more than finger tips, which are relatively similar values of H & S? My code so far is:

Mat imageHSV, roi;
int bins = 25;
cvtColor(image, imageHSV, CV_BGR2HSV);
roi = imageHSV(cv::Rect((image.cols/2)-10, (image.rows/2)-10, 20, 20));
//Process our input (some code adapted from OpenCV backprojection article
hue.create(imageHSV.size(), imageHSV.depth());
int channels[] = {0,0};
mixChannels(&imageHSV, 1, &hue, 1, channels, 1);
MatND backproj;
int histSize = MAX(bins,2);
float hue_range[] = {0,180};
const float* ranges = {hue_range};
calcHist(&hue, 1, 0, Mat(), roi, 1, &histSize, &ranges, true, false);
normalize(roi, roi, 0, 255, NORM_MINMAX, -1, Mat());
calcBackProject(&hue, 1, 0, roi, backproj, &ranges, 1, true);
image = backproj;

Here is an example screenshot: