检测直线(Line)并按倾斜角度进行校正、画线、画延长线

2018-03-23 10:53:16 203

1、检测直线(Line)并按倾斜角度进行校正

[html] view plain copy
  1. int main(int argc, char** argv)  

  2. {  

  3.     double degree;  

  4.     double sum=0.0;  

  5.     int m=0;  

  6.     const char* filename = "c:\2.jpg";  

  7.     IplImage* src = cvLoadImage( filename, 0 );  

  8.     IplImage* dst;  

  9.     IplImage* color_dst;  

  10.     CvMemStorage* storage = cvCreateMemStorage(0);  

  11.     CvSeq* lines = 0;  

  12.     int i;  

  13.     if( !src )  

  14.     {  

  15.         return -1;  

  16.     }  

  17.     dst = cvCreateImage( cvGetSize(src), 8, 1 );  

  18.     color_dst = cvCreateImage( cvGetSize(src), 8, 3 );  

  19.     cvCanny( src, dst, 50, 200, 3 );  

  20.     cvCvtColor( dst, color_dst, CV_GRAY2BGR );  

  21.   

  22.     lines = cvHoughLines2(dst, storage, CV_HOUGH_PROBABILISTIC, 1, CV_PI/180, 200, 50, 10);  

  23.     double k;  

  24.   

  25.     for (i = 0; i < lines->total/*/20*/; i++)  

  26.     {  

  27.         CvPoint* line = (CvPoint*)cvGetSeqElem(lines, i);  

  28.         if (fabs(line[0].x-line[1].x)>src->width/10 &&  fabs(line[0].y-line[1].y)<4)  //只检测近似水平的线,并且长度不能太短  

  29.         {  

  30.            cvLine(color_dst, line[0], line[1], CV_RGB(255, 0, 0), 3, CV_AA, 0);  

  31.               

  32.            double k=(line[0].y-line[1].y)/(line[0].x-line[1].x+0.000001);//不加0.000001 会变成曲线,斜率可能为0,即e.x-s.x可能为0  

  33.            degree=atan(k)*180/3.1415926;  

  34.            sum+=degree;  

  35.            m++;  

  36.           // break;  

  37.         }  

  38.   

  39.     }  

  40.     cvSaveImage("c:\hualine19.jpg",color_dst);  

  41.       

  42.     //新加的根据角度进行调整  

  43.       

  44.     degree=sum/m;  

  45.   

  46.     int center[2]={0,0};      

  47.       

  48.     center[0]=src->width/2;//这两句可以设置旋转中心的坐标  

  49.     center[1]=src->height/2;  

  50. //  cvSaveImage("c:\srccopy000.jpg",src);  

  51.       

  52.     myRotate(src,0,0, /*- */degree,center);//对原图像按倾斜角度进行旋转  

  53.           

  54.   

  55.     cvSaveImage("c:\line.jpg",src);  

  56. return 0;  

  57. }  


2、根据二值图像扫描每一行,如果一行全是白点,就画线

[html] view plain copy
  1. // Line.cpp : Defines the entry point for the console application.  

  2. //  

  3.   

  4. #include "stdafx.h"  

  5. #include "cv.h"  

  6. #include "highgui.h"  

  7.   

  8.   

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

  10. {  

  11.     IplImage * picture , * gray, *dst;  

  12.     picture=cvLoadImage("pi3.jpg",1);  

  13.     gray = cvCreateImage(cvSize(picture->width,picture->height),IPL_DEPTH_8U,1);  

  14.     cvCvtColor(picture,gray,CV_BGR2GRAY);  

  15.     dst = cvCreateImage(cvSize(picture->width,picture->height),IPL_DEPTH_8U,1);  

  16.   

  17.       

  18.     int w=picture->width;  

  19.     int h=picture->height;     

  20.       

  21.     cvThreshold(gray,dst,220,255,CV_THRESH_BINARY);   

  22.     cvSaveImage("dst.jpg",dst);  

  23.       

  24.     int i,j;  

  25.       

  26.     int flag1,flag2,flag3;  

  27.     flag1=0;  

  28.     flag2=0;  

  29.     flag3=1;  

  30.   

  31.     int LineBytes=(w*8+31)/32*4;  

  32.     int m=0;  

  33.     uchar *pdata=(uchar *)dst->imageData;  

  34.     for(j=0;j<h;j++)  

  35.     {  

  36.         for(i=0;i<w;i++)  

  37.         {     

  38.             if( flag1 == 0 && flag2 ==0)  

  39.             {  

  40.                 if(pdata[LineBytes*j +i] == 0)  

  41.                 {                 

  42.                     if (m%2==0 )  

  43.                     {  

  44.                         cvLine(picture,cvPoint(0,j-1),cvPoint(w,j-1),cvScalar(0,0,0),1);  

  45.                         m++;  

  46.                     }  

  47.                     //cvSaveImage("pi1.jpg",picture);  

  48.                     flag2=1;  

  49.                     flag1=1;  

  50.                       

  51.                     break;  

  52.                       

  53.                 }  

  54.             }  

  55.             else  

  56.             {  

  57.                 if (pdata[LineBytes*j +i] == 255)  

  58.                 {  

  59.                     flag2=0;  

  60.                 //  m++;  

  61.                 }  

  62.                 else  

  63.                 {  

  64.                     flag2=1;  

  65.                     break;  

  66.                 }  

  67.             }  

  68.         }  

  69.           

  70.         if (flag1 ==1 && flag2 ==0)  

  71.         {  

  72.             flag1 = 0;  

  73.             flag2 = 0;  

  74.             //m++;  

  75.             if (m%2)  

  76.             {  

  77.                 cvLine(picture,cvPoint(0,j+1),cvPoint(w,j+1),cvScalar(0,0,255),1);  

  78.                 //m++;  

  79.   

  80.             }  

  81.             j=j+4;  

  82.               

  83.         }  

  84.     }     

  85.       

  86.     cvSaveImage("pi2.jpg",picture);  

  87.       

  88.     return 0;  

  89. }  


传递安全之声,从心感动您!

3、画直线的延长线

[html] view plain copy
  1. // Line.cpp : Defines the entry point for the console application.  

  2. //  

  3.   

  4. #include "stdafx.h"  

  5. #include "cv.h"  

  6. #include "highgui.h"  

  7.   

  8. void OnDrawDotline(CvPoint s, CvPoint e,IplImage *workimg)  

  9. {  

  10.     CvPoint pa,pb;  

  11.       

  12.     double k=(s.y-e.y)/(s.x-e.x+0.000001);//不加0.000001 会变成曲线,斜率可能为0,即e.x-s.x可能为0  

  13.       

  14.     double h=workimg->height,w=workimg->width;      

  15.       

  16.     pa.x=w;  

  17.     pa.y=s.y+k*(w-s.x);   

  18.       

  19.     cvLine(workimg,e,pa,CV_RGB(0,255,255), 2, CV_AA, 0 );   //向右画线  

  20.       

  21.       

  22.     pb.y=e.y-k*e.x;  

  23.     pb.x=0;  

  24.       

  25.     cvLine(workimg,pb,s,CV_RGB(0,0,255), 2, CV_AA, 0 ); //向左画线  

  26.   

  27. }  

  28.   

  29.   

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

  31. {  

  32.     IplImage * picture , * gray, *dst;  

  33.     picture=cvLoadImage("3.jpg",1);  

  34.     gray = cvCreateImage(cvSize(picture->width,picture->height),IPL_DEPTH_8U,1);  

  35.     cvCvtColor(picture,gray,CV_BGR2GRAY);  

  36.     dst = cvCreateImage(cvSize(picture->width,picture->height),IPL_DEPTH_8U,1);  

  37.   

  38.       

  39.     int w=picture->width;  

  40.     int h=picture->height;     

  41.       

  42.     cvThreshold(gray,dst,220,255,CV_THRESH_BINARY);   

  43.     cvSaveImage("dst.jpg",dst);  

  44.       

  45.     cvLine(picture,cvPoint(20,50),cvPoint(60,40),CV_RGB(255,0,0),3);  

  46.   

  47.     OnDrawDotline(cvPoint(20,50), cvPoint(60,40),picture);  

  48.   

  49.     cvSaveImage("pi2.jpg",picture);  

  50.       

  51.     return 0;  

  52. }  

传递安全之声,从心感动您!

原创文章 https://blog.csdn.net/liulina603/article/details/41309169

电话咨询
邮件咨询
在线地图
QQ客服