miniprojet/tests/src/traitement.cpp

84 lines
1.8 KiB
C++
Raw Normal View History

2019-10-29 09:37:01 +00:00
#include "opencv2/opencv.hpp"
#include <math.h>
#include <algorithm>
#include "math.hpp"
2019-10-29 09:37:01 +00:00
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;
2019-10-29 09:41:19 +00:00
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=0;
if (argc > 1) {
seuil = atol(argv[1]);
}
int cmax = 10;
int N = 5000;
2019-10-29 09:41:19 +00:00
char detect;
cv::VideoCapture cap(0);
2019-10-29 09:41:19 +00:00
if(!cap.isOpened())
2019-10-29 09:37:01 +00:00
return -1;
cv::namedWindow("Image",1);
cv::namedWindow("Detection",1);
cv::namedWindow("Contours",1);
while(true) {
int X, Y, DIM, index;
2019-10-29 09:41:19 +00:00
unsigned int numc;
uchar R, G, B;
std::vector<std::vector<cv::Point>> contours;
std::vector<cv::Vec4i> hierarchy;
cv::Mat frame;
2019-10-29 09:41:19 +00:00
cap >> frame;
X=frame.rows;
Y=frame.cols;
cv::Mat binaire(X,Y,CV_8UC1);
cv::imshow("Image", frame);
cv::GaussianBlur(frame, frame, cv::Size(7,7), 1.5, 1.5);
2019-10-29 09:41:19 +00:00
X=frame.rows;
Y=frame.cols;
DIM=frame.channels();
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);
int id = math::max_cont(contours);
std::cout << contours[id].size() << std::endl;
std::vector<cv::Point> new_cont = math::simplify_contour(contours[id], cmax);
cv::drawContours(Dessin, contours, id, 255);
cv::drawContours(Dessin, new_cont, id, 255);
cv::imshow("Contours", Dessin);
if(cv::waitKey(30) == 27) {
2019-10-29 09:37:01 +00:00
break;
}
}
return 0;
}