Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

How to create polyline with mouse event

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.