opencv绘制多边形

时间:2022-02-08 03:59:16

opencv2.4.4

今天用cvFilllPoly()进行图像的绘制,其中遇到一些参数的问题,个人理解如下:

CVAPI(void)  cvFillPoly( CvArr* img, CvPoint** pts, const int* npts,
                         int contours, CvScalar color,
                         int line_type CV_DEFAULT(8), int shift CV_DEFAULT(0) );

img:图像

pst:折线的顶点指针数组,即:每个图像数组首个坐标的地址

npts:多边形顶点个数数组,2-2个图像,4-第一个图像有4个顶点,3-第二个图像有3个顶点

contours:折线的线段数量,如第三个参数说明所示,我想画两个多边形,那这个参数就是2,如果想画4个多边形,这个参数就是4

color:多边形中的填充色

line_type :组成多边形线段类型

shift:顶点坐标的小数点位数


个人觉得cvFillPoly的难点主要在于双指针和数组处,写了一个程序

方法三:

//画多边形
#include "cv.h"
#include "highgui.h"
#include "cxcore.h"
#include "stdio.h"


int main(void)
{
IplImage* image;
CvSize imagesize = cvSize(640,640);
CvPoint spoint1[3] = {{100,150},{430,240},{352,466}};
CvPoint spoint2[4] = {{440,300},{560,450},{318,600},{100,419}};
CvPoint* pts1[1] = {&spoint1[0]};
CvPoint* pts2[1] = {&spoint2[0]};
int npts1[1] = {3};
int npts2[1] = {4};


image = cvCreateImage(imagesize, IPL_DEPTH_8U, 3);
cvFillPoly(image, pts1, npts1, 1, CV_RGB(255,0,0),8,0);
cvFillPoly(image, pts2, npts2, 1, CV_RGB(0,0,255),8,0);


cvShowImage("image", image);
cvWaitKey(0);
}

方法二:

//画多边形
#include "cv.h"
#include "highgui.h"
#include "cxcore.h"
#include "stdio.h"


int main(void)
{
IplImage* image;
CvSize imagesize = cvSize(640,640);
CvPoint spoint1[3] = {{100,150},{430,240},{352,466}};
CvPoint spoint2[4] = {{440,300},{560,450},{318,600},{100,419}};
CvPoint* pts1[2] = {&spoint1[0],&spoint2[0]};
int npts1[2] = {3,4};


image = cvCreateImage(imagesize, IPL_DEPTH_8U, 3);
cvFillPoly(image, pts1, npts1, 2, CV_RGB(255,0,0),8,0);


cvShowImage("image", image);
cvWaitKey(0);
}


方法三:

//画多边形
/*#include "cv.h"
#include "highgui.h"
#include "cxcore.h"
#include "stdio.h"


int main(void)
{
IplImage* image;
CvSize imagesize = cvSize(640,640);
CvPoint spoint1[3] = {{100,150},{430,240},{352,466}};
CvPoint spoint2[4] = {{440,300},{560,450},{318,600},{100,419}};
CvPoint* pts1[1] = {&spoint1[0]};
CvPoint* pts2[1] = {&spoint2[0]};
int npts1[1] = {3};
int npts2[1] = {4};


image = cvCreateImage(imagesize, IPL_DEPTH_8U, 3);
cvFillPoly(image, pts1, npts1, 1, CV_RGB(255,0,0),8,0);
cvFillPoly(image, pts2, npts2, 1, CV_RGB(0,0,255),8,0);


cvShowImage("image", image);
cvWaitKey(0);
}*/


//画多边形
#include "cv.h"
#include "highgui.h"
#include "cxcore.h"
#include "stdio.h"


int main(void)
{
IplImage* image;
CvSize imagesize = cvSize(640,640);
CvPoint spoint1[7];
CvPoint* pts1[2] = {&spoint1[0],&spoint1[3]};
int npts1[2] = {3,4};


spoint1[0] = cvPoint(100,150);
spoint1[1] = cvPoint(430,240);
spoint1[2] = cvPoint(352,466);
spoint1[3] = cvPoint(440,300);
spoint1[4] = cvPoint(560,450);
spoint1[5] = cvPoint(318,600);
spoint1[6] = cvPoint(100,419);


image = cvCreateImage(imagesize, IPL_DEPTH_8U, 3);
cvFillPoly(image, pts1, npts1, 2, CV_RGB(255,0,0),8,0);


cvShowImage("image", image);
cvWaitKey(0);
}