Séparation du code: ajout de la fonction filter qui choisit les pixels qui respectent les critères RGB.

This commit is contained in:
Guillaume Courrier 2019-10-29 16:58:26 +01:00
parent edffb137b7
commit 25bb7a8ea7

View file

@ -1,51 +1,65 @@
#include "opencv2/opencv.hpp"
using namespace cv;
using namespace std;
int filter(const cv::Mat& img, cv::Mat output, int seuil) {
bool detect = false;
uchar R, G, B;
int rows = img.rows;
int cols = img.cols;
int dim = img.channels();
int indexNB;
int main(int, char**)
{
for (int index=0,indexNB=0;index<dim*rows*cols;index+=dim,indexNB++) {
detect=0;
B = img.data[index ];
G = img.data[index + 1];
R = img.data[index + 2];
if ((R>G) && (R>B))
if (((R-B)>=seuil) || ((R-G)>=seuil))
detect=1;
if (detect==1)
output.data[indexNB]=255;
else
output.data[indexNB]=0;
}
}
int main(int argc, char** argv) {
int seuil=80;
if (argc > 1) {
seuil = atol(argv[1]);
}
char detect;
VideoCapture cap(0);
cv::VideoCapture cap(0);
if(!cap.isOpened())
return -1;
namedWindow("Image",1);
namedWindow("Detection",1);
namedWindow("Contours",1);
for(;;) {
int X,Y,DIM,index,indexNB;
cv::namedWindow("Image",1);
cv::namedWindow("Detection",1);
cv::namedWindow("Contours",1);
while(true) {
int X,Y,DIM,index;
unsigned int numc;
uchar R,G,B;
vector<vector<Point> > contours;
vector<Vec4i> hierarchy;
Mat frame;
std::vector<std::vector<cv::Point> > contours;
std::vector<cv::Vec4i> hierarchy;
cv::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);
cv::Mat binaire(X,Y,CV_8UC1);
cv::imshow("Image", frame);
cv::GaussianBlur(frame, frame, cv::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);
filter(frame, binaire, seuil);
cv::imshow("Detection", binaire);
cv::findContours(binaire, contours, hierarchy, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_NONE);
cv::Mat Dessin = cv::Mat::zeros(X,Y, CV_8UC1);
unsigned int max = 0;
int id = 0;
for(numc = 0; numc<contours.size(); numc++) {
@ -54,9 +68,9 @@ int main(int, char**)
id = numc;
}
}
drawContours(Dessin, contours, id, 255);
imshow("Contours", Dessin);
if(waitKey(30) == 27) {
cv::drawContours(Dessin, contours, id, 255);
cv::imshow("Contours", Dessin);
if(cv::waitKey(30) == 27) {
break;
}
}