Ask Your Question

Revision history [back]

polylines() supports only vector<Point> or a Mat having Integer values. ( it seems someone can make a feature request about supporting float values )

but there is still a tricky way :

convert your vector<Point2f> to a Mat of type 32S like below

    vector<Point2f> triangle;        
    Mat mat32s;
    Mat(triangle).convertTo(mat32s, CV_32S);

try whole code to understand better

#include "opencv2/highgui.hpp"
#include "opencv2/imgproc.hpp"
#include <iostream>

using namespace cv;
using namespace std;


int main(int /*argc*/, char** /*argv*/)
{

    Mat img(500, 500, CV_8UC3);
    RNG& rng = theRNG();

    for (;;)
    {
        int i, count = rng.uniform(1, 101);
        vector<Point> points;

        // Generate a random set of points
        for (i = 0; i < count; i++)
        {
            Point pt;
            pt.x = rng.uniform(img.cols / 4, img.cols * 3 / 4);
            pt.y = rng.uniform(img.rows / 4, img.rows * 3 / 4);

            points.push_back(pt);
        }

        // Find the minimum area enclosing triangle
        vector<Point2f> triangle;

        minEnclosingTriangle(points, triangle);

        img = Scalar::all(0);

        // Draw the points
        for (i = 0; i < count; i++)
            circle(img, points[i], 3, Scalar(0, 0, 255), FILLED, LINE_AA);

        //polylines(img, triangle, true, Scalar(0, 255, 0), 2); // gives error

        Mat mat32s;
        Mat(triangle).convertTo(mat32s, CV_32S);

        polylines(img, mat32s, true, Scalar(0, 255, 0), 2); // after converting type 32F to 32S it is OK

        imshow("Rectangle, triangle & circle", img);

        char key = (char)waitKey();
        if (key == 27 || key == 'q' || key == 'Q') // 'ESC'
            break;
    }

    return 0;
}

i think @ziri answered your question. please accept his answer.

i wanted to give some additional details hoping to be useful.

polylines() supports only vector<Point> or a Mat having Integer values. ( it seems someone can make a feature request about supporting float values )

but there is still a tricky way :

convert your vector<Point2f> to a Mat of type 32S like below

    vector<Point2f> triangle;        
    Mat mat32s;
    Mat(triangle).convertTo(mat32s, CV_32S);

try whole code to understand better

#include "opencv2/highgui.hpp"
#include "opencv2/imgproc.hpp"
#include <iostream>

using namespace cv;
using namespace std;


int main(int /*argc*/, char** /*argv*/)
{

    Mat img(500, 500, CV_8UC3);
    RNG& rng = theRNG();

    for (;;)
    {
        int i, count = rng.uniform(1, 101);
        vector<Point> points;

        // Generate a random set of points
        for (i = 0; i < count; i++)
        {
            Point pt;
            pt.x = rng.uniform(img.cols / 4, img.cols * 3 / 4);
            pt.y = rng.uniform(img.rows / 4, img.rows * 3 / 4);

            points.push_back(pt);
        }

        // Find the minimum area enclosing triangle
        vector<Point2f> triangle;

        minEnclosingTriangle(points, triangle);

        img = Scalar::all(0);

        // Draw the points
        for (i = 0; i < count; i++)
            circle(img, points[i], 3, Scalar(0, 0, 255), FILLED, LINE_AA);

        //polylines(img, triangle, true, Scalar(0, 255, 0), 2); // gives error

        Mat mat32s;
        Mat(triangle).convertTo(mat32s, CV_32S);

        polylines(img, mat32s, true, Scalar(0, 255, 0), 2); // after converting type 32F to 32S it is OK

        imshow("Rectangle, triangle & circle", img);

        char key = (char)waitKey();
        if (key == 27 || key == 'q' || key == 'Q') // 'ESC'
            break;
    }

    return 0;
}

i think @ziri answered your question. please accept his answer.

i wanted to give some additional details hoping to be useful.

polylines() supports only vector<Point> or a Mat having Integer values. ( it seems someone can make a feature request about supporting float values )

but there is still a tricky way :

convert your vector<Point2f> to a Mat of type 32S like below

    vector<Point2f> triangle;        
    Mat mat32s;
    Mat(triangle).convertTo(mat32s, CV_32S);

try whole the following code to understand better

#include "opencv2/highgui.hpp"
#include "opencv2/imgproc.hpp"
#include <iostream>

using namespace cv;
using namespace std;


int main(int /*argc*/, char** /*argv*/)
{

    Mat img(500, 500, CV_8UC3);
    RNG& rng = theRNG();

    for (;;)
    {
        int i, count = rng.uniform(1, 101);
        vector<Point> points;

        // Generate a random set of points
        for (i = 0; i < count; i++)
        {
            Point pt;
            pt.x = rng.uniform(img.cols / 4, img.cols * 3 / 4);
            pt.y = rng.uniform(img.rows / 4, img.rows * 3 / 4);

            points.push_back(pt);
        }

        // Find the minimum area enclosing triangle
        vector<Point2f> triangle;

        minEnclosingTriangle(points, triangle);

        img = Scalar::all(0);

        // Draw the points
        for (i = 0; i < count; i++)
            circle(img, points[i], 3, Scalar(0, 0, 255), FILLED, LINE_AA);

        //polylines(img, triangle, true, Scalar(0, 255, 0), 2); // gives error

        Mat mat32s;
        Mat(triangle).convertTo(mat32s, CV_32S);

        polylines(img, mat32s, true, Scalar(0, 255, 0), 2); // after converting type 32F to 32S it is OK

        imshow("Rectangle, triangle & circle", img);

        char key = (char)waitKey();
        if (key == 27 || key == 'q' || key == 'Q') // 'ESC'
            break;
    }

    return 0;
}

i think @ziri answered your question. please accept his answer.

i wanted to give some additional details hoping to be useful.

polylines() supports only vector<Point> or a Mat having Integer values. ( it seems someone can make a feature request about supporting float values )

but there is still a tricky way :

convert your vector<Point2f> to a Mat of type 32S like belowbelow to use it with polylines()

    vector<Point2f> triangle;        
    Mat mat32s;
    Mat(triangle).convertTo(mat32s, CV_32S);

try the following code to understand better

#include "opencv2/highgui.hpp"
#include "opencv2/imgproc.hpp"
#include <iostream>

using namespace cv;
using namespace std;


int main(int /*argc*/, char** /*argv*/)
{

    Mat img(500, 500, CV_8UC3);
    RNG& rng = theRNG();

    for (;;)
    {
        int i, count = rng.uniform(1, 101);
        vector<Point> points;

        // Generate a random set of points
        for (i = 0; i < count; i++)
        {
            Point pt;
            pt.x = rng.uniform(img.cols / 4, img.cols * 3 / 4);
            pt.y = rng.uniform(img.rows / 4, img.rows * 3 / 4);

            points.push_back(pt);
        }

        // Find the minimum area enclosing triangle
        vector<Point2f> triangle;

        minEnclosingTriangle(points, triangle);

        img = Scalar::all(0);

        // Draw the points
        for (i = 0; i < count; i++)
            circle(img, points[i], 3, Scalar(0, 0, 255), FILLED, LINE_AA);

        //polylines(img, triangle, true, Scalar(0, 255, 0), 2); // gives error

        Mat mat32s;
        Mat(triangle).convertTo(mat32s, CV_32S);

        polylines(img, mat32s, true, Scalar(0, 255, 0), 2); // after converting type 32F to 32S it is OK

        imshow("Rectangle, triangle & circle", img);

        char key = (char)waitKey();
        if (key == 27 || key == 'q' || key == 'Q') // 'ESC'
            break;
    }

    return 0;
}