美文网首页iOS 图像处理iOS图像处理iOS Developer
OpenCV学习_ios平台(三):基本绘图

OpenCV学习_ios平台(三):基本绘图

作者: 爱抽烟的芭比 | 来源:发表于2017-03-18 23:37 被阅读224次

    demo地址:
    https://github.com/GoldenRocking/OpenCV_iOS
    一.直线:

    void cvLine{
         CvArr*           array,              //一般为一个图像类型的指针IplImage*
         CvPoint           pt1,               //起始点坐标
         CvPoint           pt2,               //结束点坐标
         CvScalar         color,              //颜色
         int            thickness  = 1,         //  线的粗细(像素)
         int            connectivity  = 8      //反走样模式         
    }
    

    二.矩形:

    void cvRectangle{
         CvArr*           array,            
         CvPoint           pt1,               //顶点坐标1
         CvPoint           pt2,               //顶点坐标2
         CvScalar         color,              //颜色
         int            thickness  = 1,       //  线的粗细(像素)      
    }
    

    三.圆:

    void cvCircle{
         CvArr*           array,            
         CvPoint           center,             //圆心
         int               radius,             //半径
         CvScalar          color,              //颜色
         int            thickness  = 1,        //线的粗细(像素) 
         int            connectivity  = 8      //反走样模式         
    }
    

    四.椭圆:

    void cvEllipse{
         CvArr*             array,            
         CvPoint            center,              //圆心
         CVSize              axes,               //椭圆的长短半轴长度
         double              angle,              //偏离主轴的角度
         double             start_angle,         //弧度开始位置的角度
         double             end_angle,           //弧度结束位置的角度
         CvScalar            color,              //颜色
         int               thickness  = 1,       //线的粗细(像素) 
         int               connectivity  = 8      //反走样模式         
    }
    

    五.椭圆(外接矩形):

    void cvEllipse{
         CvArr*             array,            
        CvBox2D             box,
         CvScalar           color,              //颜色
         int               thickness  = 1,       //线的粗细(像素) 
         int               connectivity  = 8,      //反走样模式  
         int               shift = 0       
    }
    
    //CvBox2D的数据结构:
    tydef struct {
          CvPoint2D32f       center;
          CvSize2D32f         size;
          float               angle;
    }  CvBox2D
    

    六.多边形

    void cvFillPoly{
          CvArr*               img,
          CvPoint**            pst,
          int*                 npts,
          int                 contours,
          CvScallar            color,
          int                 line_type  =  8
    }
    
    void cvFillConvexPoly{
          CvArr*               img,
          CvPoint*             pst,
          int                  npts,
          CvScallar            color,
          int                 line_type  =  8
    }
    
    void cvPolyLine{
          CvArr*                img,
          CvPoint**             pst,
          int*                  npts,
          int                   contours,
          int                   is_closed, 
          CvScallar             color,
          int                   thickness = 1,
          int                   line_type  =  8
    }
    

    下面我们根据基本绘图函数,来绘制两张图像。

    先创建一个绘图的工具:

    #ifndef drawTool_hpp
    #define drawTool_hpp
    
    #include <stdio.h>
    using namespace cv;
    void MyEllipse(Mat img,double angle,int w);
    void MyFilledCircle(Mat img,cv::Point center);
    void MyPolygon( Mat img,int w);
    void MyLine( Mat img, cv::Point start, cv::Point end );
    
    #endif /* drawTool_hpp */
    
    
    #include "drawTool.hpp"
    
    
    void MyEllipse(Mat img,double angle,int w)
    {
        int thickness = 2;
        int lineType = 8;
        
        ellipse(img,
                cv::Point(w/2.0,w/2.0),
                cv::Size(w/4.0,w/16.0),
                angle,
                0,
                360,
                Scalar(255,0,0),
                thickness,
                lineType
                );
    }
    
    void MyFilledCircle(Mat img,cv::Point center)
    {
        int thickness = -1;
        int lineType = 8;
        
        cv::circle( img,
                   center,
                   300/32.0,
                   cv:: Scalar( 0, 0, 255 ),
                   thickness,
                   lineType );
    }
    
    void MyPolygon( Mat img,int w )
    {
        int lineType = 8;
        
        /** 创建一些点 */
        cv::Point rook_points[1][20];
        rook_points[0][0] = cv::Point( w/4.0, 7*w/8.0 );
        rook_points[0][1] = cv::Point( 3*w/4.0, 7*w/8.0 );
        rook_points[0][2] = cv::Point( 3*w/4.0, 13*w/16.0 );
        rook_points[0][3] = cv::Point( 11*w/16.0, 13*w/16.0 );
        rook_points[0][4] = cv::Point( 19*w/32.0, 3*w/8.0 );
        rook_points[0][5] = cv::Point( 3*w/4.0, 3*w/8.0 );
        rook_points[0][6] = cv::Point( 3*w/4.0, w/8.0 );
        rook_points[0][7] = cv::Point( 26*w/40.0, w/8.0 );
        rook_points[0][8] = cv::Point( 26*w/40.0, w/4.0 );
        rook_points[0][9] = cv::Point( 22*w/40.0, w/4.0 );
        rook_points[0][10] = cv::Point( 22*w/40.0, w/8.0 );
        rook_points[0][11] = cv::Point( 18*w/40.0, w/8.0 );
        rook_points[0][12] = cv::Point( 18*w/40.0, w/4.0 );
        rook_points[0][13] = cv::Point( 14*w/40.0, w/4.0 );
        rook_points[0][14] = cv::Point( 14*w/40.0, w/8.0 );
        rook_points[0][15] = cv::Point( w/4.0, w/8.0 );
        rook_points[0][16] = cv::Point( w/4.0, 3*w/8.0 );
        rook_points[0][17] = cv::Point( 13*w/32.0, 3*w/8.0 );
        rook_points[0][18] = cv::Point( 5*w/16.0, 13*w/16.0 );
        rook_points[0][19] = cv::Point( w/4.0, 13*w/16.0) ;
        
        const cv::Point* ppt[1] = { rook_points[0] };
        int npt[] = { 20 };
        
        fillPoly( img,
                 ppt,
                 npt,
                 1,
                 Scalar( 255, 255, 255 ),
                 lineType );
    }
    
    void MyLine( Mat img, cv::Point start, cv::Point end )
    {
        int thickness = 2;
        int lineType = 8;
        line( img,
             start,
             end,
             Scalar( 0, 0, 0 ),
             thickness,
             lineType );
    }
    

    首先,我们绘制一个基本的原子::

    
    const int w = 300;
    /// 创建空全黑像素的空图像
        Mat atom_image = Mat::zeros(w, w, CV_8UC3);
    
    /// 1. 画一个简单的原子。
    
       
        /// 1.a. 创建椭圆
       MyEllipse(atom_image, 90,w);
        MyEllipse(atom_image, 0,w);
        MyEllipse(atom_image, 45,w);
        MyEllipse(atom_image, -45,w);
    
        /// 1.b. 创建圆
        MyFilledCircle( atom_image, cv::Point( w/2.0, w/2.0) );
        
        UIImageView *img = [[UIImageView alloc]init];
        img.frame = CGRectMake(10, 0, w, w);
        [img setImage:[cvprocess UIImageFromCVMat:atom_image ]];
        
        [self.view addSubview:img];
        
    

    然后,我们再绘制一个赌棍:

    调用:

    /// 创建空全黑像素的空图像
     
        Mat rook_image = Mat::zeros(w, w, CV_8UC3);
       /// 2. 画一个赌棍
        
        /// 2.a. 创建一个凸多边形
        MyPolygon( rook_image ,w);
        
        /// 2.b. 创建矩形
        rectangle( rook_image,
                  cv::Point( 0, 7*w/8.0 ),
                  cv::Point( w, w),
                  Scalar( 0, 255, 255 ),
                  -1,
                  8 );
        
        /// 2.c. 画几条直线
        MyLine( rook_image, cv::Point( 0, 15*w/16 ), cv::Point( w, 15*w/16 ) );
        MyLine( rook_image, cv::Point( w/4, 7*w/8 ), cv::Point( w/4, w ) );
        MyLine( rook_image, cv::Point( w/2, 7*w/8 ), cv::Point( w/2, w ) );
        MyLine( rook_image, cv::Point( 3*w/4, 7*w/8 ), cv::Point( 3*w/4, w ) );
        
        UIImageView *img2 = [[UIImageView alloc]init];
        img2.frame = CGRectMake(10, 320, w, w);
        [img2 setImage:[cvprocess UIImageFromCVMat:rook_image ]];
        
        [self.view addSubview:img2];
    
    

    运行结果:

    AA79CD99-C49D-4EB4-8540-913F225197A2.png

    demo地址:
    https://github.com/GoldenRocking/OpenCV_iOS

    相关文章

      网友评论

        本文标题:OpenCV学习_ios平台(三):基本绘图

        本文链接:https://www.haomeiwen.com/subject/cyspnttx.html