From ea6879e9a888608d1d51739dd254955858e46e88 Mon Sep 17 00:00:00 2001 From: Guillaume Courrier Date: Tue, 29 Oct 2019 10:41:19 +0100 Subject: [PATCH] =?UTF-8?q?D=C3=A9tection=20simple=20des=20contours?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 1 + opencv/detection.cpp | 8 +----- src/traitement.cpp | 68 +++++++++++++++++++++++++++++++++++--------- 3 files changed, 56 insertions(+), 21 deletions(-) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..a01ee28 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.*.swp diff --git a/opencv/detection.cpp b/opencv/detection.cpp index 2ce495b..1d63fe4 100644 --- a/opencv/detection.cpp +++ b/opencv/detection.cpp @@ -46,15 +46,9 @@ int main(int, char**) 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 max) { - max = contours[numc].size(); - id = numc; - } + drawContours(Dessin, contours, id, 255); } - drawContours(Dessin, contours, id, 255); imshow("Contours", Dessin); if(waitKey(30) == 27) { break; diff --git a/src/traitement.cpp b/src/traitement.cpp index 6756258..2ce495b 100644 --- a/src/traitement.cpp +++ b/src/traitement.cpp @@ -1,24 +1,64 @@ #include "opencv2/opencv.hpp" +using namespace cv; +using namespace std; + int main(int, char**) { - cv::VideoCapture cap(0); // open the default camera - if(!cap.isOpened()) // check if we succeeded + int seuil=80; + char detect; + VideoCapture cap(0); + if(!cap.isOpened()) return -1; - - cv::Mat edges; - cv::namedWindow("edges",1); - while(1){ - cv::Mat frame; - cap >> frame; // get a new frame from camera - //cv::cvtColor(frame, edges, CV_BGR2GRAY); - //cv::GaussianBlur(edges, edges, cv::Size(7,7), 1.5, 1.5); - //cv::Canny(edges, edges, 0, 30, 3); - cv::imshow("edges", frame); - if(cv::waitKey(30) == 27) { + namedWindow("Image",1); + namedWindow("Detection",1); + namedWindow("Contours",1); + for(;;) { + int X,Y,DIM,index,indexNB; + unsigned int numc; + uchar R,G,B; + vector > contours; + vector 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;indexG) && (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 max) { + max = contours[numc].size(); + id = numc; + } + } + drawContours(Dessin, contours, id, 255); + imshow("Contours", Dessin); + if(waitKey(30) == 27) { break; } } - // the camera will be deinitialized automatically in VideoCapture destructor return 0; }