opencv常用函数备忘

系统 1912 0

 //显示图片

      
        1
      
       IplImage * src = cvLoadImage(
      
        "
      
      
        xx.JPG
      
      
        "
      
      
        );


      
      
        2
      
       cvNamedWindow(
      
        "
      
      
        show_image
      
      
        "
      
      ,
      
        1
      
      
        );


      
      
        3
      
       cvShowImage(
      
        "
      
      
        show_image
      
      
        "
      
      
        ,src);


      
      
        4
      
       cvWaitKey(
      
        0
      
      
        );


      
      
        5
      
       cvReleaseImage(&
      
        str);


      
      
        6
      
       cvDestroyWindow(
      
        "
      
      
        show_image
      
      
        "
      
      );
    

 

 //色彩空间转换,转换类型为CV_BGR2GRAY

      
        1
      
       IplImage* dst = cvCreateImage(cvGetSize(src),src->depth,
      
        1
      
      
        );


      
      
        2
      
      
        cvCvtColor(src,dst,CV_BGR2GRAY);


      
      
        3
      
    

 //开、闭、膨胀、腐蚀操作

      
        1
      
       IplConvKernel* element = cvCreateStructuringElementEx(
      
        3
      
      ,
      
        3
      
      ,
      
        0
      
      ,
      
        0
      
      , CV_SHAPE_ELLIPSE,
      
        0
      
      );
      
        //
      
      
        创建3*3椭圆结构元素 
      
      
        2
      
       IplImage * temp=cvCreateImage(cvGetSize(pGrayImg), IPL_DEPTH_8U, 
      
        1
      
      
         );


      
      
        3
      
       cvMorphologyEx(pGrayImg,pGrayImg, temp, element,CV_MOP_CLOSE,
      
        1
      
      );
      
        //
      
      
        闭操作 先膨胀再腐蚀
      
      
        4
      
       cvMorphologyEx(pGrayImg,pGrayImg, temp, element,CV_MOP_OPEN,
      
        1
      
      );
      
        //
      
      
        开操作 先腐蚀再膨胀
      
      
        5
      
       cvReleaseStructuringElement(&
      
        element);


      
      
        6
      
       cvReleaseImage(&
      
        temp);


      
      
        7
      
       cvDilate(  pGrayImg, pGrayImg, NULL, 
      
        1
      
      );
      
        //
      
      
        膨胀
      
      
        8
      
       cvErode( pGrayImg, pGrayImg, NULL, 
      
        1
      
      );
      
        //
      
      
        腐蚀
      
    

 

//边缘检测

      cvCanny(pGrayImg,pGrayImg, 
      
        200
      
      , 
      
        220
      
      , 
      
        3
      
       );
      
        //
      
      
        边缘检测  
      
    

 

 

 

//二值图像中检索轮廓

      
        1
      
      
        int
      
       cvFindContours( CvArr* image, CvMemStorage* storage, CvSeq** first_contour,
      
        int
      
       header_size=
      
        sizeof
      
      (CvContour),
      
        int
      
       mode=CV_RETR_LIST,
      
        int
      
       method=CV_CHAIN_APPROX_SIMPLE, CvPoint offset=cvPoint(
      
        0
      
      ,
      
        0
      
      ) ); 
    

http://blog.csdn.net/augusdi/article/details/9000893

 

//二值图像中找圆、椭圆拟合

      
         1
      
      
        //
      
      
        椭圆拟合
      
      
         2
      
      
        void
      
       my_FitEllipse(CvSeq *pContour, CvBox2D *
      
        box_rect)


      
      
         3
      
      
        {


      
      
         4
      
           CvMat*p=cvCreateMat(
      
        1
      
      ,pContour->
      
        total,CV_32FC2);


      
      
         5
      
           CvPoint2D32f *p_temp=(CvPoint2D32f*)(p->
      
        data.fl);


      
      
         6
      
      
         7
      
      
        for
      
      (
      
        int
      
       i=
      
        0
      
      ;i<pContour->total;i++
      
        )


      
      
         8
      
      
            {


      
      
         9
      
               CvPoint*p0=(CvPoint*
      
        )cvGetSeqElem(pContour,i);


      
      
        10
      
               *p_temp=cvPointTo32f(*
      
        p0);


      
      
        11
      
               p_temp++
      
        ; 


      
      
        12
      
      
            }


      
      
        13
      
           *box_rect  =
      
         cvFitEllipse2(p);


      
      
        14
      
           cvReleaseMat(&
      
        p);


      
      
        15
      
      
        }


      
      
        16
      
      
        17
      
      
        /*
      
      
        18
      
      
        *功能描述:


      
      
        19
      
      
        *   二值图像中找圆形


      
      
        20
      
      
        *输入:


      
      
        21
      
      
        *    pStrimg - 原图


      
      
        22
      
      
        *    vCircle - 圆集合


      
      
        23
      
      
        */
      
      
        24
      
      
        void
      
       findCircle(IplImage *pStrImg, vector<Ccircle> &
      
        vCircle)


      
      
        25
      
      
        {


      
      
        26
      
           CvSeq *pContour =
      
         NULL;   


      
      
        27
      
           CvSeq *pConInner =
      
         NULL;   


      
      
        28
      
           CvMemStorage *pStorage =
      
         NULL;  


      
      
        29
      
           IplImage *pTmpImg = cvCreateImage(cvGetSize(pStrImg), pStrImg->depth, pStrImg->
      
        nChannels);


      
      
        30
      
      
            cvCopy(pStrImg, pTmpImg);


      
      
        31
      
      
        32
      
      
        //
      
      
         查找所有轮廓   
      
      
        33
      
           pStorage = cvCreateMemStorage(
      
        0
      
      
        );   


      
      
        34
      
           cvFindContours(pTmpImg, pStorage, &pContour, 
      
        sizeof
      
      
        (CvContour), CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE);


      
      
        35
      
      
        36
      
      
        int
      
       wai = 
      
        0
      
      
        ;  


      
      
        37
      
      
        int
      
       nei = 
      
        0
      
      
        ;  


      
      
        38
      
      
        for
      
       (; pContour != NULL; pContour = pContour->
      
        h_next)   


      
      
        39
      
      
            {   


      
      
        40
      
               wai++
      
        ;  


      
      
        41
      
      
        //
      
      
         内轮廓循环   
      
      
        42
      
      
        for
      
       (pConInner = pContour->v_next; pConInner != NULL; pConInner = pConInner->
      
        h_next)   


      
      
        43
      
      
                {   


      
      
        44
      
                   nei++
      
        ;  


      
      
        45
      
      
                }  


      
      
        46
      
      
        if
      
      (pContour->total < 
      
        5
      
      
        )


      
      
        47
      
      
                {


      
      
        48
      
      
        //
      
      
        TRACE("Number of points should be >= 5\n");
      
      
        49
      
      
        continue
      
      
        ;


      
      
        50
      
      
                }


      
      
        51
      
      
                CvBox2D box_rect;


      
      
        52
      
               my_FitEllipse(pContour, &
      
        box_rect);


      
      
        53
      
      
        if
      
      (fabs(box_rect.size.width - box_rect.size.height) < 
      
        1
      
      ){
      
        //
      
      
        椭圆长轴,短轴长度相近时判断为圆
      
      
        54
      
      
                    Ccircle circle;


      
      
        55
      
                   circle.center.x =
      
         box_rect.center.x;


      
      
        56
      
                   circle.center.y =
      
         box_rect.center.y;


      
      
        57
      
                   circle.r = (box_rect.size.height + box_rect.size.width) / 
      
        4
      
      
        ;


      
      
        58
      
      
                    vCircle.push_back(circle);


      
      
        59
      
      
                }


      
      
        60
      
      
        //
      
      
        cvEllipseBox(pBinary, box_rect, CV_RGB(255, 255, 255));
      
      
        61
      
               TRACE(
      
        "
      
      
        center:%f,%f, w:%f, h:%f\n
      
      
        "
      
      
        , box_rect.center.x, box_rect.center.y, box_rect.size.width, box_rect.size.height);


      
      
        62
      
      
        63
      
      
        //
      
      
        CvRect rect = cvBoundingRect(pContour,0);


      
      
        64
      
      
        //
      
      
        cvRectangle(pBinary, cvPoint(rect.x, rect.y), cvPoint(rect.x + rect.width, rect.y + rect.height),CV_RGB(255,255, 255), 1, 8, 0);
      
      
        65
      
      
            }   


      
      
        66
      
      
        67
      
           printf(
      
        "
      
      
        wai = %d, nei = %d
      
      
        "
      
      
        , wai, nei);  


      
      
        68
      
           cvReleaseImage(&
      
        pTmpImg);


      
      
        69
      
           cvReleaseMemStorage(&
      
        pStorage);   


      
      
        70
      
           pStorage =
      
         NULL;   


      
      
        71
      
      
        72
      
       }
    

 

 

//图像上点的访问

      
         1
      
           uchar*
      
        data ;


      
      
         2
      
           uchar*
      
        ivs_data ;


      
      
         3
      
      
        //
      
      
        IplImage*ivs_image = cvCreateImage(cvGetSize(gray_image),IPL_DEPTH_8U, 1);
      
      
         4
      
      
         5
      
           ivs_height    = dst->
      
        height;


      
      
         6
      
           ivs_width     = dst->
      
        width;


      
      
         7
      
           ivs_step      = dst->widthStep/
      
        sizeof
      
      
        (uchar);


      
      
         8
      
           ivs_channel = dst->
      
        nChannels;


      
      
         9
      
           data   = (uchar*)str->
      
        imageData;


      
      
        10
      
      
        //
      
      
        printf("Processing a %d X %d image with %d channel!\n",ivs_height,ivs_width,ivs_channel);
      
      
        11
      
      
        12
      
      
        13
      
           ivs_data = (uchar*)dst->
      
        imageData;


      
      
        14
      
      
        15
      
      
        16
      
      
        //
      
      
         反色图像
      
      
        17
      
      
        for
      
      (ivs_i=
      
        0
      
      ;ivs_i<ivs_height;ivs_i++
      
        )


      
      
        18
      
      
            {


      
      
        19
      
      
        for
      
      (ivs_j=
      
        0
      
      ;ivs_j<ivs_width;ivs_j++
      
        )


      
      
        20
      
      
                {


      
      
        21
      
      
        for
      
      (ivs_k=
      
        0
      
      ;ivs_k<ivs_channel;ivs_k++
      
        )


      
      
        22
      
      
                    {


      
      
        23
      
                       ivs_data[ivs_i*ivs_step+ivs_j*ivs_channel+ivs_k]=
      
        255
      
      -data[ivs_i*ivs_step+ivs_j*ivs_channel+
      
        ivs_k];


      
      
        24
      
      
        25
      
      
        26
      
                   }
      
        //
      
      
        k
      
      
        27
      
               }
      
        //
      
      
        j
      
      
        28
      
           }
      
        //
      
      
        i
      
    

 

opencv常用函数备忘


更多文章、技术交流、商务合作、联系博主

微信扫码或搜索:z360901061

微信扫一扫加我为好友

QQ号联系: 360901061

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描下面二维码支持博主2元、5元、10元、20元等您想捐的金额吧,狠狠点击下面给点支持吧,站长非常感激您!手机微信长按不能支付解决办法:请将微信支付二维码保存到相册,切换到微信,然后点击微信右上角扫一扫功能,选择支付二维码完成支付。

【本文对您有帮助就好】

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描上面二维码支持博主2元、5元、10元、自定义金额等您想捐的金额吧,站长会非常 感谢您的哦!!!

发表我的评论
最新评论 总共0条评论