2个多边形的关系判断和相交面积计算


#include "stdafx.h"
#include <cv.h>
#include <highgui.h>

IplImage *Image1;

CvPoint PointArray1[6];
CvPoint PointArray2[4];
CvScalar Color;
int PolyVertexNumber;
int Shift;
int main()
{
CvSize ImageSize1 = cvSize(1000,700);
Image1 = cvCreateImage(ImageSize1, IPL_DEPTH_8U, 3);
IplImage* Image2 = cvCreateImage(ImageSize1, IPL_DEPTH_8U, 3);
IplImage* Image3 = cvCreateImage(ImageSize1, IPL_DEPTH_8U, 3);
IplImage* Image4 = cvCreateImage(ImageSize1, IPL_DEPTH_8U, 1);
PointArray1[0]=cvPoint(200,200);
PointArray1[1]=cvPoint(400,150);
PointArray1[2]=cvPoint(650,230);
PointArray1[3]=cvPoint(800,300);
PointArray1[4]=cvPoint(900,550);
PointArray1[5]=cvPoint(100,500);

PointArray2[0]=cvPoint(20,20);
PointArray2[1]=cvPoint(30,20);
PointArray2[2]=cvPoint(30,30);
PointArray2[3]=cvPoint(20,30);

CvScalar Color=CV_RGB(0,255,0);
PolyVertexNumber=6;
Shift=0;

cvFillConvexPoly(Image1,PointArray1,PolyVertexNumber,Color,CV_AA,Shift);


Color=CV_RGB(0,0,255,);
cvFillConvexPoly(Image2,PointArray2,4,Color,CV_AA,Shift);

cvAbsDiff(Image1,Image2,Image3);

cvThreshold(Image3,Image3,250,255,CV_THRESH_BINARY );
//
cvCvtColor(Image3,Image4,CV_BGR2GRAY);
cvThreshold(Image4,Image4,50,255,CV_THRESH_BINARY );
CvMemStorage *storage=cvCreateMemStorage(0);
CvSeq *contours=0;

int rs = cvFindContours(Image4,storage,&contours,sizeof(CvContour),
CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE);
double rr=0;
if (rs==1)
{
rr= cvContourArea(contours);
}

cvNamedWindow("FillConvexPoly",0);
cvShowImage("FillConvexPoly",Image1);
cvShowImage("FillConvexPoly2",Image2);
cvShowImage("FillConvexPoly3",Image3);
cvWaitKey(0);
//cvSaveImage("poly.jpg",Image1);
cvReleaseImage(&Image1);
return 0;
}

posted @ 2013-05-02 23:35  ahuo  阅读(1093)  评论(0编辑  收藏  举报