您的位置:首页 > 运维架构

用opencv画虚线框矩形

2016-04-08 15:50 281 查看
#include <opencv2/opencv.hpp>

using namespace std;
using namespace cv;

void  drawDashRect(CvArr* img,int linelength,int dashlength,CvRect* blob,CvScalar color,int thickness)
{
int w=cvRound(blob->width);//width
int h=cvRound(blob->height);//height

int tl_x=cvRound(blob->x);//top left x
int tl_y=cvRound(blob->y);//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);
}

}

int main(int argc, char* argv[])
{

IplImage *img = cvLoadImage( "F:\\chen\\车辆跟踪对比图\\blurcar4 0024.jpg");

CvRect rect1 = cvRect(100,200,200,100);

drawDashRect(img,1,2,&rect1,CV_RGB(255,255,255),1);
cvShowImage("1",img);

cvWaitKey(0);

return 0;
}


做对比实验时,经常用到虚线框,但是opencv中没有找到直接函数,所以就在网上看见大神写的,借过来用,怕以后忘记,所以现在记下来
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: