Ask Your Question
2

How to create polyline with mouse event

asked 2017-08-21 20:36:23 -0600

Shabrina gravatar image

I have image matrix, then The will create a polylines with right klik on mouse when I already determine the some point. this is the code.

void on_mouse(int event,int x,int y,int flags,void *ustc)
    {
        //Point pt;//mouse position;
        char coordinate[100];

        if (event == CV_EVENT_LBUTTONDOWN)
        {
            pt = Point2f(x,y);
            cout<<x<<" "<<y<<endl;
            coor.push_back(pt);
            //cout<< coor << endl;

            cv::FileStorage simpan("template.txt", cv::FileStorage::WRITE);
            cv::write(simpan,"Point",coor);
            simpan.release();

            circle(src_1,pt,3,Scalar(0,255,50),CV_FILLED,CV_AA,0);
            //src.copyTo(src);
            imshow("Medblur",src_1);
            }

        if(event==CV_EVENT_RBUTTONDOWN)
        {
            polylines(src_1,coor,false,Scalar(255,255,255),2,150,0);
        }
    }

    int main(int argc, char** argv){
    CvCapture* capture= cvCaptureFromFile("D:/Kuliah/SMT 7/TA/data/Data Input/1.avi");
    int loop=0, count=0;
    std::string suffix = ".jpg";
    IplImage* jt =NULL;
    char fname[10];

    FileStorage smp30("template.txt", cv::FileStorage::READ);
    ab[30] = smp30["Point"];
    read(ab[30],jnt[30]);
    do
    {   
        std::stringstream ss;
        jt= cvQueryFrame(capture);
        src= cv::cvarrToMat(jt);
        //src = imread("tresh.jpg",1);
        medianBlur(src,src_1,3);
        //double ai=PSNR(src,ln);
        //cout<<count<<"\t"<<ai<<"\n";
        threshold(src_1,src_2,40,130,CV_THRESH_BINARY);
        //cvtColor(src, src, COLOR_BGR2GRAY);
        imshow("Medblur",src_1);
        imshow("Thresh",src_2);
        imshow("Asli",src);
        setMouseCallback("Medblur",on_mouse,0);

        waitKey(0);
    }
    while (jt !=NULL);

}

and this is the error

OpenCV Error: Assertion failed (p.checkVector(2, CV_32S) >= 0) in cv::polylines, file ........\opencv\modules\core\src\drawing.cpp, line 2067

thanks :)) I really need help. Thank you for your attention.

edit retag flag offensive close merge delete

Comments

Opencv version ? What is coor, src_1 in on_mouse function? You shouldn't use setMouseCallback("Medblur",on_mouse,0); in do while :

namedWindow("Medblur");
setMouseCallback("Medblur",on_mouse,0);
do {.....
LBerger gravatar imageLBerger ( 2017-08-22 02:10:16 -0600 )edit

opencv version 2.4.9. coor is vector<Point2f>, src_1 is mat that contain result of median.

if I don't use setMouseCallback("Medblur",on_mouse,0); in do while, this system can't read a frame.. thanks

Shabrina gravatar imageShabrina ( 2017-08-22 06:32:06 -0600 )edit

update opencv to 2.4.12.3 or 3.3

LBerger gravatar imageLBerger ( 2017-08-22 06:34:26 -0600 )edit

2 answers

Sort by ยป oldest newest most voted
1

answered 2017-08-21 23:10:00 -0600

Ziri gravatar image

Use "Point" instead of "Point2f". You'll have to cast x & y coordinates.

edit flag offensive delete link more
1

answered 2017-08-22 12:13:01 -0600

updated 2017-08-22 12:45:53 -0600

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 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;
}
edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2017-08-21 20:36:23 -0600

Seen: 1,288 times

Last updated: Aug 22 '17