• Hough变换,找直线


    /* This is a standalone program. Pass an image name as a first parameter
       of the program.Switch between standard and probabilistic Hough transform
       by changing "#if 1" to "#if 0" and back 
    */

    #include 
    <cv.h>
    #include 
    <highgui.h>
    #include 
    <math.h>

    int main(int argc, char** argv)
    {
        IplImage
    * src;
        src
    =cvLoadImage("1.bmp",-1);
       
            IplImage
    * dst = cvCreateImage( cvGetSize(src), 81 );
            IplImage
    * color_dst = cvCreateImage( cvGetSize(src), 83 );
            CvMemStorage
    * storage = cvCreateMemStorage(0);
            CvSeq
    * lines = 0;
            
    int i;
        IplImage
    * src1=cvCreateImage(cvSize(src->width,src->height),IPL_DEPTH_8U,1);

        cvCvtColor(src, src1, CV_BGR2GRAY);  
    //    cvCopy(src,src1);
            cvCanny( src1, dst, 502003 );

            cvCvtColor( dst, color_dst, CV_GRAY2BGR );
    #if 0
            lines 
    = cvHoughLines2( dst, storage, CV_HOUGH_STANDARD, 1, CV_PI/18015000 );

            
    for( i = 0; i < lines->total; i++ )
            
    {
                
    float* line = (float*)cvGetSeqElem(lines,i);
                
    float rho = line[0];
                
    float theta = line[1];
                CvPoint pt1, pt2;
                
    double a = cos(theta), b = sin(theta);
                
    if( fabs(a) < 0.001 )
                
    {
                    pt1.x 
    = pt2.x = cvRound(rho);
                    pt1.y 
    = 0;
                    pt2.y 
    = color_dst->height;
                }

                
    else if( fabs(b) < 0.001 )
                
    {
                    pt1.y 
    = pt2.y = cvRound(rho);
                    pt1.x 
    = 0;
                    pt2.x 
    = color_dst->width;
                }

                
    else
                
    {
                    pt1.x 
    = 0;
                    pt1.y 
    = cvRound(rho/b);
                    pt2.x 
    = cvRound(rho/a);
                    pt2.y 
    = 0;
                }

                cvLine( color_dst, pt1, pt2, CV_RGB(
    255,0,0), 38 );
            }

    #else
            lines 
    = cvHoughLines2( dst, storage, CV_HOUGH_PROBABILISTIC, 1, CV_PI/180803010 );
            
    for( i = 0; i < lines->total; i++ )
            
    {
                CvPoint
    * line = (CvPoint*)cvGetSeqElem(lines,i);
                cvLine( color_dst, line[
    0], line[1], CV_RGB(255,0,0), 38 );
            }

    #endif
            cvNamedWindow( 
    "Source"1 );
            cvShowImage( 
    "Source", src );

            cvNamedWindow( 
    "Hough"1 );
            cvShowImage( 
    "Hough", color_dst );

            cvWaitKey(
    0);
        
    }

  • 相关阅读:
    ADO.NET Entity Framework 基本概述
    Team Fundation Server 2010 三
    Team Fundation Server 2010 一
    ADO.NET Data Services Framework 基础概述
    Git忽略已跟踪文件的改动
    菜单
    Eschool校园网平台介绍
    学习DDD与MVC系统架构的开源项目
    学校系统需求
    Firebird embed server
  • 原文地址:https://www.cnblogs.com/wqj1212/p/1010355.html
Copyright © 2020-2023  润新知