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.