void DealConHull(int Threshvalue, Mat &temp,int &i,cv::Mat &ROIMG)
{
int pos=0;
double area=0;
double maxArea=0;
float ratio=0.0;
std::vector<double>Areas;
Mat Thresh_Image;
std::vector<std::vector<Point> >contours;
std::vector<Vec4i>hierarchy;
threshold(temp,Thresh_Image,Threshvalue,255,THRESH_BINARY);
findContours(Thresh_Image,contours,hierarchy,CV_RETR_TREE,CV_CHAIN_APPROX_SIMPLE,Point(0,0));
std::vector<std::vector<Point> >hull(contours.size());
for(int i=0;i<contours.size();i++)
{
convexHull(Mat(contours[i]),hull[i],false);
}
std::vector<std::vector<Point> >SelectPoints;
for(int k=0;k<hull.size();k++)
{
area=fabs(contourArea(hull[k]));
if(area>AreaValue_min&&area<AreaValue_max)
{
Areas.push_back(area);
SelectPoints.push_back(hull[k]);
}
}
if(Areas.size()>0)
{
pos=(int)(max_element(Areas.begin(),Areas.end())-Areas.begin());
RotatedRect rect=minAreaRect(SelectPoints[pos]);
Point2f p[4];
rect.points(p);
double max_x=p[0].x;
double max_y=p[0].y;
double min_x=p[0].x;
double min_y=p[0].y;
for(int j=0;j<=3;j++)
{
max_x=max_x>p[j].x?max_x:p[j].x;
max_y=max_y>p[j].y?max_y:p[j].y;
min_x=min_x<p[j].x?min_x:p[j].x;
min_y=min_y<p[j].y?min_y:p[j].y;
}
max_x=max_x>Thresh_Image.cols?Thresh_Image.cols:max_x;
max_y=max_y>Thresh_Image.rows?Thresh_Image.rows:max_y;
min_x=min_x<1?1:min_x;
min_y=min_y<1?1:min_y;
int width=(int)(max_x-min_x);
int height=(int)(max_y-min_y);
if(width<Thresh_Image.cols&&width>0&&height<Thresh_Image.rows&&height>0)
{
ROIMG=temp(Rect((int)min_x,(int)min_y,width,height));
}
else
{
cout<<i+1<<" Confusion fingers!"<<endl;
}
}
else
{
cout<<i+1<<" ratio: "<<ratio<<" No vein!"<<endl;
}
}