#### Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Supported by

# [solved] define regions of interest for mouseclicks

edited February 2015

Hello,

I can't figure out how to define regions of interest for mouseclicks and would like to ask for help with it.
In my experiment, subjects see a sketchpad with three boxes and are asked to choose between them via clicking on one of them. I first tried to use the touch_response item to collect the mouseclicks, but my supervisor asked me to limit the clickable areas exactly to the three boxes (Image_buttons and form base won't work either because the picture showing the three boxes has to correspond exactly to the last frame of a previous shown video). I think the best way to do this would be using an inline script. Unfortunately I don't know anything about programming, so I am not able to solve this by myself. If given the coordinates of the three boxes, could you provide me with a script to define the regions of interest?

Thanks

• edited 11:52PM

Hi Tabea,

Don't worry, if you don't know how to program yet. Python is rather easy to learn and you'll be able to do quite a lot after a short while. I'd like to suggest, that you give it a try and do a few online tutorials (there are plenty good ones around, just google).

Regarding your question, the general idea of what you want is following:

1) Sample current mouse position and waiting for a click

2) As soon as mouse gets clicked, take its position and check whether this position lies within the boundaries of one of the 3 ROIs

2a) Check the first ROI whether it got clicked:
If the current x-coordinate is bigger the the lower x-boundary AND
smaller than the upper x-boundary AND the same for the y-coordinates. If every condition is true, you know this ROI was clicked, if at least one of them is false, proceed with the next ROI

2b) same as 2a, just with different coordinates.

2c) same as 2a, just with different coordinates.

In a slightly more pythonic way, it will look something like this:

``````# import mouse module
from openexp.mouse import mouse

# initialize a mouse object
my_mouse = mouse(exp)

# define helper variables to indicate which ROI was clicked
ROI1_clicked, ROI2_clicked, ROI3_clicked = False, False, False
while True:
# next line corresponds to first point above (sampling and waiting for a click)
button, position, timestamp = my_mouse.get_click(timeout=20)
# timeout defines, how long you will wait for a response

# next lines correspond to 2nd point (Checking whether coordinates
# lie within ROI
if position[0] >= ROI1_x_min and position[0] <= ROI1_x_max  and \
position[1] >= ROI1_y_min and position[1] <= ROI1_y_max :
ROI1_clicked = True
break
elif ...same for ROI2_x and ROI2_y...:
ROI2_clicked = True
break
elif ...same for ROI3_x and ROI3_y...:
ROI3_clicked = True
break

``````

From that point you continue running your script depending on which ROI was clicked.

It may seem complicated, but it actually isn't. Give it a try, if you need again help, just ask.

Good luck,

Eduard

• edited February 2015

Hello,

thank you for encouraging me to give Python a try! It was actually quite fun (regarding my blank despair in the beginning).

The script I finally used looked like this:

``````# define ROIs
ROI_y_min = 546
ROI_y_max ((and so on))

# import mouse module
from openexp.mouse import mouse

# initialize a mouse object
my_mouse = mouse(exp)
my_mouse.set_visible()

# define helper variables to indicate which ROI was clicked
ROI1_clicked = False
ROI2_clicked = False
ROI3_clicked = False

while True:
# next line corresponds to first point above (sampling and waiting for a click)
click, position, timestamp = my_mouse.get_click(timeout=20)
if click == None:
print('No click')

elif ROI1_x_min <= position[0] <= ROI1_x_max and ROI_y_min <= position[1] <= ROI_y_max :
ROI1_clicked = True
break

elif ROI2_x_min <= position[0] <= ROI2_x_max and ROI_y_min <= position[1] <= ROI_y_max :
ROI2_clicked = True
break

elif ROI3_x_min <= position[0] <= ROI3_x_max and ROI_y_min <= position[1] <= ROI_y_max :
ROI3_clicked = True
break

exp.set('ROI1_clicked', ROI1_clicked)
exp.set('ROI2_clicked', ROI2_clicked)
exp.set('ROI3_clicked', ROI3_clicked)
exp.set('ans_time', timestamp)
``````
• edited 11:52PM

Great to hear. And thanks for sharing your solution.

Eduard