opencv 轮廓的长度,面积,外接矩形(平行坐标轴),处接最小矩形,处接圆 , 椭圆

代码如下:

#include <cv.h>
#include <cxcore.h>
#include <highgui.h>
#include <iostream>
using namespace std;

int main()
{
	IplImage *src = cvLoadImage("f:\\images\\test2.bmp",CV_LOAD_IMAGE_GRAYSCALE);
	CvMemStorage *storage = cvCreateMemStorage();
	CvSeq *seq = NULL;
	int cnt = cvFindContours(src,storage,&seq);
	seq = seq->h_next;
	double length = cvArcLength(seq);
	double area = cvContourArea(seq);
	CvRect rect = cvBoundingRect(seq,1);
	CvBox2D box = cvMinAreaRect2(seq,NULL);
	
	cout<<"Length = "<<length<<endl;
	cout<<"Area = "<<area<<endl;
	
	IplImage *dst = cvCreateImage(cvGetSize(src),8,3); cvZero(dst);
	cvDrawContours(dst,seq,CV_RGB(255,0,0),CV_RGB(255,0,0),0);
	cvRectangleR(dst,rect,CV_RGB(0,255,0));
	cvShowImage("dst",dst);
	cvWaitKey();

	CvPoint2D32f center;
	float radius;
	cvMinEnclosingCircle(seq,¢er,&radius);
	cvCircle(dst,cvPointFrom32f(center),cvRound(radius),CV_RGB(100,100,100));
	cvShowImage("dst",dst);
	cvWaitKey();

	CvBox2D ellipse = cvFitEllipse2(seq);
	cvEllipseBox(dst,ellipse,CV_RGB(255,255,0));
	cvShowImage("dst",dst);
	cvWaitKey();

	//绘制外接最小矩形
	CvPoint2D32f pt[4];
	cvBoxPoints(box,pt);
	for(int i = 0;i<4;++i){
		cvLine(dst,cvPointFrom32f(pt[i]),cvPointFrom32f(pt[((i+1)%4)?(i+1):0]),CV_RGB(0,0,255));
	}
	cvShowImage("dst",dst);
	cvWaitKey();

	cvReleaseImage(&src);
	cvReleaseImage(&dst);
	cvReleaseMemStorage(&storage);	
}

输入图像 为bmp格式(自己用纯黑画的,已经是二值了), 如果用我下面的图片直接另存为,则就先载入图像,然后灰度化,再二值化



结果:

opencv 轮廓的长度,面积,外接矩形(平行坐标轴),处接最小矩形,处接圆 , 椭圆_第1张图片




你可能感兴趣的:(image,null,DST)