//根据轮廓的面积大小进行选择
vector<VP> selectShapeArea(Mat src,Mat& draw,vector<VP> contours,int minvalue,int maxvalue){
vector<VP> result_contours;
draw = Mat::zeros(src.rows,src.cols,CV_8UC3);
for (int i=0;i<contours.size();i++){
double countour_area = contourArea(contours[i]);
if (countour_area >minvalue && countour_area<maxvalue)
result_contours.push_back(contours[i]);
}
for (int i=0;i<result_contours.size();i++){
int iRandB = rng.uniform(0,255);
int iRandG = rng.uniform(0,255);
int iRandR = rng.uniform(0,255);
Scalar color = Scalar(iRandB,iRandG,iRandR);
drawContours(draw,result_contours,i,color,-1);
char cbuf[100];sprintf_s(cbuf,"%d",i+1);
//寻找最小覆盖圆,求出圆心。使用反色打印轮廓序号
float radius;
cv::Point2f center;
cv::minEnclosingCircle(result_contours[i],center,radius);
putText(draw,cbuf,center, FONT_HERSHEY_PLAIN ,5,Scalar(255-iRandB,255-iRandG,255-iRandR),5);
}
return result_contours;
}
vector<VP> selectShapeArea(vector<VP> contours,int minvalue,int maxvalue)
{
vector<VP> result_contours;
for (int i=0;i<contours.size();i++){
double countour_area = contourArea(contours[i]);
if (countour_area >minvalue && countour_area<maxvalue)
result_contours.push_back(contours[i]);
}
return result_contours;
}
在Halcon中,运用非常广泛的SelectShape的Opencv实现,能够根据轮廓的大小,直接挑选出所需要的部分。我在这个基础上进行了强化,能够把每个轮廓的序号标注出来。并且依然提供draw打印。
![](https://images2015.cnblogs.com/blog/508489/201609/508489-20160925234305702-378826109.png)
![](https://images2015.cnblogs.com/blog/508489/201609/508489-20160925234308186-525425401.png)