diff --git a/tests/examples/test-descripteurs.cpp b/tests/examples/test-descripteurs.cpp new file mode 100644 index 0000000..f18de6e --- /dev/null +++ b/tests/examples/test-descripteurs.cpp @@ -0,0 +1,38 @@ +#include + +int main(int argc, char** argv) { + std::string imagename = ""; + int seuil = 25; + int cmax = 10; + if (argc > 2) { + imagename = argv[1]; + seuil = atoi(argv[2]); + } + + cv::namedWindow("Image", CV_WINDOW_AUTOSIZE); + cv::namedWindow("Binaire", CV_WINDOW_AUTOSIZE); + cv::namedWindow("Contour", CV_WINDOW_AUTOSIZE); + + cv::Mat image = cv::imread(imagename, CV_LOAD_IMAGE_COLOR); + cv::Mat binaire(image.rows, image.cols, CV_8UC1); + cv::Mat contour_image(image.rows, image.cols, CV_8UC1); + + std::vector> contours; + std::vector> contrs; + std::vector hierarchy; + + math::filter(image, binaire, seuil); + cv::findContours(binaire, contours, hierarchy, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_NONE); + + int index = math::max_cont(contours); + contrs.push_back(contours[index]); + contrs.push_back(math::simplify_contour(contrs[0], cmax)); + cv::drawContours(contour_image, contrs, -1, 255); + + imshow("Image", image); + imshow("Binaire", binaire); + imshow("Contour", contour_image); + + cv::waitKey(0); + return 0; +} diff --git a/tests/examples/test-fft.cpp b/tests/examples/test-fft.cpp index fb759e0..0327fd4 100644 --- a/tests/examples/test-fft.cpp +++ b/tests/examples/test-fft.cpp @@ -27,6 +27,8 @@ int main(int argc, char** argv) { s.push_back(math::complex(std::sin(2*math::pi()*f0*float(i)/fe), 0)); } + std::cout << math::mean(s) << std::endl; + math::csignal tfd; clock_t begin = std::clock(); diff --git a/tests/src/math.hpp b/tests/src/math.hpp index a853c34..c2f510a 100644 --- a/tests/src/math.hpp +++ b/tests/src/math.hpp @@ -13,13 +13,34 @@ namespace math { using contour = std::vector; constexpr double pi() {return std::atan(1)*4;} + 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; + + for (int index=0,indexNB=0;indexG) && (R>B)) + if (((R-B)>=seuil) || ((R-G)>=seuil)) + detect=1; + if (detect==1) + output.data[indexNB]=255; + else + output.data[indexNB]=0; + } + } + csignal cont2sig(const contour& cont) { csignal sig; - auto sig_it = sig.begin(); - auto cont_it = cont.begin(); - - for (auto cont_it = cont.begin(); cont_it != cont.end(); ++cont_it) { - *(sig_it++) = complex((*cont_it).x, (*cont_it).y); + for (auto p: cont) { + sig.push_back(complex(p.x, p.y)); } return sig; }; @@ -35,7 +56,7 @@ namespace math { csignal diff(const csignal& input, complex mean) { csignal res; for (auto x: input) { - res.push_back(x - mean); + res.push_back(x-mean); } return res; } @@ -79,6 +100,8 @@ namespace math { int opt_size; if (N < input.size()) { opt_size = 1 << (int)std::ceil(std::log(input.size())/std::log(2)); + } else if (N==0){ + opt_size = input.size(); } else { opt_size = 1 << (int)std::ceil(std::log(N)/std::log(2)); } @@ -89,30 +112,85 @@ namespace math { return fft_rec(sig); }; - contour coef2cont(const csignal& tfd, complex mean, int size, int cmax) { - contour cont; - auto tf_it = tfd.begin(); - auto cont_it = cont.begin(); - int kmin = tfd.size()/2 - cmax; - int kmax = tfd.size()/2 + cmax; + void operator*=(csignal& sig, complex& m) { + for(auto x: sig) { + x *= m; + } + } - for (int m=0; m std::abs(desc[0])) { + std::reverse(desc.begin(), desc.end()); + } + + double phy = std::arg(desc[desc.size()-1]*desc[0])/2; + desc *= std::exp(complex(0, -phy)); + double theta = std::arg(desc[0]); + + for (int k=0; k& contours) { @@ -122,7 +200,7 @@ namespace math { if (contours[i].size() > max) { max = contours[i].size(); id = i; - } + } } return id; }; diff --git a/tests/src/traitement.cpp b/tests/src/traitement.cpp index 80ece67..65311c0 100644 --- a/tests/src/traitement.cpp +++ b/tests/src/traitement.cpp @@ -28,15 +28,14 @@ int filter(const cv::Mat& img, cv::Mat output, int seuil) { } int main(int argc, char** argv) { - int seuil=0; + int seuil=80; + int cmax = 10; + int N = 5000; if (argc > 1) { seuil = atol(argv[1]); } - int cmax = 10; - int N = 5000; - char detect; cv::VideoCapture cap(0); if(!cap.isOpened()) @@ -44,6 +43,7 @@ int main(int argc, char** argv) { cv::namedWindow("Image",1); cv::namedWindow("Detection",1); cv::namedWindow("Contours",1); + cv::namedWindow("New Contours",1); while(true) { int X, Y, DIM, index; unsigned int numc; @@ -66,15 +66,27 @@ int main(int argc, char** argv) { 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); + cv::Mat new_contour_image = cv::Mat::zeros(X,Y, CV_8UC1); - int id = math::max_cont(contours); - std::cout << contours[id].size() << std::endl; + if (contours.size() > 0) { + std::vector> contrs; - std::vector new_cont = math::simplify_contour(contours[id], cmax); + int id = math::max_cont(contours); + contrs.push_back(contours[id]); - cv::drawContours(Dessin, contours, id, 255); - cv::drawContours(Dessin, new_cont, id, 255); + std::cout << "Number of countours: " + << contours.size() + << "; Index of biggest contour: " + << id + << std::endl; + + contrs.push_back(math::simplify_contour(contrs[0], cmax)); + + cv::drawContours(Dessin, contrs, 0, 255); + cv::drawContours(new_contour_image, contrs, 1, 255); + } cv::imshow("Contours", Dessin); + cv::imshow("New Contours", new_contour_image); if(cv::waitKey(30) == 27) { break; }