转自:http://blog.sina.com.cn/s/blog_4b0020f301010cy8.html
前两天查资料看opencv 画虚线矩形,先收录一下;后面有可能会用得到。不过代码没有测试。
OpenCV2.0提供了一些比较简单的绘图函数,比如绘制 矩形,椭圆,圆,多边形,直线;但都是实线,有时需要用做对比试验,虚线矩形就派上了用场,下面是一种简单的实现。
void drawDashRect(CvArr* img,int linelength,int dashlength,CvBlob* blob,CvScalar color,int thickness) { int w=cvRound(blob->w);//width int h=cvRound(blob->h);//height int tl_x=cvRound(blob->x-blob->w/2);//top left x int tl_y=cvRound(blob->y-blob->h/2);//top left y int totallength=dashlength+linelength; int nCountX=w/totallength;// int nCountY=h/totallength;// CvPoint start,end;//start and end point of each dash //draw the horizontal lines start.y=tl_y; start.x=tl_x; end.x=tl_x; end.y=tl_y; for (int i=0;i<nCountX;i++) { end.x=tl_x+(i+1)*totallength-dashlength;//draw top dash line end.y=tl_y; start.x=tl_x+i*totallength; start.y=tl_y; cvLine(img,start,end,color,thickness); } for (int i=0;i<nCountX;i++) { start.x=tl_x+i*totallength; start.y=tl_y+h; end.x=tl_x+(i+1)*totallength-dashlength;//draw bottom dash line end.y=tl_y+h; cvLine(img,start,end,color,thickness); } for (int i=0;i<nCountY;i++) { start.x=tl_x; start.y=tl_y+i*totallength; end.y=tl_y+(i+1)*totallength-dashlength;//draw left dash line end.x=tl_x; cvLine(img,start,end,color,thickness); } for (int i=0;i<nCountY;i++) { start.x=tl_x+w; start.y=tl_y+i*totallength; end.y=tl_y+(i+1)*totallength-dashlength;//draw right dash line end.x=tl_x+w; cvLine(img,start,end,color,thickness); } }