I am trying to make a hand recognition system but when i used grayscale for cvtColor, i get debug assertion fail but when i use HSV the code works fine. Can you resolve this ? I am a newbie in opencv.
#include "stdafx.h"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/objdetect.hpp"
#include < opencv2\opencv.hpp>
#include < stdio.h>
#include <iostream>
using namespace std;
using namespace cv;
int thresh = 100;
int findBiggestContour(vector<vector<Point> > contours){
int indexOfBiggestContour = -1;
int sizeOfBiggestContour = 0;
for (int i = 0; i < contours.size(); i++){
if (contours[i].size() > sizeOfBiggestContour){
sizeOfBiggestContour = contours[i].size();
indexOfBiggestContour = i;
}
}
return indexOfBiggestContour;
}
void shifcontour(vector<Point>& contour, int x, int y)
{
for (size_t i = 0; i<contour.size(); i++)
{
contour[i].x += x;
contour[i].y += y;
}
}
int main()
{
cout << "beginning";
VideoCapture cap("pathaka.MP4");
if (!cap.isOpened()) // check if we succeeded
return -1;
Ptr<BackgroundSubtractor> pMOG2 = createBackgroundSubtractorMOG2();
for (;;)
{
Mat original, img;
cap >> img;
imshow("Source", img);
Mat hsv;
cvtColor(img, hsv, CV_BGR2GRAY);
Mat bw;
inRange(hsv, Scalar(0, 30, 80), Scalar(20, 150, 255), bw);
GaussianBlur(bw, bw, Size(7, 7), 1.5, 1.5);
Canny(bw, bw, 0, 30, 3);
vector<vector<Point> > contours;
vector<vector<Point> > convex_hull;
vector<Vec4i> hierarchy;
int erosion_type = MORPH_ELLIPSE;
int erosion_size = 0;
Mat element = getStructuringElement(erosion_type,
Size(2 * erosion_size + 1, 2 * erosion_size + 1),
Point(erosion_size, erosion_size));
dilate(bw, bw, element);
findContours(bw, contours, hierarchy, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE, Point(0, 0));
int s = findBiggestContour(contours);
Mat drawing = Mat::zeros(img.size(), CV_8UC1);
dilate(drawing, drawing, getStructuringElement(MORPH_ELLIPSE, Size(5, 5)));
dilate(drawing, drawing, getStructuringElement(MORPH_ELLIPSE, Size(5, 5)));
std::vector<cv::Point> cnt;
cnt = contours[s];
Moments M;
M = cv::moments(cnt);
cv::Point result;
result = cv::Point(M.m10 / M.m00, M.m01 / M.m00);
Point center(drawing.cols / 2, drawing.rows / 2);
cv::circle(drawing, center, 3, Scalar(255, 255, 255), -1, 8, 0);
int x;
if (result.x > center.x)
{
x = result.x - center.x;
x = -x;
}
else
{
x = result.x - center.x;
}
int y;
if (result.y < center.y)
{
y = center.y - result.y;
}
else
{
y = center.y - result.y;
}
cout << "x:" << x << endl;
cout << "y: " << y << endl;
shifcontour(contours[s], x, y);
drawContours(drawing, contours, s, Scalar(255), -1, 8, hierarchy, 0, Point());
imshow("Hsv", drawing);
if (waitKey(30) >= 0) break;
}
return 0;
}