# How to get input value from mouse click?

**URL:** <https://discourse.slicer.org/t/how-to-get-input-value-from-mouse-click/5903>\
**Category:** Development\
**Created:** [February 24, 2019, 1:49pm UTC](https://discourse.slicer.org/t/how-to-get-input-value-from-mouse-click/5903 "2019-02-24T13:49:42Z")\
**Posts on this page:** 17\
**Page:** 1

<div class="post-metadata">

**Author:** ![zenox](https://avatars.discourse-cdn.com/v4/letter/z/bb73d2/32.png) [@zenox](https://discourse.slicer.org/u/zenox)\
**Post date:** [February 24, 2019, 1:49pm UTC](https://discourse.slicer.org/t/how-to-get-input-value-from-mouse-click/5903/1 "2019-02-24T13:49:42Z")

</div>

Hello,

I am looking to add a button in my extension GUI. The button should allow a user to click somewhere on the volume to retrieve the input value where is clicked.

Can someone say if it’s possible ? In Python.

 ![2019-02-23-171215_1920x1080_scrot](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/4/d/4d4708ee9fba44e4d1829e8ce5c46f23d2c0ed93.png)

Thank you,

zenox

---

<div class="post-metadata">

**Author:** ![cpinter](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/cpinter/32/7995_2.png) [@cpinter](https://discourse.slicer.org/u/cpinter)\
**Post date:** [February 24, 2019, 5:12pm UTC](https://discourse.slicer.org/t/how-to-get-input-value-from-mouse-click/5903/2 "2019-02-24T17:12:35Z")

</div>

Without knowing the purpose and details of your module, I’d probably add a markups fiducial list that the user fills with clicking around. Then you can very easily get the voxel value under those fiducials.

---

<div class="post-metadata">

**Author:** ![zenox](https://avatars.discourse-cdn.com/v4/letter/z/bb73d2/32.png) [@zenox](https://discourse.slicer.org/u/zenox)\
**Post date:** [February 24, 2019, 5:54pm UTC](https://discourse.slicer.org/t/how-to-get-input-value-from-mouse-click/5903/3 "2019-02-24T17:54:53Z")

</div>

@cpinter, Thank you very much for your answer.

The purpose is just to retrieve the value of input where the user clicked somewhere in a volume.

I will look at the official documentation to find an example of code, then try to implement this feature in my module.

---

<div class="post-metadata">

**Author:** ![cpinter](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/cpinter/32/7995_2.png) [@cpinter](https://discourse.slicer.org/u/cpinter)\
**Post date:** [February 24, 2019, 6:08pm UTC](https://discourse.slicer.org/t/how-to-get-input-value-from-mouse-click/5903/4 "2019-02-24T18:08:10Z")

</div>

> [@zenox](#):
>
> The purpose is just to retrieve the value of input where the user clicked somewhere in a volume

Yes but the best way to do it could be very different depending of the bigger picture (real-time or not - interaction vs analysis, number of points, etc.), so it could be useful if you described the purpose of the module and the value picking.

---

<div class="post-metadata">

**Author:** ![zenox](https://avatars.discourse-cdn.com/v4/letter/z/bb73d2/32.png) [@zenox](https://discourse.slicer.org/u/zenox)\
**Post date:** [February 24, 2019, 6:26pm UTC](https://discourse.slicer.org/t/how-to-get-input-value-from-mouse-click/5903/5 "2019-02-24T18:26:21Z")

</div>

Ok, the goal is to retrieve the value of 1 point to adjust my GUI and help the user of the extension. And this is not for real-time.

- First, the user clicks on a button _Get Value_ (B)
- Then, in the background, there is a listener that allows user to click somewhere in the volume and get **input value**
- Finally, we adjust the _Threshold Range_ cursors (A) with this **input value** in the GUI

 ![3d_slicer_gui](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/c/5/c528c4f315ad763d41e961dcca7086cd07f8551f.jpeg)

For example, if user clicked on **input value** = 1125, we will adjust the _Thresold Range_ cursors (A) with +/- 125, so between 1000 and 1250.

---

<div class="post-metadata">

**Author:** ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)\
**Post date:** [February 24, 2019, 8:21pm UTC](https://discourse.slicer.org/t/how-to-get-input-value-from-mouse-click/5903/6 "2019-02-24T20:21:59Z")

</div>

We usually use temporary markup nodes for this:

- User clicks “Get value” button
- Module creates a markup node, adds an observer for point add event, sets the node as active node (in selection node singleton) and sets mouse interaction mode to Place fiducial mode (in interaction node singleton)
- User clicks in the image
- Module is notified about the added fiducial, retrieves its position, then deletes the markup node

---

<div class="post-metadata">

**Author:** ![zenox](https://avatars.discourse-cdn.com/v4/letter/z/bb73d2/32.png) [@zenox](https://discourse.slicer.org/u/zenox)\
**Post date:** [February 24, 2019, 8:28pm UTC](https://discourse.slicer.org/t/how-to-get-input-value-from-mouse-click/5903/7 "2019-02-24T20:28:12Z")

</div>

@lassoan, Thank you very much for your answer.

Now, that’s more clear for me.

I will come back here if I don’t find a way to implement it in my module.

Have a good day,

zenox

---

<div class="post-metadata">

**Author:** ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)\
**Post date:** [February 24, 2019, 8:37pm UTC](https://discourse.slicer.org/t/how-to-get-input-value-from-mouse-click/5903/8 "2019-02-24T20:37:42Z")

</div>

You can use qSlicerMarkupsPlaceWidget to all the steps that I described above. The code below creates a button that places a markup (and it also provides signals that you can observe):

```auto
# Temporary markups node
markupsNode = slicer.mrmlScene.AddNewNodeByClass("vtkMRMLMarkupsFiducialNode")

def placementModeChanged(active):
  print("Placement: " +("active" if active else "inactive"))
  # You can inspect what is in the markups node here, delete the temporary markup node, etc.

# Create and set up widget that contains a single "place markup" button. The widget can be placed in the module GUI.
placeWidget = slicer.qSlicerMarkupsPlaceWidget()
placeWidget.setMRMLScene(slicer.mrmlScene)
placeWidget.setCurrentNode(markupsNode)
placeWidget.buttonsVisible=False
placeWidget.placeButton().show()
placeWidget.connect('activeMarkupsFiducialPlaceModeChanged(bool)', placementModeChanged)
placeWidget.show()

```

---

<div class="post-metadata">

**Author:** ![zenox](https://avatars.discourse-cdn.com/v4/letter/z/bb73d2/32.png) [@zenox](https://discourse.slicer.org/u/zenox)\
**Post date:** [February 24, 2019, 9:04pm UTC](https://discourse.slicer.org/t/how-to-get-input-value-from-mouse-click/5903/9 "2019-02-24T21:04:42Z")

</div>

@lassoan, This is really helpful, thank you ! Now, it should be easy enough to implement, I’ll let you know if it works.

---

<div class="post-metadata">

**Author:** ![zenox](https://avatars.discourse-cdn.com/v4/letter/z/bb73d2/32.png) [@zenox](https://discourse.slicer.org/u/zenox)\
**Post date:** [February 25, 2019, 10:16pm UTC](https://discourse.slicer.org/t/how-to-get-input-value-from-mouse-click/5903/10 "2019-02-25T22:16:48Z")

</div>

It works, but opens a new window with the button.

Do you know how I could attach it to my “Get Value” button in the GUI ?

I define my widget to display a button:

```
def setup(self):
  self.getValueButton = qt.QPushButton("Get Value")
  self.getValueButton.toolTip = "Get value from image"
  self.getValueButton.enabled = False
  self.getValueButton.connect('clicked(bool)', self.onGetValueButton)

```

A function to create a markup node, switch to Place fiducial mode and retrieve click position:

```
def getMousePointValue(self):
  return position

```

A function connected to my button:

```
def onGetValueButton(self):
  position = self.getMousePointValue()
```

---

<div class="post-metadata">

**Author:** ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)\
**Post date:** [February 25, 2019, 10:48pm UTC](https://discourse.slicer.org/t/how-to-get-input-value-from-mouse-click/5903/11 "2019-02-25T22:48:23Z")

</div>

Instead of calling `show()` method, add the widget to your module’s GUI the same way as you add any other buttons and widgets.

---

<div class="post-metadata">

**Author:** ![zenox](https://avatars.discourse-cdn.com/v4/letter/z/bb73d2/32.png) [@zenox](https://discourse.slicer.org/u/zenox)\
**Post date:** [February 26, 2019, 12:16pm UTC](https://discourse.slicer.org/t/how-to-get-input-value-from-mouse-click/5903/12 "2019-02-26T12:16:09Z")

</div>

@lassoan, Oh obviously… it works ! And I can retrieve markup’s position without problem.

In the screenshot of my first post, I have 2 volumes (_input.nrrd_ and _segmentation.nrrd_).  
I don’t find a way to retrieve the **input** value (here 1125) at markup’s position, have you an idea ?

---

<div class="post-metadata">

**Author:** ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)\
**Post date:** [February 27, 2019, 6:34pm UTC](https://discourse.slicer.org/t/how-to-get-input-value-from-mouse-click/5903/13 "2019-02-27T18:34:12Z")

</div>

> [@zenox](#):
>
> I don’t find a way to retrieve the **input** value (here 1125) at markup’s position

See example in the script repository: [Documentation/Nightly/ScriptRepository - Slicer Wiki](https://www.slicer.org/wiki/Documentation/Nightly/ScriptRepository#Get_volume_voxel_coordinates_from_markup_fiducial_RAS_coordinates)

---

<div class="post-metadata">

**Author:** ![zenox](https://avatars.discourse-cdn.com/v4/letter/z/bb73d2/32.png) [@zenox](https://discourse.slicer.org/u/zenox)\
**Post date:** [March 1, 2019, 7:31am UTC](https://discourse.slicer.org/t/how-to-get-input-value-from-mouse-click/5903/14 "2019-03-01T07:31:20Z")

</div>

Ok, I can retrieve the voxel coordinates of my input volume without problem. But I don’t know how to access the value with theses coordinates, like volume[x, y , z] = 1125 ? Sorry, I’m really a noob with Slicer.

---

<div class="post-metadata">

**Author:** ![pieper](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/pieper/32/8_2.png) [@pieper](https://discourse.slicer.org/u/pieper)\
**Post date:** [March 1, 2019, 12:32pm UTC](https://discourse.slicer.org/t/how-to-get-input-value-from-mouse-click/5903/15 "2019-03-01T12:32:08Z")

</div>

You just need to use `slicer.utils.array` to get a numpy array, and then you can index into it.

[https://www.slicer.org/wiki/Documentation/Nightly/Developers/Python\_scripting#Accessing\_Volume\_data\_as\_numpy\_array](https://www.slicer.org/wiki/Documentation/Nightly/Developers/Python_scripting#Accessing_Volume_data_as_numpy_array)

[https://www.slicer.org/wiki/Documentation/Nightly/ScriptRepository#Get\_the\_values\_of\_all\_voxels\_for\_a\_label\_value](https://www.slicer.org/wiki/Documentation/Nightly/ScriptRepository#Get_the_values_of_all_voxels_for_a_label_value)

---

<div class="post-metadata">

**Author:** ![zenox](https://avatars.discourse-cdn.com/v4/letter/z/bb73d2/32.png) [@zenox](https://discourse.slicer.org/u/zenox)\
**Post date:** [March 4, 2019, 2:19am UTC](https://discourse.slicer.org/t/how-to-get-input-value-from-mouse-click/5903/16 "2019-03-04T02:19:44Z")

</div>

Thank you very much @pieper and @lassoan, all is working.

---

<div class="post-metadata">

**Author:** ![zenox](https://avatars.discourse-cdn.com/v4/letter/z/bb73d2/32.png) [@zenox](https://discourse.slicer.org/u/zenox)\
**Post date:** [March 4, 2019, 2:23am UTC](https://discourse.slicer.org/t/how-to-get-input-value-from-mouse-click/5903/17 "2019-03-04T02:23:43Z")

</div>

I put a part of code here, that could help others in the futur:

```
 def getMousePointValue(self, volumeNode, markupsNode):
    # Get point coordinate in RAS
    point_ras = [0, 0, 0, 1]

    # Get number of markups
    num_fids = markupsNode.GetNumberOfFiducials()

    # Get coordinates of last markup
    markupsNode.GetNthFiducialWorldCoordinates(num_fids - 1, point_ras)

    # Apply that transform to get volume's RAS coordinates
    transform_ras_to_volume_ras = vtk.vtkGeneralTransform()
    slicer.vtkMRMLTransformNode.GetTransformBetweenNodes(None, volumeNode.GetParentTransformNode(), transform_ras_to_volume_ras)
    point_volume_ras = transform_ras_to_volume_ras.TransformPoint(point_ras[0:3])

    # Get voxel coordinates from physical coordinates
    volume_ras_to_ijk = vtk.vtkMatrix4x4()
    volumeNode.GetRASToIJKMatrix(volume_ras_to_ijk)
    point_ijk = [0, 0, 0, 1]
    volume_ras_to_ijk.MultiplyPoint(np.append(point_volume_ras, 1.0), point_ijk)
    point_ijk = [int(round(c)) for c in point_ijk[0:3] ]

    # Get markup's position
    x, y, z = point_ijk[0], point_ijk[1], point_ijk[2]

    # Get volume as slicer array
    a = arrayFromVolume(volumeNode)

    # Get value at markup's position
    value = a[z,y,x]
```
