OpenCV最小外接正矩形

最小外接正矩形 cvBoundingRect(contour,1)

#include "stdafx.h"

#include "cv.h"

#include "highgui.h"

#include "cxcore.h"

#include "math.h"

#include 

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

{    

 IplImage *src; 

 IplImage *dst;

 IplImage *ROI;

 CvMemStorage* storage=cvCreateMemStorage(0); 

 CvSeq* contour=0; 

 src=cvLoadImage("I:\\test.jpg",0);

 cvNamedWindow("image0",1);

 cvShowImage("image0",src);

 int   hei=src->height;

 int wid=src->width; 

 uchar *data;

 data=(uchar*)src->imageData;

 int widstep=src->widthStep;

 int channel=src->nChannels;

  dst=cvCreateImage(cvSize(wid,hei),IPL_DEPTH_8U,3);

  ROI=cvCreateImage(cvSize(wid,hei),IPL_DEPTH_8U,3);

 for (int i=0;i120)

        {

            data[i*widstep+j*channel]=0;

        }

        else

        {

            data[i*widstep+j*channel]=255;

        }

     }

 }

  cvNamedWindow("image",0); 

  cvShowImage("image",src);  

 printf("图像的高为:%d,宽为:%d\n\n",hei,wid); 

 cvCvtColor(src, dst, CV_GRAY2BGR);;

cvFindContours(src,storage,&contour,sizeof(CvContour),CV_RETR_CCOMP,CV_CHAIN_APPROX_SIMPLE); 

 for(;contour!=0;contour=contour->h_next) 

 

 {

     double length =cvArcLength(contour);  

     double area =fabs(cvContourArea(contour));  

     CvRect rect = cvBoundingRect(contour,1);  

  

     cout<<"Length="<


 

原图:

OpenCV最小外接正矩形_第1张图片

二值化反色图:

OpenCV最小外接正矩形_第2张图片

最小正矩形图:

OpenCV最小外接正矩形_第3张图片

最小正矩形信息:

OpenCV最小外接正矩形_第4张图片

你可能感兴趣的:(OpenCV)