Hand detection not working.
I'm using OpenCV3 with Visual Studio 2015 in C++. I cannot seem to get the program to detect my hand only. It creates a contour of my hand and the surrounding wall when I don't make it only display the largest area contour. However when I force it to find and display only the largest area contour, it completely ignores my hand. I've tried it in several background cases including a white wall with just my hand. My code is as follows:
#include "opencv2\opencv.hpp"
using namespace cv;
void on_trackbar(int, void*) {
// Dummy function
}
int main(int argv, char** argc) {
Mat frame;
Mat grayFrame;
Mat hsvFrame;
Mat thesholdFrame;
VideoCapture capture;
//Trackbar variables (H,S,V)
int H_MIN = 0;
int H_MAX = 180;
int S_MIN = 0;
int S_MAX = 255;
int V_MIN = 0;
int V_MAX = 255;
namedWindow("trackbar", 0);
//create memory to store trackbar name on window
char TrackbarName[50];
sprintf(TrackbarName, "H_MIN");
sprintf(TrackbarName, "H_MAX");
sprintf(TrackbarName, "S_MIN");
sprintf(TrackbarName, "S_MAX");
sprintf(TrackbarName, "V_MIN");
sprintf(TrackbarName, "V_MAX");
createTrackbar("H_MIN", "trackbar", &H_MIN, H_MAX, on_trackbar);
createTrackbar("H_MAX", "trackbar", &H_MAX, H_MAX, on_trackbar);
createTrackbar("S_MIN", "trackbar", &S_MIN, S_MAX, on_trackbar);
createTrackbar("S_MAX", "trackbar", &S_MAX, S_MAX, on_trackbar);
createTrackbar("V_MIN", "trackbar", &V_MIN, V_MAX, on_trackbar);
createTrackbar("V_MAX", "trackbar", &V_MAX, V_MAX, on_trackbar);
capture.open(0);
std::vector<std::vector<cv::Point> > contours;
while (true){
capture >> frame;
waitKey(10);
cvtColor(frame, hsvFrame, COLOR_BGR2HSV);
//imshow("HSV", hsvFrame);
inRange(hsvFrame, Scalar(H_MIN, S_MIN, V_MIN), Scalar(H_MAX, S_MAX, V_MAX), thesholdFrame);
int dilation_size = 3;
Mat element = getStructuringElement(MORPH_ELLIPSE, Size(2 * dilation_size + 1, 2 * dilation_size + 1), Point(dilation_size, dilation_size));
//medianBlur(hsvFrame, hsvFrame, 5);
//dilate(hsvFrame, hsvFrame, element);
findContours(thesholdFrame, contours, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE);
int largest_contour_area = 0;
int largest_contour_area_index = 1;
for (int i = 0; i < contours.size(); i++) {
double contour_area = contourArea(contours[i], false);
if (contour_area > largest_contour_area) {
largest_contour_area = contour_area;
largest_contour_area_index = i;
}
}
drawContours(frame, contours, largest_contour_area_index, (0, 255, 0), 1);
//drawContours(frame, contours, -1, (0, 255, 0), 3);
putText(frame, "NO DETECTION", Point(25, 40), 2, 1, CV_RGB(255, 255, 0), 1, 8, false);
imshow("Threshold", thesholdFrame);
//imshow("HSV", hsvFrame);
imshow("Camera", frame);
}
}
Can any of you try and run this to see if it works? Keep in mind that I am new to OpenCV.
becarefull findContour in opencv 2.4 change input image (thesholdFrame). it is not written in opencv 3 doc but try something like this :
move waitKey(10); just after imshow("Camera", frame);
Just a general comment: you can find here the general webpage for all the OpenCV version documentation. It is important as there could be minor subtle changes between versions.
Since OpenCV 3.2,
findContours
should no more modify the input image.@LBerger I tried adjusting my code to match what you have there and it is displaying the same thing.It isn't detecting my hand at all. It keeps wanting to detect random parts of the wall. Do you know any approximate HSV min and max values that I should use to detect certain skin colours? Maybe my HSV values aren't done correctly. I do remember the values that worked perfectly once but aren't working anymore. Currently I am in front of a white wall, with nothing but my hand and using values of H(0,180), S(0,220),V(190,255). Where the first value is the MIN value and the second one is the max value.
if you want to find HSV value in a rect you can build this source file : give an image as parameter select a rect using mouse and press g. Values are displayed in console
@berak like @Eduardo said in opencv 3.2 there is no problem. i have tested using this prrogram
it is always 0. May be a warning could be inserted in doc now Since opencv 3.2 Source image is not modified by this function
may be this commit changes doc
@LBerger , ah, you're right (removed the other answer)