1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
| int detection::compare_contours()
{ int nb=0;
vector<vector<Point> > contourp1;
vector<vector<Point> > contourp2;
vector<Vec4i> hierarchyp2;
canny ct1(im1);
canny ct2 (im2);
ct1.edge_show();
ct2.edge_show();
if( (ct1.contours.size())!=(ct2.contours.size()) )
{
nb=1;
cout<<"nouveaux objets introduits"<<endl;
}
else {
cout<<"memes objets"<<endl;
}
contourp1=ct1.contours;
contourp2=ct2.contours;
hierarchyp2=ct2.hierarchy;
int k=0;
/*********************************************/
for (int j=0;j<ct2.contours.size();j++)
{
double result=0;
for (int i=0;i<ct1.contours.size();i++)
{
result=result+matchShapes(Mat(ct1.contours[i]), Mat(ct2.contours[j]), CV_CONTOURS_MATCH_I3, 0);
}
if (result==0) {
contourp2.erase(contourp2.begin()+k);
hierarchyp2.erase(hierarchyp2.begin()+k);
}
else k++;
}
k=0;
for (int j=0;j<ct1.contours.size();j++)
{
double result=0;
for (int i=0;i<ct2.contours.size();i++)
{
result=result+matchShapes(Mat(ct2.contours[i]), Mat(ct1.contours[j]), CV_CONTOURS_MATCH_I3, 0);
}
if (result==0) {contourp1.erase(contourp1.begin()+k);}
else k++;
}
cout << contourp1.size() << endl;
/************************************************************/
for (int j=0;j<contourp2.size();j++)
{
int i=0;
double compare =1;
//int size=1;&&(size!=0) && (size!=0)
while ( (i<contourp1.size()) && (compare!=0) )
{
compare=cv::matchShapes(Mat(contourp1[i]), Mat(contourp2[j]), CV_CONTOURS_MATCH_I1, 0);
// if (contourp1[i].size()==contourp2[j].size()){size=0;}
i++;
}
if (compare!=0) {nvcontour.push_back(contourp2[j]);
nvhierarchy.push_back(ct2.hierarchy[j]);}
}
/***************************************************/
if ( nvcontour.size() != 0)
{
cout <<"images differentes"<<endl;
cout <<" taille du nouveau vecteur"<<nvcontour.size()<<endl;
}
else {
cout <<"images similaires"<<endl;
cout <<" taille du nouveau vecteur"<<nvcontour.size()<<endl;
}
//drawing
Mat dstt= Mat::zeros(ct1.src.rows,ct2.src.cols, CV_8UC3);
for(int idx=0;idx<nvcontour.size();idx++ )
{
Scalar color( rand()&255, rand()&255, rand()&255 );
drawContours( dstt,nvcontour, idx, color, CV_FILLED, 8);
}
//show in window
namedWindow( "nouveaux", CV_WINDOW_AUTOSIZE );
imshow( "nouveaux",dstt);
return nb;
} |
Partager