A yousef
A yousef

Reputation: 3

using cvSetMouseCallback in javacv

I'm fairly new to programming and I have been doing some image processing using javacv but I am having trouble finding a coordinate using a mouse event for an iplimage. I basically want to get a (x,y) coordinate of an iplimage using the left click button. I would be very appreciative if someone could give me a basic example of how to use this function. I looked online and all the stuff I saw was rather confusing. I know the parameters are cvSetMouseCallback("string", on_mouse, null) , however I have no idea what on_mouse is.

Upvotes: 0

Views: 538

Answers (1)

demongolem
demongolem

Reputation: 9708

So a snippet of code using this method is:

    cvNamedWindow("LKpyr_OpticalFlow", CV_WINDOW_AUTOSIZE);
    cvShowImage("LKpyr_OpticalFlow", imgC);
    CvMouseCallback on_mouse = new CvMouseCallback() {
        @Override
        public void call(int event, int x, int y, int flags, com.googlecode.javacpp.Pointer param) {
            System.out.println("point = (" + x + ", " + y + ")");
        }
    };
    cvSetMouseCallback("LKpyr_OpticalFlow", on_mouse, null);

    cvWaitKey(0);

To answer the on_mouse question, it is simply a mouse callback. In other words, what happens when a mouse event occurs? In the above code, a brief CvMouseCallback has been implemented by overriding the call method. In this code, the x and y coordinate are displayed to standard output. The code can be found here for the original class CvMouseCallback.

So how do you know what called the callback? That is given in the event parameter to call. The usual way to differentiate is to do a switch statement over event. For example, the left button corresponds to CV_EVENT_LBUTTONDOWN. So if you only want the left button to print, make sure that even is equal to the above constant.

Upvotes: 1

Related Questions