OpenCV最小外接正矩形

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

#include "stdafx.h"

#include "cv.h"

#include "highgui.h"

#include "cxcore.h"

#include "math.h"

#include <iostream.h>

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;i<hei;i++)

 {

     for(int j=0;j<wid;j++)

     {

        if (data[i*widstep+j*channel]>120)

        {

            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="<<length<<"   Area="<<area<<endl;  

     CvPoint p1;

     CvPoint p2;

     p1.x=rect.x;

     p1.y=rect.y;

     p2.x=rect.x+rect.width;

     p2.y=rect.y+rect.height;

     cout<<"p1=("<<p1.x<<","<<p1.y<<")";

     cout<<"p2=("<<p2.x<<","<<p2.y<<")"<<endl;

     cvRectangle(dst,p1,p2,CV_RGB(255,0,0),1,8,0); 

 }

     cvNamedWindow("dst",1);

     cvShowImage("dst",dst);

     cvWaitKey(0);

 return 0; 

}


 

原图:

二值化反色图:

最小正矩形图:

最小正矩形信息:

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值