Select and Move Objects/Points using OpenCV – Drag and Drop

If you have a passion in image processing and computer animations, this post will be interesting . This post explains how to use the  OpenCV to select and move the objects.

 Before the explanations, watch this video to get the idea.

As you can see, the controls are based on the mouse movements and clicks.

  • So first need to implement the mouse-handler that comes with OpenCV – cvSetMouseCallback
    • Left button down – select the object of clicked point – findImg( x, y)
    • Left button up – release the object if there any objects has been picked – releaseImg(selectedImg,x,y);
    • On mouse move – move the selected object with mouse

  • Then implement the logics for the findImg( x, y) & releaseImg(selectedImg,x,y)  methods according to your requirements.
    •  Here i used an integer array to store the initial position of each object.
    • Then search for object using the x & y coordinates when there is a click.
    • when releasing the object update the integer array with released coordinate.
  • Finally call the cvSetMouseCallback( “Drag & Drop”, mouseHandler, NULL ) to activate the mouse handler to the window. where;
    • “Drag & Drop” – the OpenCV window name
    • mouseHandler – the mouse handler function name.
Check the links for demo and source

Happy reading and animation  🙂 .

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *

%d bloggers like this: