First time here? Check out the FAQ!

Ask Your Question
1

Wait for a mouse click?

asked Jun 15 '13

yes123 gravatar image

updated Oct 13 '18

I have a video. I would like to show the next frame only if the user clicks on a pixel on the current frame.

(The user must select a point in each frame)

Without using waitKey(0) I could I make opencv waiting for the mouse click before showing the next frame?

Preview: (hide)

1 answer

Sort by » oldest newest most voted
3

answered Jun 15 '13

berak gravatar image

you can't do without waitkey, if you plan to use imshow.

but you could make the grab/read part of the capture depend on mouseclick:

// avoid global vars, pass a struct* instead:
struct User
{
    VideoCapture cap;
    Mat m;
};

void mouse(int k, int x, int y, int s, void *p)
{
    User * u  = (User*)p;
    if ( k && s )
        u->cap.read(u->m);
}
int main(int,char**)
{
    User u;
    namedWindow("i",0);
    setMouseCallback("i",mouse,&u);
    u.cap.open("mona.flv");
    while( u.cap.isOpened() )
    {
        if (! u.m.empty())
            imshow("i",u.m);
        int k = waitKey(10);
        if ( k==27 ) break;
    }
    return 0;
}
Preview: (hide)

Question Tools

Stats

Asked: Jun 15 '13

Seen: 9,916 times

Last updated: Jun 15 '13