oid Detection::findBestContour(Mat edge , vector<point> & contour ,vector<point> &hull,bool vis ) { vector<vector<point> > contours; vector<vec4i> hierarchy; int maxIndex=0,maxArea=0,tmpArea=0,minArea=100; // find the contours findContours( edge , contours, hierarchy, RETR_TREE, CHAIN_APPROX_SIMPLE, Point(0, 0) ); //compute the convex hull vector<vector<point> >hulls( contours.size() ); for( int i = 0; i < contours.size(); i++ ) { convexHull( Mat(contours[i]), hulls[i], false ); }
vector<int> indices(contours.size());
iota(indices.begin(), indices.end(), 0);
sort(indices.begin(), indices.end(), [&contours,&hulls](int lhs, int rhs) {
return contourArea(hulls[lhs]) > contourArea( hulls[rhs]);
});
Mat res = Mat::zeros(edge.rows, edge.cols, CV_8UC3);
int N = 5; // set number of largest contours
N = min(N, int(contours.size()));
if(contours.size()==0)
return;
drawContours(res, hulls, indices[0], Scalar{255,0,0});
//hull=hulls[indices[0]];
contour=contours[indices[0]];
for (int i =1; i < N; ++i)
{
//hull.insert( hull.end(), hulls[indices[i]].begin(), hulls[indices[i]].end() );
double area=contourArea(hulls[indices[i]]);
double arc=arcLength(contours[indices[i]], true);
if((area/arc)>5)
{
vector<Point> tmpContour;
vector<Point> tmpHull1;
vector<Point> tmpHull2;
tmpContour.insert( tmpContour.end(), contours[indices[i]].begin(), contours[indices[i]].end() );
tmpContour.insert( tmpContour.end(), contour.begin(),contour.end() );
convexHull( Mat(contour), tmpHull1, false );
convexHull( Mat(tmpContour), tmpHull2, false );
if((contourArea(tmpHull1)/contourArea(tmpHull2))<0.95&&
(contourArea(tmpHull1)/contourArea(tmpHull2))>0.8)
{
Scalar color(rand() & 255, rand() & 255, rand() & 255);
//drawContours(res, contours, indices[i], color);
drawContours(res, hulls, indices[i], color);
contour.insert( contour.end(), contours[indices[i]].begin(), contours[indices[i]].end() );
}
}
}
convexHull( Mat(contour), hull, false );
if(vis)
imshow("contour",res);
}
This is the code which I have to convert.
def findBestContour(edges,contor,hull):
r,c = edges.shape
print(r,c)
_,contours,hierarchy = cv2.findContours(edges,cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
hull =[]
for i in range(len(contours)):
hull.append(cv2.convexHull(contours[i], False))
indices = len(contours)
indices = range(0,len(contours))
# p#rint("indices",indices)
res = np.zeros([r,c], dtype='uint8')
N = 5
N = min(N,len(contours))
if(len(contours)==0):
return 1
cv2.drawContours(res,hull,indices[0],(255,0,0))
contour =contours[indices[0]]
print("contour",contour)
i =1
while i<N:
area = cv2.contourArea(hull[indices[i]])
print("area",area)
arc = cv2.arcLength(contours[indices[i]],True)
print("arc",arc)
if((area/arc)>5):
tmpContour = []
tmphull1 = []
tmphull2 = []
_ =[ tmpContour.insert(len(tmpContour),contours[indices[i]][j]) for j in range(len(contours[indices[i]])) ]
_ =[ tmpContour.insert(len(tmpContour),contour[j]) for j in range(len(contour)) ]
tmphull1 =(cv2.convexHull(contour,False))
print("tmphull1",tmphull1)
if (cv2.contourArea(tmphull1)/cv2.contourArea(tmphull2))<0.95 and (cv2.contourArea(tmphull1)/cv2.contourArea(tmphull2) > 0.8):
cv2.drawContours(res,hull,i,(255,255,255))
_ =[ contour.insert(len(contour),contours[indices[i]][j]) for j in range(len(contours[indices[i]]))]
i=1+i
cv2.convexHull(contor,hull,False)
cv2.imshow("res",res)
cv2.waitKey(0)
cv2.destroyAllWindows()
This is what I did.
Help needed
1). sort(indices.begin(), indices.end(), &contours,&hulls { return contourArea(hulls[lhs]) > contourArea( hulls[rhs]);
(Here we are sorting indices left to right but I don't know how to write into python.
2) Please go through the PYTHON code and give me overall feedback
p.s. I never Worked on C++
THANK YOU IN ADVANCE