python-microscope / microscope

Python library for control of microscope devices, supporting hardware triggers and distribution of devices over the network for performance and flexibility.
https://www.python-microscope.org
GNU General Public License v3.0
66 stars 39 forks source link

ROI selection fix for Ximea cameras (industrial) #240

Open jacopoabramo opened 2 years ago

jacopoabramo commented 2 years ago

Greetings,

I have a Ximea camera from the xiB-64 family which I want to use with microscope. One of the issues that this camera has is that when changing the ROI, the minimum step of either the X/Y offset or the height/width to make a change is variable depending on the actual values one sets for each parameter. I think this is not valid for all device families but it applies to mine since it is usually used for industrial applications. Nevertheless I tried applying this change in this commit. I also made a quick test with the following script:

from time import sleep
from queue import Queue
from microscope.cameras.ximea import XimeaCamera
from microscope import ROI

buffer = Queue()

camera = XimeaCamera()
camera._handle.set_trigger_source("XI_TRG_OFF")
camera.set_exposure_time(10.0*1e-6)
camera.set_client(buffer)

# testing ROIs
newROI = ROI(0, 0, 211, 233)
print(f"TEST 1: ROI = {newROI}")
camera.set_roi(newROI)
print(f"TEST 1: actual ROI = {camera._roi}")

camera.enable()
sleep(0.10)
camera.disable()

print(f"Current queue size: {buffer.qsize()}")
print(f"First image of buffer: {buffer.queue[0]}")
buffer.queue.clear()

###################################################

newROI = ROI(198, 212, 430, 382)
print(f"TEST 2: ROI = {newROI}")
camera.set_roi(newROI)
print(f"TEST 2: actual ROI = {camera._roi}")

camera.enable()
sleep(0.10)
camera.disable()

camera.shutdown()

print(f"Current queue size: {buffer.qsize()}")
print(f"First image of buffer: {buffer.queue[0]}")

And this is the output I get:

TEST 1: ROI = ROI(left=0, top=0, width=211, height=233)
TEST 1: actual ROI = ROI(left=0, top=0, width=224, height=232)
Current queue size: 1359
First image of buffer: [[3 6 6 ... 8 8 8]
 [6 6 3 ... 7 7 8]
 [5 5 5 ... 8 5 5]
 ...
 [5 5 5 ... 5 5 5]
 [4 4 4 ... 5 5 5]
 [1 4 4 ... 5 5 5]]
TEST 2: ROI = ROI(left=198, top=212, width=430, height=382)
TEST 2: actual ROI = ROI(left=192, top=212, width=416, height=384)
Current queue size: 751
First image of buffer: [[ 8  7  8 ...  8  9  9]
 [ 8  7  7 ...  8 10  8]
 [ 5  5  5 ...  5  5  5]
 ...
 [ 8  5  5 ...  5  5  8]
 [ 5  5  5 ...  5  5  5]
 [ 5  5  5 ...  5  5  5]]

Let me know if this seems feasible to you.

jacopoabramo commented 2 years ago

Note: the camera._handle.set_trigger_source("XI_TRG_OFF") serves as a way to enter free-running mode with the Ximea cameras. I also mentioned this in this thread.