代码如下:
- #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格式(自己用纯黑画的,已经是二值了),
如果用我下面的图片直接另存为,则就先载入图像,然后灰度化,再二值化
结果:
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请
点击举报。