2019-10-29 09:37:01 +00:00
|
|
|
#include "opencv2/opencv.hpp"
|
|
|
|
|
2019-10-29 09:41:19 +00:00
|
|
|
using namespace cv;
|
|
|
|
using namespace std;
|
|
|
|
|
2019-10-29 09:37:01 +00:00
|
|
|
int main(int, char**)
|
|
|
|
{
|
2019-10-29 09:41:19 +00:00
|
|
|
int seuil=80;
|
|
|
|
char detect;
|
|
|
|
VideoCapture cap(0);
|
|
|
|
if(!cap.isOpened())
|
2019-10-29 09:37:01 +00:00
|
|
|
return -1;
|
2019-10-29 09:41:19 +00:00
|
|
|
namedWindow("Image",1);
|
|
|
|
namedWindow("Detection",1);
|
|
|
|
namedWindow("Contours",1);
|
|
|
|
for(;;) {
|
|
|
|
int X,Y,DIM,index,indexNB;
|
|
|
|
unsigned int numc;
|
|
|
|
uchar R,G,B;
|
|
|
|
vector<vector<Point> > contours;
|
|
|
|
vector<Vec4i> hierarchy;
|
|
|
|
Mat frame;
|
|
|
|
cap >> frame;
|
|
|
|
X=frame.rows;
|
|
|
|
Y=frame.cols;
|
|
|
|
Mat Binaire(X,Y,CV_8UC1);
|
|
|
|
imshow("Image", frame);
|
|
|
|
GaussianBlur(frame, frame, Size(7,7), 1.5, 1.5);
|
|
|
|
X=frame.rows;
|
|
|
|
Y=frame.cols;
|
|
|
|
DIM=frame.channels();
|
|
|
|
for (index=0,indexNB=0;index<DIM*X*Y;index+=DIM,indexNB++)
|
|
|
|
{
|
|
|
|
detect=0;
|
|
|
|
B=frame.data[index ];
|
|
|
|
G=frame.data[index + 1];
|
|
|
|
R=frame.data[index + 2];
|
|
|
|
if ((R>G) && (R>B))
|
|
|
|
if (((R-B)>=seuil) || ((R-G)>=seuil))
|
|
|
|
detect=1;
|
|
|
|
if (detect==1)
|
|
|
|
Binaire.data[indexNB]=255;
|
|
|
|
else
|
|
|
|
Binaire.data[indexNB]=0;
|
|
|
|
}
|
|
|
|
imshow("Detection", Binaire);
|
|
|
|
findContours(Binaire, contours, hierarchy, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_NONE);
|
|
|
|
Mat Dessin = Mat::zeros(X,Y, CV_8UC1);
|
|
|
|
unsigned int max = 0;
|
|
|
|
int id = 0;
|
|
|
|
for(numc = 0; numc<contours.size(); numc++) {
|
|
|
|
if (contours[numc].size() > max) {
|
|
|
|
max = contours[numc].size();
|
|
|
|
id = numc;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
drawContours(Dessin, contours, id, 255);
|
|
|
|
imshow("Contours", Dessin);
|
|
|
|
if(waitKey(30) == 27) {
|
2019-10-29 09:37:01 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|