OpenCV study notes: drawing instructions (rectangle, circle, line, text annotation)

Environment: CentOS7
g++ (GCC) 4.8.5 20150623 (Red Hat 4.8.5-16)
$ pkg-config --modversion opencv
2.4.13

Effect picture (before and after comparison):

    

Code:

#include<stdlib.h>
#include<stdio.h>
#include<math.h>
#include<cv.h>
#include<highgui.h>
#include<iostream>
using namespace std;

int main(int argc,char**argv)
{
    /********Image Conversion********/
    /* Grayscale-color conversion of byte image */
    IplImage *img1 = cvLoadImage("wongrgb.jpg");
    // Draw a rectangle: draw a rectangle between points 1 and 2, with red edges, width
    cvRectangle(img1, cvPoint(10,10), cvPoint(70,70), cvScalar(0,255,0), 3);
    // Draw a circle: center is, radius. Circle color, width
    cvCircle(img1, cvPoint(100,100), 20, cvScalar(255,255,0), 2);
    // draw a line segment: a green line segment with a line width of 1 between (100,100) and (200,200)
    cvLine(img1, cvPoint(100,100), cvPoint(200,200), cvScalar(0,0,255), 3);

    /* Draw a set of line segments: */
    /*void cvPolyLine( CvArr* img, CvPoint** pts, int* npts,
                        int contours, int is_closed,
                        CvScalar color, int thickness=1,
                        int line_type=8, int shift=0 );
        img images.
        pts An array of vertex pointers for the polyline.
        npts An array of fixed-point numbers of polylines. It can also be considered as the size of the pts pointer array
        contours The number of segments of the polyline.
        is_closed indicates whether the polygon is closed. If closed, the function connects the start and end points.
        color The color of the polyline.
        thickness The thickness of the line.
        line_type The type of line segment. See cvLine.
        shift decimal places for vertices */
    CvPoint curve1[]={10,10, 10,100, 100,100, 100,10};
    CvPoint curve2[]={30,30, 30,130, 130,130, 130,30, 150,10};
    CvPoint* curveArr[2]={curve1, curve2};
    int nCurvePts[2]={4,5};
    int nCurves=2;
    int isCurveClosed=1;
    int lineWidth=2;
    cvPolyLine(img1,curveArr,nCurvePts,nCurves,
                    isCurveClosed,cvScalar(0,255,255),lineWidth);

    /*Draw a set of polygons filled with color:*/
    /*cvFillPoly is used to fill an area bounded by the polygon outline alone.
                Functions can fill in complex areas, for example, areas with holes, areas with intersections, etc.
    void cvFillPoly( CvArr* img, CvPoint** pts, int* npts,
                    int contours,CvScalar color, int
                    line_type=8, int shift=0 );
            img images.
            pts Pointer to an array of polygons.
            npts An array of polygon vertex counts.
            contours The number of line segments that make up the filled area.
            color The color of the polygon.
            line_type The type of lines that make up the polygon.
            shift The number of decimal places for vertex coordinates. */
    cvFillPoly(img1,curveArr,nCurvePts,nCurves,cvScalar(0,255,255));

    /*Text annotation:*/
    /*Available font types are: CV_FONT_HERSHEY_SIMPLEX, CV_FONT_HERSHEY_PLAIN,
    CV_FONT_HERSHEY_DUPLEX, CV_FONT_HERSHEY_COMPLEX, CV_FONT_HERSHEY_TRIPLEX,
    CV_FONT_HERSHEY_COMPLEX_SMALL, CV_FONT_HERSHEY_SCRIPT_SIMPLEX,
    CV_FONT_HERSHEY_SCRIPT_COMPLEX,*/
    CvFont font;
    double hScale=1.0;
    double vScale=1.0;
    lineWidth=2;
    cvInitFont(&font,CV_FONT_HERSHEY_SIMPLEX|CV_FONT_ITALIC, hScale,vScale,0,lineWidth);
    cvPutText (img1,"Comment",cvPoint(20,100), &font, cvScalar(8,0,0));

    cvShowImage("win1",img1);
    cvSaveImage("wongrgb1.jpg",img1);
    cvWaitKey(0);
    return 0;
}

Compile and run:

$ make
g++ main.cpp `pkg-config --cflags --libs opencv`
$ ./a.out
Code reference: "OpenCV Chinese Reference Manual"

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325760421&siteId=291194637