# Converting view coordinates to image coordinates

**URL:** <https://discourse.slicer.org/t/converting-view-coordinates-to-image-coordinates/29865>\
**Category:** Support\
**Created:** [June 6, 2023, 3:15pm UTC](https://discourse.slicer.org/t/converting-view-coordinates-to-image-coordinates/29865 "2023-06-06T15:15:06Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![asyeda](https://avatars.discourse-cdn.com/v4/letter/a/3da27b/32.png) [@asyeda](https://discourse.slicer.org/u/asyeda)\
**Post date:** [June 6, 2023, 3:15pm UTC](https://discourse.slicer.org/t/converting-view-coordinates-to-image-coordinates/29865/1 "2023-06-06T15:15:06Z")

</div>

I am writing a custom scripted loadable module where one of the features lets the user click on a button, then click & drag to select a region of interest for further analysis. I have written MouseEvents like so:

```auto
class MyModuleWidget(ScriptedLoadableModuleWidget, VTKObservationMixin):
	"""
 	Uses ScriptedLoadableModuleWidget base class, available at:
	https://github.com/Slicer/Slicer/blob/master/Base/Python/slicer/ScriptedLoadableModule.py
	"""

	def __init__ (self, parent=None):
        ...

    def setup(self):
        ...
        self.ui.myButton.connect("clicked(bool)", self.onButtonClick)
        ...

    def onButtonClick(self):
		sliceWidget = slicer.app.layoutManager().sliceWidget("Red")
		renderWindowInteractor = sliceWidget.sliceView().renderWindow().GetInteractor()
		self.leftButtonPressObserverID = renderWindowInteractor.AddObserver(vtk.vtkCommand.LeftButtonPressEvent, self.onLeftButtonPress)
		self.leftButtonReleaseObserverID = renderWindowInteractor.AddObserver(vtk.vtkCommand.LeftButtonReleaseEvent, self.onLeftButtonRelease)

	def onLeftButtonPress(self, obj, event):
		interactor = obj
		eventPosition = interactor.GetEventPosition()
		if event == "LeftButtonPressEvent":
			self.startPos = eventPosition

	def onLeftButtonRelease(self, obj, event):
		interactor = obj
		eventPosition = interactor.GetEventPosition()

		if event == "LeftButtonReleaseEvent":
			self.endPos = eventPosition

			# Perform analysis on the selected region
			if self.startPos and self.endPos:
				# TODO: convert to image coordinates and perform analysis

			# Reset the start and end positions
			self.startPos = None
			self.endPos = None

			# Remove the observers
			sliceWidget = slicer.app.layoutManager().sliceWidget("Red")
			renderWindowInteractor = sliceWidget.sliceView().renderWindow().GetInteractor()
			renderWindowInteractor.RemoveObserver(self.leftButtonPressObserverID)
			renderWindowInteractor.RemoveObserver(self.leftButtonReleaseObserverID)

```

The start and end positions are being correctly captured. However, the coordinates are not image coordinates, but view coordinates, i.e. selecting the top corner of the image should be, for example:

```auto
startPos = (0, 0)
endPos = (30, 30)

```

But if I select this region, the coordinates might instead be set to:

```auto
startPos = (30, 30)
endPos = (60, 60)

```

How can I convert these view coordinates to image coordinates?

---

<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:** [June 6, 2023, 11:23pm UTC](https://discourse.slicer.org/t/converting-view-coordinates-to-image-coordinates/29865/2 "2023-06-06T23:23:14Z")

</div>

You can look into implementation of segment editor effects that take mouse clicks as inputs. However, I would recommend to use a markups plane or ROI for selecting a region.

---

<div class="post-metadata">

**Author:** ![asyeda](https://avatars.discourse-cdn.com/v4/letter/a/3da27b/32.png) [@asyeda](https://discourse.slicer.org/u/asyeda)\
**Post date:** [June 8, 2023, 1:26am UTC](https://discourse.slicer.org/t/converting-view-coordinates-to-image-coordinates/29865/3 "2023-06-08T01:26:50Z")

</div>

I see, is there a way to implement it so that the user selects the ROI after clicking the button, and that ROI is used for further analysis? Something like this:

![Slicer question](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/1/e/1e382db0980bb11682e2cf03e8a603b94d117371.gif)
