1、cvConvexHull2 尋找凸包
hull := cvConvexHull2 (ptseq, 0, CV_CLOCKWISE, 1);//ptseq,hull:pCvSeq;
//畫出凸包點,並將凸包點用直線連接起來
ppoint:=cvGetSeqElem(hull,hull.total-1);//ppoint:pCvPoint;
p1.x:=ppoint.x; //p1:TCvPoint;
p1.y:=ppoint.y;
for idx := 0 to hull.total-1 do
begin
ppoint:=cvGetSeqElem(hull,idx);
tempoint.x:=ppoint.x;//tempoint:Tcvpoint;
tempoint.y:=ppoint.y;
cvCircle(srcimage,tempoint,3,CV_RGB(255,255,255),3,8,0);
cvLine(srcimage,p1,tempoint,CV_RGB(255,255,255),3,8,0);
p1:=tempoint;
end;
cvShowImage('畫出凸包',srcimage);
cvZero(srcimage);
//還可以直接用cvDrawContours將找到的凸包畫出來
cvDrawContours(srcimage, hull, CV_RGB(255,255,255), CV_RGB(255, 255, 255), 0, -1, 8,cvPoint(0,0));//-1表示將凸包內部填充,取值1表示連接輪廓,連接線的寬度為1,取值2,3以此類推
2、cvBoundingRect 尋找外部矩形
rect:=cvBoundingRect(ptseq,1);//rect:TCvRect;ptseq:pCvSeq;
tempoint.x:=rect.x;//tempoint,p1:Tcvpoint;
tempoint.y:=rect.y;
p1.x:=rect.x+rect.width;
p1.y:=rect.y+rect.height;
cvRectangle(srcimage,tempoint,p1,CV_RGB(255,255,255),3,8,0);
cvShowImage('外部矩形',srcimage);
cvzero(srcimage);
3、cvMinAreaRect2 最小外接矩形
box2d:=cvCreateMemStorage ();//box2d: pCvMemStorage;
box:=cvMinAreaRect2(ptseq,box2d);//box:TCvBox2D;ptseq:pCvSeq;
cvBoxPoints(box,pt0,pt1,pt2,pt3);//cvBoxPoints為一個自定義函數,下面將貼出函數代碼,pt0,pt1,pt2,pt3:Tcvpoint:
cvLine(srcimage,pt0,pt1,CV_RGB(255,255,255),3,8,0);
cvLine(srcimage,pt2,pt1,CV_RGB(255,255,255),3,8,0);
cvLine(srcimage,pt2,pt3,CV_RGB(255,255,255),3,8,0);
cvLine(srcimage,pt0,pt3,CV_RGB(255,255,255),3,8,0);
cvShowImage('最小外接矩形',srcimage);
cvzero(srcimage);
cvBoxPoints(MinAreaRectbox: TCvBox2D; var p0, p1, p2,p3: TCvPoint);//自定義函數的功能是根據TCvBox2D結構體中的參數,返回最小外接矩形的四個頂點
var
a,b:Single;
begin
a:=sin(MinAreaRectbox.angle*3.1415927/180)/2;
b:=cos(MinAreaRectbox.angle*3.1415927/180)/2;
p0.x:= round(MinAreaRectbox.center.x - a*MinAreaRectbox.size.height - b*MinAreaRectbox.size.width);
p0.y:= round(MinAreaRectbox.center.y + b*MinAreaRectbox.size.height - a*MinAreaRectbox.size.width);
p1.x:= round(MinAreaRectbox.center.x + a*MinAreaRectbox.size.height - b*MinAreaRectbox.size.width);
p1.y:= round(MinAreaRectbox.center.y - b*MinAreaRectbox.size.height - a*MinAreaRectbox.size.width);
p2.x:= round(2*MinAreaRectbox.center.x - p0.x);
p2.y:= round(2*MinAreaRectbox.center.y - p0.y);
p3.x:= round(2*MinAreaRectbox.center.x - p1.x);
p3.y:= round(2*MinAreaRectbox.center.y - p1.y);
end;
4、cvMinEnclosingCircle 最小外接圓
cvMinEnclosingCircle(ptseq,@center,@radius);//center:TCvPoint2D32f;radius:Single;ptseq:pCvSeq;
tempoint.x:=round(center.x);//tempoint:TCVpoint;
tempoint.y:=round(center.y);
cvCircle(srcimage,tempoint,Round(radius),CV_RGB(255,255,255),3,8,0);
cvShowImage('最小外接圓',srcimage);
cvzero(srcimage);