标签:
//灰度域变化 threshold(gray,gray,0,255,THRESH_BINARY_INV); GaussianBlur(gray,gray,Size(3,3),0,0); //寻找和绘制轮廓 VP bigestContour = FindBigestContour(gray); contours.push_back(bigestContour);
vector<vector<point>>
int itopleft =65535; int idownright =0; Point ptopleft; Point pdownright; Point pdownleft; for(int i=0;i<bigestContour.size();i++){ //左上 if(bigestContour[i].x + bigestContour[i].y <itopleft){ itopleft = bigestContour[i].x + bigestContour[i].y ; ptopleft = bigestContour[i]; } //右下 if(bigestContour[i].x+bigestContour[i].y>idownright){ idownright = bigestContour[i].x+bigestContour[i].y; pdownright = bigestContour[i]; } } int idownleft =65534; //对于左下的点来说,应该是所有y大于左上的点中,x最小的 for(int i=0;i<bigestContour.size();i++){ if(bigestContour[i].y>ptopleft.y){ if(bigestContour[i].x<idownleft){ idownleft = bigestContour[i].x; pdownleft = bigestContour[i]; } } } //绘制 circle(board,ptopleft,10,Scalar(255),5); circle(board,pdownright,10,Scalar(255),5); circle(board,pdownleft,10,Scalar(255),5);
///在board上寻找角点 ///// Detector parameters int blockSize = 2; int apertureSize = 3; double k = 0.04; int thresh = 1; /// Detecting corners board.convertTo(board,CV_32F); cornerHarris( board,dst,2,3,0.04); ///// Normalizing normalize( dst, dst_norm, 0, 255, NORM_MINMAX, CV_32FC1, Mat() ); convertScaleAbs( dst_norm, dst_norm_scaled ); ///// Drawing a circle around corners for( int j = 0; j < dst_norm.rows ; j++ ) { for( int i = 0; i < dst_norm.cols; i++ ) { if( (int) dst_norm.at<float>(j,i) > thresh ) { circle( dst_norm_scaled, Point( i, j ), 5, Scalar(0), 2, 8, 0 ); circle(src,Point( i, j ), 5, Scalar(255,0,0), -1, 8, 0 ); }
}
}
//遍历轮廓,求出所有支撑角度 int icount = bigestContour.size(); float fmax = -1;//用于保存局部最大值 int imax = -1; bool bstart = false; for (int i=0;i<bigestContour.size();i++){ Point2f pa = (Point2f)bigestContour[(i+icount-7)%icount]; Point2f pb = (Point2f)bigestContour[(i+icount+7)%icount]; Point2f pc = (Point2f)bigestContour[i]; //两支撑点距离 float fa = getDistance(pa,pb); float fb = getDistance(pa,pc)+getDistance(pb,pc); float fang = fa/fb; float fsharp = 1-fang; if (fsharp>0.05){ bstart = true; if (fsharp>fmax){ fmax = fsharp; imax = i; } }else{ if (bstart){ circle(board,bigestContour[imax],10,Scalar(255),1); circle(src,bigestContour[imax],10,Scalar(255,255,255),1); imax = -1; fmax = -1; bstart = false; } } }
标签:
原文地址:http://www.cnblogs.com/jsxyhelu/p/5106760.html