|
|
|
@ -22,18 +22,21 @@ namespace math { |
|
|
|
int indexNB; |
|
|
|
|
|
|
|
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]; |
|
|
|
detect = false; |
|
|
|
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) |
|
|
|
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<opt_size-input.size(); ++i) { |
|
|
|
sig.push_back(complex(0, 0)); |
|
|
|
@ -136,12 +140,12 @@ namespace math { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
csignal extract(const csignal& tfd, int cmax) { |
|
|
|
csignal extract(const csignal& tfd, int cmin, int cmax) { |
|
|
|
csignal res; |
|
|
|
for (int k=0; k<cmax; ++k) { |
|
|
|
res.push_back(tfd[tfd.size() - cmax + k]); |
|
|
|
} |
|
|
|
for (int k=cmax; k<2*cmax; ++k) { |
|
|
|
int kmin = tfd.size()/2 + cmin; |
|
|
|
int kmax = tfd.size()/2 + cmax; |
|
|
|
|
|
|
|
for (int k=kmin; k<kmax; ++k) { |
|
|
|
res.push_back(tfd[k]); |
|
|
|
} |
|
|
|
return res; |
|
|
|
@ -155,41 +159,75 @@ namespace math { |
|
|
|
return res; |
|
|
|
} |
|
|
|
|
|
|
|
csignal desc2sig(const csignal& desc, complex mean, int N, int kmin) { |
|
|
|
csignal desc2sig(const csignal& desc, complex mean, int N, int cmin, int cmax) { |
|
|
|
csignal cont; |
|
|
|
auto desc_it = desc.begin(); |
|
|
|
int kmin = desc.size()/2 + cmin; |
|
|
|
int kmax = desc.size()/2 + cmax; |
|
|
|
|
|
|
|
for (int m=0; m<N; ++m) { |
|
|
|
complex sum; |
|
|
|
for (int k=0; k<desc.size(); ++k) { |
|
|
|
sum += desc[k]*std::exp(complex(0, 2*pi()*(k+kmin)*m/N)); |
|
|
|
for (int k=kmin; k<kmax; ++k) { |
|
|
|
sum += desc[k]*std::exp(complex(0, 2*pi()*k*m/N)); |
|
|
|
} |
|
|
|
cont.push_back(mean + sum); |
|
|
|
} |
|
|
|
return cont; |
|
|
|
}; |
|
|
|
|
|
|
|
std::array<int, 4> bounds(const contour& cont) { |
|
|
|
std::array<int, 4> 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<int, 4>& 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<desc.size(); ++k) { |
|
|
|
desc[k] *= std::exp(complex(0, -theta*k)); |
|
|
|
desc[k] *= std::exp(complex(0, -theta*(k-cmin))); |
|
|
|
} |
|
|
|
desc /= desc[0]; |
|
|
|
desc /= desc[desc.size()/2+1]; |
|
|
|
|
|
|
|
csignal sig = desc2sig(desc, zm, z.size(), cmax); |
|
|
|
csignal sig = desc2sig(desc, zm, z.size(), cmin, cmax); |
|
|
|
return sig2cont(sig); |
|
|
|
}; |
|
|
|
|
|
|
|
|