Thanks for the resources, they’re much appreciated. I have modified the original code using some of the functions but I run into two problems. Firstly, when estimateTransformation() is comparing a contour with itself, it evaluation the rotation as -180 degrees. Secondly, I can only get 1 of the rotated contours drawn, the second one doesn’t appear. Would appreciate your input!
Here is the code:
int main(int argc, char **argv)
{
vector<vector> contours;
vector hierarchy;
Mat mTest,mThresh,mConnected;
Mat m1,m2,m3,m4,m5;
Mat m = imread("/Users/Memo/Desktop/image.jpg", IMREAD_GRAYSCALE);
bitwise_not(m,m1);
threshold(m1,mThresh,5,255,THRESH_BINARY);
findContours(mThresh,contours,hierarchy, RETR_TREE, CHAIN_APPROX_NONE);
vector<int> ctrSelec;
for (int i = 0; i < contours.size(); i++)
{
if (contours[i].size()>= 500 )
{
ctrSelec.push_back(i);
}
}
Mat mc = Mat::zeros(m.size(),CV_8UC3);
vector< vector<Point2f> >z;
vector< vector<Point2f> >Z;
z.resize(ctrSelec.size());
Z.resize(ctrSelec.size());
for (int i = 0; i < ctrSelec.size();i++)
{
ximgproc::contourSampling(contours[ctrSelec[i]], z[i], 1024);
dft(z[i],Z[i],DFT_SCALE); //Fourier transform
}
int indRef = 1;
vector<float> alpha,phi,s;
alpha.resize(ctrSelec.size());
phi.resize(ctrSelec.size());
s.resize(ctrSelec.size());
ximgproc::ContourFitting fit;
double dist;
Mat t;
fit.setFDSize(50);
fit.setCtrSize(1024);
Mat dst;
vector<vector<Point>> ctrRotated;
for (int i = 0; i < ctrSelec.size();i++)
{
fit.estimateTransformation(z[indRef], z[i], t, &dist, false);
cout<<“Contour “<<indRef<<” with “<<i<< " origin “<<1-t.at(0,0)<<” and rotated of”<<t.at(0, 1)* 180 / M_PI<<” and scale "<<t.at(0, 2)<<endl;
for (int i = 0; i < ctrSelec.size(); i++)
{
if (i!=indRef)
drawContours(mc,contours,ctrSelec[i],Scalar(255,0,0));
else
drawContours(mc,contours,ctrSelec[i],Scalar(255,255,255));
putText(mc,format("%d",i),Point(Z[i][0].x,Z[i][0].y),FONT_HERSHEY_SIMPLEX,1,Scalar(255,0,0));
}
ximgproc::transformFD(z[1], t, dst, false);
cout<<"bla bla"<<endl;
ctrRotated.push_back(dst);
drawContours(mc,ctrRotated,0,Scalar(0,0,255));
// for (int i = 0; i < ctrSelec.size(); i++)
// {
// drawContours(mc,ctrRotated,i,Scalar(0,0,255));
// }
}
imshow("mc ",mc);
waitKey();
return 0;
};