fin de l'implémentation des descripteurs de Fourrier, enfin

This commit is contained in:
Guillaume Courrier 2019-12-16 17:30:29 +01:00
parent b3871fa415
commit c132000776
3 changed files with 47 additions and 12 deletions

View file

@ -11,7 +11,7 @@ int main (int ac,char ** av) {
csignal coord = {0,1,0,0,0,0,0}; csignal coord = {0,1,0,0,0,0,0};
coord = {0,1,0,0,0}; coord = {0,1,0,0,0};
csignal res; csignal res;
res = fft_rec(coord); res = dft(coord);
for(int n=0; n<coord.size(); n++) { for(int n=0; n<coord.size(); n++) {
cout << coord[n] << endl; cout << coord[n] << endl;

View file

@ -100,6 +100,21 @@ namespace math {
return res; return res;
} }
csignal& dft(const csignal& input) {
csignal* res = new csignal();
int size = input.size();
for (int k=0; k<size; ++k) {
complex t=0;
for (int n=0; n<size; ++n) {
t += (input[n] * std::exp(complex(0, -2*pi()*n*k/size)));
}
std::cout << t << std::endl;
res->push_back(t);
}
return *res;
}
csignal fft_rec(const csignal& input) { csignal fft_rec(const csignal& input) {
int size = input.size(); int size = input.size();
std::cout << "Size: " << size << std::endl; std::cout << "Size: " << size << std::endl;
@ -277,10 +292,34 @@ namespace math {
return res; return res;
} }
csignal descriptors(const contour& cont, int cmax) {
csignal z = cont2sig(cont);
complex zm = mean(z);
csignal tfd = dft(diff(z, zm));
tfd /= z.size();
int cmin = -cmax;
csignal desc = extract(tfd, cmin, cmax);
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()/2-1]*desc[desc.size()/2+1])/2;
desc *= std::exp(complex(0, -phy));
double theta = std::arg(desc[desc.size()/2+1]);
for (int k=0; k<desc.size(); ++k) {
desc[k] *= std::exp(complex(0, -theta*(k-cmin)));
}
desc /= std::abs(desc[desc.size()/2+1]);
return desc;
}
contour simplify_contour(const contour& cont, int cmax) { contour simplify_contour(const contour& cont, int cmax) {
csignal z = cont2sig(cont); csignal z = cont2sig(cont);
complex zm = mean(z); complex zm = mean(z);
csignal tfd = fft(diff(z, zm)); csignal tfd = dft(diff(z, zm));
tfd /= z.size(); tfd /= z.size();
int cmin = -cmax; int cmin = -cmax;
csignal desc = extract(tfd, cmin, cmax); csignal desc = extract(tfd, cmin, cmax);

View file

@ -6,7 +6,6 @@
int main(int argc, char** argv) { int main(int argc, char** argv) {
int seuil=65; int seuil=65;
int cmax = 10; int cmax = 10;
int N = 5000;
if (argc > 1) { if (argc > 1) {
seuil = atol(argv[1]); seuil = atol(argv[1]);
@ -31,7 +30,6 @@ int main(int argc, char** argv) {
X=frame.rows; X=frame.rows;
Y=frame.cols; Y=frame.cols;
cv::Mat binaire(X,Y,CV_8UC1); cv::Mat binaire(X,Y,CV_8UC1);
cv::imshow("Image", frame);
cv::GaussianBlur(frame, frame, cv::Size(7,7), 1.5, 1.5); cv::GaussianBlur(frame, frame, cv::Size(7,7), 1.5, 1.5);
X=frame.rows; X=frame.rows;
Y=frame.cols; Y=frame.cols;
@ -39,7 +37,6 @@ int main(int argc, char** argv) {
math::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); cv::findContours(binaire, contours, hierarchy, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_NONE);
cv::Mat Dessin = cv::Mat::zeros(X,Y, CV_8UC1); cv::Mat Dessin = cv::Mat::zeros(X,Y, CV_8UC1);
cv::Mat new_contour_image = cv::Mat::zeros(X,Y, CV_8UC1); cv::Mat new_contour_image = cv::Mat::zeros(X,Y, CV_8UC1);
@ -49,20 +46,19 @@ int main(int argc, char** argv) {
int id = math::max_cont(contours); int id = math::max_cont(contours);
contrs.push_back(contours[id]); contrs.push_back(contours[id]);
std::cout << "Number of countours: "
<< contours.size()
<< "; Index of biggest contour: "
<< id
<< std::endl;
contrs.push_back(math::simplify_contour(contrs[0], cmax)); contrs.push_back(math::simplify_contour(contrs[0], cmax));
math::csignal desc = math::descriptors(contrs[0], cmax);
cv::drawContours(Dessin, contrs, 0, 255); cv::drawContours(Dessin, contrs, 0, 255);
cv::drawContours(new_contour_image, contrs, 1, 255); cv::drawContours(new_contour_image, contrs, 1, 255);
} }
cv::imshow("Image", frame);
cv::imshow("Detection", binaire);
cv::imshow("Contours", Dessin); cv::imshow("Contours", Dessin);
cv::imshow("New Contours", new_contour_image); cv::imshow("New Contours", new_contour_image);
if(cv::waitKey(30) == 27) { if(cv::waitKey(30) == 27) {
break; break;
} }