Détection simple des contours

This commit is contained in:
Guillaume Courrier 2019-10-29 10:41:19 +01:00
parent bf4a35f2c8
commit ea6879e9a8
3 changed files with 56 additions and 21 deletions

1
.gitignore vendored Normal file
View file

@ -0,0 +1 @@
.*.swp

View file

@ -46,15 +46,9 @@ int main(int, char**)
imshow("Detection", Binaire); imshow("Detection", Binaire);
findContours(Binaire, contours, hierarchy, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_NONE); findContours(Binaire, contours, hierarchy, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_NONE);
Mat Dessin = Mat::zeros(X,Y, CV_8UC1); Mat Dessin = Mat::zeros(X,Y, CV_8UC1);
unsigned int max = 0;
int id = 0;
for(numc = 0; numc<contours.size(); numc++) { for(numc = 0; numc<contours.size(); numc++) {
if (contours[numc].size() > max) { drawContours(Dessin, contours, id, 255);
max = contours[numc].size();
id = numc;
}
} }
drawContours(Dessin, contours, id, 255);
imshow("Contours", Dessin); imshow("Contours", Dessin);
if(waitKey(30) == 27) { if(waitKey(30) == 27) {
break; break;

View file

@ -1,24 +1,64 @@
#include "opencv2/opencv.hpp" #include "opencv2/opencv.hpp"
using namespace cv;
using namespace std;
int main(int, char**) int main(int, char**)
{ {
cv::VideoCapture cap(0); // open the default camera int seuil=80;
if(!cap.isOpened()) // check if we succeeded char detect;
VideoCapture cap(0);
if(!cap.isOpened())
return -1; return -1;
namedWindow("Image",1);
cv::Mat edges; namedWindow("Detection",1);
cv::namedWindow("edges",1); namedWindow("Contours",1);
while(1){ for(;;) {
cv::Mat frame; int X,Y,DIM,index,indexNB;
cap >> frame; // get a new frame from camera unsigned int numc;
//cv::cvtColor(frame, edges, CV_BGR2GRAY); uchar R,G,B;
//cv::GaussianBlur(edges, edges, cv::Size(7,7), 1.5, 1.5); vector<vector<Point> > contours;
//cv::Canny(edges, edges, 0, 30, 3); vector<Vec4i> hierarchy;
cv::imshow("edges", frame); Mat frame;
if(cv::waitKey(30) == 27) { 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) {
break; break;
} }
} }
// the camera will be deinitialized automatically in VideoCapture destructor
return 0; return 0;
} }