Just a Computer Graphics Studio & My Life

[OpenCV] 描繪連續的點

我們想要描繪連續的點,可呼叫線的函式,然而若點有好幾百個,程式碼就太冗長,於是就改呼叫折線(polylines)的函式。

Fang Polylines.jpg

先把一堆點準備好,然後丟進這神奇的函式⋯⋯

//! draws one or more polygonal curves

CV_EXPORTS void polylines(Mat& img, const Point** pts, const int* npts,

                          int ncontours, bool isClosed, const Scalar& color,

                          int thickness=1, int lineType=8, int shift=0 );

/**
 Theme: Draw Polylines
 IDE: Xcode 7
 Language: C++
 Date: 105/04/11
 Author: HappyMan
 Blog: https://cg2010studio.wordpress.com/
 */
#include <iostream>

#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"

using namespace cv;
using namespace std;

Mat src;

const char* window_name = "Happy Window";

void drawContour(vector<Point> contour)
{
    const Point *pts = (const Point *) Mat(contour).data;
    int npts = Mat(contour).rows;

    cout << "Number of polygon vertices: " << npts << endl;

    // draw the polygon
    polylines(src, &pts, &npts, 1,
              false,            // draw closed contour (i.e. joint end to start)
              Scalar(63,255,0), // colour RGB ordering (here = green)
              3, 		// line thickness
              CV_AA, 0);
}

/**
 * @function main
 */

int main(int, char** argv)
{
    /// Load an image
    string fileName = "Fang.jpg";
    src = imread(fileName, CV_LOAD_IMAGE_COLOR);

    if(!src.data)
    { return -1; }

    vector<Point> contour;
    contour.push_back(Point(10,100));
    contour.push_back(Point(120,120));
    contour.push_back(Point(150,260));
    contour.push_back(Point(80,380));
    contour.push_back(Point(450,100));

    drawContour(contour);

    /// Create a window
    imshow("Happy Source", src);

    /// Wait until user exit program by pressing a key
    waitKey(0);

    return 0;
}

執行結果如首圖的綠色折線。

接下來就是嘗試修改其參數,看看效果如何囉~

Advertisements

發表留言

在下方填入你的資料或按右方圖示以社群網站登入:

WordPress.com Logo

您的留言將使用 WordPress.com 帳號。 登出 / 變更 )

Twitter picture

您的留言將使用 Twitter 帳號。 登出 / 變更 )

Facebook照片

您的留言將使用 Facebook 帳號。 登出 / 變更 )

Google+ photo

您的留言將使用 Google+ 帳號。 登出 / 變更 )

連結到 %s

標籤雲

%d 位部落客按了讚: