有没有人可以帮我解释一下下面OpenCV画轮廓代码的意思?很急 在线等!!! 100
voidFindContours(IplImage*dst){cvFindContours(dst,storage,&contour,sizeof(CvContour),...
void FindContours( IplImage* dst )
{
cvFindContours(dst,storage,&contour,sizeof(CvContour),CV_RETR_EXTERNAL,CV_CHAIN_APPROX_SIMPLE);
if(contour)
cvDrawContours(dst,contour,cvScalarAll(255),cvScalarAll(255),100); //画轮廓
//cvShowImage("Contours",dst);
CvMoments m;
CvMat mat;
double M00;
CvSeq* contour2=contour;
IplImage *pTmp=cvCreateImage(cvGetSize(dst),IPL_DEPTH_8U,1);
cvSet(pTmp, cvScalar(0,0,0),0);
IplImage *pTmp1=cvCreateImage(cvGetSize(dst),IPL_DEPTH_8U,1);
cvSet(pTmp1, cvScalar(0,0,0),0);
for(;contour2!=0;contour2=contour2->h_next)
{
CvRect rect=cvBoundingRect(contour2); //得到边界的矩形框
cvDrawContours(pTmp,contour2,CV_RGB(255,255,255),CV_RGB(255,255,255),-1, 1, 8);
cvRectangle(pTmp,cvPoint(rect.x,rect.y),cvPoint(rect.x+rect.width,rect.y+rect.height),CV_RGB(255,255,255),3,CV_AA,0);
int n=contour->total;
CvRect r=((CvContour*)contour2)->rect;
CvPoint2D32f center;
cvMoments(cvGetSubRect(pTmp,&mat,r),&m,0);
M00=cvGetSpatialMoment(&m,0,0);
center.x=cvGetSpatialMoment(&m,1,0)/M00; //重心为(centerx.+r.x,center.y+r.y)
center.y=cvGetSpatialMoment(&m,0,1)/M00;
double nHalfW,nHalfH;
nHalfW=center.x<(r.width-center.x)?center.x:(r.width-center.x);
nHalfH=center.y<(r.height-center.y)?center.y:(r.height-center.y);
cvDrawContours(pTmp1,contour2,CV_RGB(255,255,255),CV_RGB(255,255,255),-1, 1, 8);
cvRectangle(pTmp1,cvPoint(center.x+r.x-nHalfW,center.y+r.y-nHalfH),cvPoint(center.x+r.x+nHalfW,center.y+r.y+nHalfH),CV_RGB(255,255,255),3,CV_AA,0);
cvRectangle(imgToShow,cvPoint(center.x+r.x-nHalfW,center.y+r.y-nHalfH),cvPoint(center.x+r.x+nHalfW,center.y+r.y+nHalfH),CV_RGB(107,180,229),3,CV_AA,0);
}
cvReleaseImage(&pTmp);
cvReleaseImage(&pTmp1);
} 展开
{
cvFindContours(dst,storage,&contour,sizeof(CvContour),CV_RETR_EXTERNAL,CV_CHAIN_APPROX_SIMPLE);
if(contour)
cvDrawContours(dst,contour,cvScalarAll(255),cvScalarAll(255),100); //画轮廓
//cvShowImage("Contours",dst);
CvMoments m;
CvMat mat;
double M00;
CvSeq* contour2=contour;
IplImage *pTmp=cvCreateImage(cvGetSize(dst),IPL_DEPTH_8U,1);
cvSet(pTmp, cvScalar(0,0,0),0);
IplImage *pTmp1=cvCreateImage(cvGetSize(dst),IPL_DEPTH_8U,1);
cvSet(pTmp1, cvScalar(0,0,0),0);
for(;contour2!=0;contour2=contour2->h_next)
{
CvRect rect=cvBoundingRect(contour2); //得到边界的矩形框
cvDrawContours(pTmp,contour2,CV_RGB(255,255,255),CV_RGB(255,255,255),-1, 1, 8);
cvRectangle(pTmp,cvPoint(rect.x,rect.y),cvPoint(rect.x+rect.width,rect.y+rect.height),CV_RGB(255,255,255),3,CV_AA,0);
int n=contour->total;
CvRect r=((CvContour*)contour2)->rect;
CvPoint2D32f center;
cvMoments(cvGetSubRect(pTmp,&mat,r),&m,0);
M00=cvGetSpatialMoment(&m,0,0);
center.x=cvGetSpatialMoment(&m,1,0)/M00; //重心为(centerx.+r.x,center.y+r.y)
center.y=cvGetSpatialMoment(&m,0,1)/M00;
double nHalfW,nHalfH;
nHalfW=center.x<(r.width-center.x)?center.x:(r.width-center.x);
nHalfH=center.y<(r.height-center.y)?center.y:(r.height-center.y);
cvDrawContours(pTmp1,contour2,CV_RGB(255,255,255),CV_RGB(255,255,255),-1, 1, 8);
cvRectangle(pTmp1,cvPoint(center.x+r.x-nHalfW,center.y+r.y-nHalfH),cvPoint(center.x+r.x+nHalfW,center.y+r.y+nHalfH),CV_RGB(255,255,255),3,CV_AA,0);
cvRectangle(imgToShow,cvPoint(center.x+r.x-nHalfW,center.y+r.y-nHalfH),cvPoint(center.x+r.x+nHalfW,center.y+r.y+nHalfH),CV_RGB(107,180,229),3,CV_AA,0);
}
cvReleaseImage(&pTmp);
cvReleaseImage(&pTmp1);
} 展开
东莞大凡
2024-08-07 广告
2024-08-07 广告
OpenCV标定板是东莞市大凡光学科技有限公司在相机标定中常用的工具。它通常由黑白格点按一定规则排列在平面上组成,如棋盘格或圆形格等。在相机标定时,将标定板置于不同位置和姿态下拍摄图像,利用OpenCV库中的函数检测标定板上的角点或圆心,进...
点击进入详情页
本回答由东莞大凡提供
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询