代码来自www.opencvchina.com
#include "cv.h"
#include "highgui.h"
#include "cxcore.h"
#include <stdlib.h>
#include <stdio.h>#ifndef LINESDISHEADER#define LINESDISHEADER//对输入图像进行细化
void ThinImage(IplImage* src, IplImage* dst, int iterations=1);//获取图像上的轮廓 坐标
void GetContoursPoints(IplImage* src , CvMat** LinePoints);#endif
#include "LinesDis.h"//对输入图像进行细化
void ThinImage(IplImage* src, IplImage* dst, int iterations)
{CvSize size = cvGetSize(src);cvCopy(src, dst);//拷贝一个数组给另一个数组int n = 0,i = 0,j = 0;for(n=0; n<iterations; n++){
IplImage* t_image;t_image = cvCloneImage(dst);for(i=0; i<size.height; i++){for(int j=0; j<size.width; j++){if(CV_IMAGE_ELEM(t_image,byte,i,j)==1){int ap=0;int p2 = (i==0)?0:CV_IMAGE_ELEM(t_image,byte, i-1, j);int p3 = (i==0 || j==size.width-1)?0:CV_IMAGE_ELEM(t_image,byte, i-1, j+1);if (p2==0 && p3==1){ap++;}int p4 = (j==size.width-1)?0:CV_IMAGE_ELEM(t_image,byte,i,j+1);if(p3==0 && p4==1){ap++;}int p5 = (i==size.height-1 || j==size.width-1)?0:CV_IMAGE_ELEM(t_image,byte,i+1,j+1);if(p4==0 && p5==1){ap++;}int p6 = (i==size.height-1)?0:CV_IMAGE_ELEM(t_image,byte,i+1,j);if(p5==0 && p6==1){ap++;}int p7 = (i==size.height-1 || j==0)?0:CV_IMAGE_ELEM(t_image,byte,i+1,j-1);if(p6==0 && p7==1){ap++;}int p8 = (j==0)?0:CV_IMAGE_ELEM(t_image,byte,i,j-1);if(p7==0 && p8==1){ap++;}int p9 = (i==0 || j==0)?0:CV_IMAGE_ELEM(t_image,byte,i-1,j-1);if(p8==0 && p9==1){ap++;}if(p9==0 && p2==1){ap++;}if((p2+p3+p4+p5+p6+p7+p8+p9)>1 && (p2+p3+p4+p5+p6+p7+p8+p9)<7){if(ap==1){if(p2*p4*p8==0){if(p2*p6*p8==0){CV_IMAGE_ELEM(dst, byte,i,j)=0;}}}} }}} cvReleaseImage(&t_image);}//将二值图像转换成灰度,以便显示i = 0;j = 0;size = cvGetSize(dst);for(i=0; i<size.height; i++){for(j=0; j<size.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;
}