title: Python3, cv2.setMouseCallback() and cv2.MouseCallback() parameter void* userdata
body: <p>My python3 program uses OpenCV4, (cv2) to apply a series of masks to an image.  In main, I have a string myMask initially set to the string 'none'.  I want to pass this variable into my mouse event handlers.  Each event handler is tied to a window that displays one of the black and white mask images and has a parameter string that ends as  ..., void* user data).  The event handler responds to an EVENT_LBUTTONDOWN.  The selected event handler then changes the user data parameter to a unique string.  I want the variable myMask in main to reflect the change made in the last mouse event handler.  Unfortunately, myMask is never changed in main. Why?  Any help would be greatly appreciated.  Here are two extracts from OpenCV and a portion of the code:</p>
  <p>§ MouseCallback
  typedef void(* cv::MouseCallback) (int event, int x, int y, int flags, void *userdata)
  #include &lt;opencv2/highgui.hpp&gt;</p>
  <p>Callback function for mouse events. see cv::setMouseCallback.</p>
  <p>Parameters
  event   one of the cv::MouseEventTypes constants.
  x   The x-coordinate of the mouse event.
  y   The y-coordinate of the mouse event.
  flags   one of the cv::MouseEventFlags constants.
  userdata    The optional parameter.</p>
  <p>§ setMouseCallback()
  void cv::setMouseCallback   (   const String &amp;  winname,
  MouseCallback   onMouse,
  void *  userdata = 0
  )<br />
  #include &lt;opencv2/highgui.hpp&gt;</p>
  <p>Sets mouse handler for the specified window.</p>
  <p>Parameters
  winname Name of the window.
  onMouse Callback function for mouse events. See OpenCV samples on how to specify and use the callback.
  userdata    The optional parameter passed to the callback.</p>
  <pre><code># BW MouseCallback funcs
  myMask = 'none'  # Mask to apply
  # One Callback func per window
  # BW none Callback
  def mouseHandler( event, x, y, flags, userdata ):
      if event == 1:   # cv2.EVENT_LBUTTONDOWN
          userdata = 'none'
  # BW halfBlack Callback
  def mouseHandlerHB( event, x, y, flags, userdata ):
      if event == 1:   # cv2.EVENT_LBUTTONDOWN
          userdata = 'halfBlack'
  # BW centreWhite Callback
  def mouseHandlerCW( event, x, y, flags, userdata ):
      if event == 1:   # cv2.EVENT_LBUTTONDOWN
          userdata = 'centreWhite'
  # BW bitAnd Callback
  def mouseHandlerBA( event, x, y, flags, userdata ):
      if event == 1:   # cv2.EVENT_LBUTTONDOWN
          userdata = 'bitAnd'
  # BW bitOr Callback
  def mouseHandlerBO( event, x, y, flags, userdata ):
      if event == 1:   # cv2.EVENT_LBUTTONDOWN
          userdata = 'bitOr'
  # BW bitXor Callback
  def mouseHandlerBX( event, x, y, flags, userdata ):
      if event == 1:   # cv2.EVENT_LBUTTONDOWN
          userdata  = 'bitXor'
  # BW setMouseCallbacks, tie Callback func to a window
  cv2.setMouseCallback( 'piCamR', mouseHandler, myMask )
  cv2.setMouseCallback( 'halfBlack', mouseHandlerHB, myMask )
  cv2.setMouseCallback( 'centreWhite', mouseHandlerCW, myMask )
  cv2.setMouseCallback( 'bitAnd', mouseHandlerBA, myMask )
  cv2.setMouseCallback( 'bitOr', mouseHandlerBO, myMask )
  cv2.setMouseCallback( 'bitXor', mouseHandlerBX, myMask )
  
  while True:
  
      # Grab a frame
      ret, frame1 = cam.read()
      print(myMask)
      # BW Apply mask?
      if myMask != 'none':
          if myMask == 'halfBlack':
              frame1 = cv2.bitwise_and( frame1, frame1, mask = halfBlack )
          elif myMask == 'centreWhite':
              frame1 = cv2.bitwise_and( frame1, frame1, mask = centreWhite )
          elif myMask == 'bitAnd':
              frame1 = cv2.bitwise_and( frame1, frame1, mask = bitAnd )
          elif myMask == 'bitOr':
              frame1 = cv2.bitwise_and( frame1, frame1, mask = bitOr )
          else:  # myMask == bitXor
              frame1 = cv2.bitwise_and( frame1, frame1, mask = bitXor )
      # Show frame and position
      cv2.imshow( 'piCamR', frame1)
  </code></pre>