diff --git a/tests/examples/test-descripteurs.cpp b/tests/examples/test-descripteurs.cpp index f18de6e..c5a70f2 100644 --- a/tests/examples/test-descripteurs.cpp +++ b/tests/examples/test-descripteurs.cpp @@ -4,34 +4,53 @@ int main(int argc, char** argv) { std::string imagename = ""; int seuil = 25; int cmax = 10; - if (argc > 2) { + + if (argc > 1) { imagename = argv[1]; + } else { + std::cout << "Invalid number of arguments: test-descripteurs []"; + return 0; + } + + if (argc > 2) { seuil = atoi(argv[2]); } cv::namedWindow("Image", CV_WINDOW_AUTOSIZE); cv::namedWindow("Binaire", CV_WINDOW_AUTOSIZE); cv::namedWindow("Contour", CV_WINDOW_AUTOSIZE); + cv::namedWindow("New 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); + cv::Mat new_contour_image(image.rows, image.cols, CV_8UC1); - std::vector> contours; - std::vector> contrs; + 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); + + math::contour c = contours[index]; + c = math::simplify_contour(c, cmax); + std::array bounds = math::bounds(c); + c = transform(c, bounds); + contrs.push_back(contours[index]); - contrs.push_back(math::simplify_contour(contrs[0], cmax)); - cv::drawContours(contour_image, contrs, -1, 255); + contrs.push_back(c); + cv::drawContours(contour_image, contrs, 0, 255); + cv::drawContours(new_contour_image, contrs, 1, 255); + /* + */ imshow("Image", image); imshow("Binaire", binaire); imshow("Contour", contour_image); + imshow("New Contour", new_contour_image); cv::waitKey(0); return 0; diff --git a/tests/src/math.hpp b/tests/src/math.hpp index c2f510a..ea55537 100644 --- a/tests/src/math.hpp +++ b/tests/src/math.hpp @@ -22,18 +22,21 @@ namespace math { int indexNB; for (int index=0,indexNB=0;indexG) && (R>B)) - if (((R-B)>=seuil) || ((R-G)>=seuil)) - detect=1; - if (detect==1) + if ((R>G) && (R>B)) { + if (((R-B)>=seuil) || ((R-G)>=seuil)) { + detect = true; + } + } + if (detect==1) { output.data[indexNB]=255; - else + } else { output.data[indexNB]=0; + } } } @@ -105,6 +108,7 @@ namespace math { } else { opt_size = 1 << (int)std::ceil(std::log(N)/std::log(2)); } + opt_size = input.size(); csignal sig(input); for (int i=0; i bounds(const contour& cont) { + std::array res = {cont[0].x, cont[0].y, cont[0].x, cont[0].y}; + + for (auto p: cont) { + if (res[0] > p.x) { + res[0] = p.x; + } + if (res[1] > p.y) { + res[1] = p.y; + } + if (res[2] < p.x) { + res[2] = p.x; + } + if (res[3] < p.y) { + res[3] = p.y; + } + } + + return res; + } + + contour transform(contour& cont, std::array& bounds) { + contour res; + for (auto p: cont) { + int px = (p.x - bounds[0])/(bounds[2]-bounds[0]); + int py = (p.y - bounds[1])/(bounds[3]-bounds[1]); + res.push_back(cv::Point(px, py)); + } + return res; + } + contour simplify_contour(const contour& cont, int cmax) { csignal z = cont2sig(cont); complex zm = mean(z); csignal tfd = fft(diff(z, zm)); tfd /= tfd.size(); - csignal desc = extract(tfd, cmax); + int cmin = -cmax; + csignal desc = extract(tfd, cmin, cmax); - if (std::abs(desc[desc.size()-1]) > std::abs(desc[0])) { + if (std::abs(desc[desc.size()/2-1]) > std::abs(desc[desc.size()/2+1])) { std::reverse(desc.begin(), desc.end()); } - double phy = std::arg(desc[desc.size()-1]*desc[0])/2; + double phy = std::arg(desc[desc.size()/2-1]*desc[desc.size()/2+1])/2; desc *= std::exp(complex(0, -phy)); - double theta = std::arg(desc[0]); + double theta = std::arg(desc[desc.size()/2+1]); for (int k=0; k #include "math.hpp" -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; - } -} - int main(int argc, char** argv) { - int seuil=80; + int seuil=65; int cmax = 10; int N = 5000; @@ -61,7 +37,7 @@ int main(int argc, char** argv) { Y=frame.cols; DIM=frame.channels(); - filter(frame, binaire, seuil); + math::filter(frame, binaire, seuil); cv::imshow("Detection", binaire); cv::findContours(binaire, contours, hierarchy, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_NONE);