首页 > 编程知识 正文

求三维空间两平行直线之间的距离,两条平行线之间的距离处处相等

时间:2023-05-03 15:51:46 阅读:175563 作者:1773

代码来自www.opencvchina.com

# include ' cv.h ' # include ' highgui.h ' # include ' CX core.h ' # include stdlib.h # include stdio.h # ifndeflineflinesdion //获取图像上的轮廓坐标voidgetcontourspoints (iplimage * src,CvMat** LinePoints ); #endif #include 'LinesDis.h'//针对输入图像输入voidthinimage(iplimage*src,IplImage* dst,int iterations ) cv size=ccv cv cv cv //将一个数组复制到另一个数组的int n=0,i=0,j=0 for(n=0; niterations; n ) {IplImage* t_image; t_image=cvcloneimage(dst; for(I=0; isize.height; I ) for(intj=0; jsize.width; j ) if(cv_image_elem ) t_image,byte,I,j )==1) { int ap=0; INTP2=(I==0)? 0:cv_image_elem(t_image,byte,i-1,j ); INTP3=(I==0||j==size.width-1 )? 0:cv_image_elem(t_image,byte,i-1,j 1 ); if(P2==0P3==1) { ap; }intP4=(j==size.width-1 )? 0:cv_image_elem(t_image,byte,I,j 1 ); if(P3==0P4==1) { ap; } intp5=(I==size.height-1|| j==size.width-1 )? 0:cv_image_elem(t_image,byte,i 1,j 1 ); if(P4==0P5==1) { ap; }intP6=(I==size.height-1 )? 0:cv_image_elem(t_image,byte,i 1,j ); if(P5==0P6==1) { ap; } intp7=(I==size.height-1|||j==0)? 0:cv_image_elem(t_image,byte,i 1,j-1 ); if(P6==0P7==1) { ap; (intp8=) j==0)? 0:cv_image_elem(t_image,byte,I,j-1 ); if(P7==0P8==1) { ap; (intp9=(I==0||j==0)? 0:cv_image_elem(t_image,byte,i-1,j-1 ); if(P8==0P9==1) { ap; (if ) p9==0p2==1) AP; (if ) ) P2P3P4P6P7P9)1) P2P3P4P5P6P8P9)7) if ) AP==1) if ) P2*P4*P8==0) if ) p2 * P6 * P8==0} } } 将二值图像变换为灰度以显示i=0; j=0; size=cvgetsize(dst ); for(I=0; isize.height; I ) for(j=0; jsize.width; j ) if(cv_image_elem(dst,uchar,I,j )==1) cv _ image _ elem (dst,uchar,I,j ) )=255; }

else { CV_IMAGE_ELEM(dst,uchar,i,j) = 0; } } }}//获取图像上的轮廓 坐标 void GetContoursPoints(IplImage* src , CvMat** LinePoints){CvMemStorage* storage = cvCreateMemStorage(0);CvSeq* contour = 0;IplImage* binary_image = cvCreateImage(cvGetSize(src),8,1);cvCopy(src,binary_image);cvFindContours(binary_image , storage , &contour , sizeof(CvContour) , CV_RETR_CCOMP,CV_CHAIN_APPROX_NONE,cvPoint(0,0) );//#define SHOWCON#ifdef SHOWCONIplImage* contours;contours = cvCreateImage(cvGetSize(src),8,1);cvZero(contours);cvNamedWindow("con");#endif int LinesCount = 0;for(;contour!=0 ; contour=contour->h_next){if(contour->total < 20 )continue;LinePoints[LinesCount] = cvCreateMat(contour->total/8,1,CV_32FC2);for(int i=0; i<contour->total/8 ; i++){CvPoint * pt = (CvPoint*)cvGetSeqElem(contour, i); // 读出第i个点。cvSet2D(LinePoints[LinesCount] , i,0,cvScalar(pt->x,src->height - pt->y,0,0));//cvSetReal2D(contours , pt->y , pt->x , 255.0);#ifdef SHOWCONCvScalar val = cvGet2D(LinePoints[LinesCount] , i, 0);cvSetReal2D(contours , val.val[1] ,val.val[0],255);cvShowImage("con" ,contours);cvWaitKey(0);#endif}LinesCount++;}#ifdef SHOWCONfor(int i=0;i<LinesCount;i++){for(int y=0;y<LinePoints[i]->rows;y++){CvScalar pt = cvGet2D(LinePoints[i] , y, 0);cvSetReal2D(contours , pt.val[1] ,pt.val[0],255);printf("(%f,%f) ",pt.val[0],pt.val[1]);}}cvShowImage("con" ,contours);cvWaitKey(0);#endifcvReleaseMemStorage(&storage);cvReleaseImage(&binary_image);}

 

// ComputeLinesDis.cpp : Defines the entry point for the console application.//#include "LinesDis.h"int main(int argc, char* argv[]){//对两条直线进行细化IplImage *pSrc = NULL,*pDst = NULL,*pTmp = NULL;//传入一个灰度图像,从文件中读取图像pSrc = cvLoadImage("1.png",CV_LOAD_IMAGE_GRAYSCALE);if(!pSrc){return 0;}pTmp = cvCreateImage(cvGetSize(pSrc),pSrc->depth , pSrc->nChannels); pDst = cvCreateImage(cvGetSize(pSrc),pSrc->depth,pSrc->nChannels);cvZero(pDst);//初始化cvThreshold(pSrc,pTmp,128,1,CV_THRESH_BINARY_INV);//做二值处理,将图像转换成0,1 //第一步 对图像中的直线进行细化ThinImage(pTmp,pDst,80);#define SHOWRESULT#ifdef SHOWRESULTcvNamedWindow("src",1);//创建窗口cvNamedWindow("dst",1);cvShowImage("src",pSrc);cvShowImage("dst",pDst);#endif//第二步 提取直线的轮廓坐标CvMat*LinesPoints[2];LinesPoints[0]=0;LinesPoints[1]=0;GetContoursPoints(pDst,LinesPoints);//#define SHOWCONT#ifdef SHOWCONTIplImage* contours = cvCreateImage(cvGetSize(pDst),8,1);cvZero(contours);cvNamedWindow("Mcon");for(int i=0;i<2;i++){for(int y=0;y<LinesPoints[i]->rows;y++){CvScalar pt = cvGet2D(LinesPoints[i] , y, 0);cvSetReal2D(contours , pt.val[1] ,pt.val[0],255);cvShowImage("Mcon" ,contours);}}cvWaitKey(0);#endif//第三步 对轮廓上的坐标进行直线拟合 计算直线方程 By = Ax + bfloat params[4] , k[2] , b[2] , A , B;cvFitLine(LinesPoints[0] , CV_DIST_L2,1,0.001,0.001,params);k[0] = params[1]/params[0];b[0] = params[3] - k[0]*params[2];A = k[0];B = 1;printf("y=%f*x+%f n",k[0]*180.0/3.1415,b[0]);cvFitLine(LinesPoints[1] , CV_DIST_L2,1,0.001,0.001,params);k[1] = params[1]/params[0];b[1] = params[3] - k[0]*params[2];printf("y=%f*x+%f n",k[1]*180.0/3.1415,b[1]);//第四部 计算两条直线之间的距离 公式是: |b1-b0| / sqrt(A*A + B*B)float dis = abs(b[1]-b[0])/sqrt(A*A + B*B);printf("dis is %f n" , dis); // 释放内存 cvReleaseImage(&pSrc);cvReleaseImage(&pDst);cvReleaseImage(&pTmp);if(LinesPoints[0])cvReleaseMat(&LinesPoints[0]);if(LinesPoints[1]);cvReleaseMat(&LinesPoints[1]);cvWaitKey(0);return 0;}

 

 

 

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。