Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

yea, broken logic / code.

to draw a marker, you would need an image, and you probably don't have that in your onMouse callback.

also, you can't erase anything, but you can choose, NOT to draw it again in the next frame.

also, your counter idea is flawed, read up on FizzBuzz (you'll probably fail that)

all schadenfreude aside, let's try to fix it. assuming, you wanted a single marker drawn at the current click position

void onMouse(int event, int x, int y, int flags, void *param){
    Point *p = (Point*)param;

    // set
    if (event == CV_EVENT_LBUTTONDOWN){
         p->x = x;
         p->y = y;
    }

    // clear
    if (event == CV_EVENT_RBUTTONDOWN){
         p->x = -1;
         p->y = -1;
    }
}


// in main()

Point p(-1,-1);  // initially invisible
namedWindow("main");
setMouseCallback("main", onMouse,  (void) &p); // use our point in the callback function

while(true) {
     Mat frame; 
     capture.read(frame);

    if (p.x!= -1) 
         cv::drawMarker(frame, p,  cv::Scalar(0, 0, 255), MARKER_CROSS, 10, 1);

    ....
}

yea, broken logic / code.

to draw a marker, you would need an image, and you probably don't have that in your onMouse callback.

also, you can't erase anything, but you can choose, NOT to draw it again in the next frame.

also, your counter idea is flawed, read up on FizzBuzz (you'll probably fail that)

all schadenfreude aside, let's try to fix it. assuming, you wanted a single marker drawn at the current click position

void onMouse(int event, int x, int y, int flags, void *param){
    Point *p = (Point*)param;

    // set
    if (event == CV_EVENT_LBUTTONDOWN){
         p->x = x;
         p->y = y;
    }

    // clear
    if (event == CV_EVENT_RBUTTONDOWN){
         p->x = -1;
         p->y = -1;
    }
}


// in main()

Point p(-1,-1);  // initially invisible
namedWindow("main");
setMouseCallback("main", onMouse,  (void) &p); // use our point in the callback function

while(true) {
     Mat frame; 
     capture.read(frame);

    if (p.x!= (p.x != -1) 
         cv::drawMarker(frame, p,  cv::Scalar(0, 0, 255), MARKER_CROSS, 10, 1);

    ....
}

yea, broken logic / code.

to draw a marker, you would need an image, and you probably don't have that in your onMouse callback.

also, you can't erase anything, but you can choose, NOT to draw it again in the next frame.

also, your counter idea is flawed, read up on FizzBuzz (you'll probably fail that)

all schadenfreude aside, let's try to fix it. assuming, you wanted a single marker drawn at the current click position ,

void onMouse(int event, int x, int y, int flags, void *param){
    Point *p = (Point*)param;

    // set
    if (event == CV_EVENT_LBUTTONDOWN){
         p->x = x;
         p->y = y;
    }

    // clear
    if (event == CV_EVENT_RBUTTONDOWN){
         p->x = -1;
         p->y = -1;
    }
}


// in main()

Point p(-1,-1);  // initially invisible
namedWindow("main");
setMouseCallback("main", onMouse,  (void) &p); // use our point in the callback function

while(true) {
     Mat frame; 
     capture.read(frame);

    if (p.x != -1) 
         cv::drawMarker(frame, p,  cv::Scalar(0, 0, 255), MARKER_CROSS, 10, 1);

    ....
}