Skip to content

Query About Image Capture Format and Synchronization Best Practices #46

Open
@manuInWound

Description

@manuInWound

Hi

I’m working on a Python script to capture 1920x1080 images at the highest possible frame rate using the GRAY16_LE format on my DMM 37UX265ML camera. My system runs Ubuntu 22.04.5 LTS 64-bit.

Current Progress:

I’ve successfully captured raw images using my code.
I’m saving the data in a .raw file format, but I’d like to confirm whether this matches the format described in the Tcam-capture documentation.

BMP - Windows Bitmap GIF - Graphic Interchange Format JPG - Joint Photographic Experts Group JPEG - Joint Photographic Experts Group PNG - Portable Network Graphics PBM - Portable Bitmap Read PGM - Portable Graymap Read PPM - Portable Pixmap XBM - X11 Bitmap XPM - X11 Pixmap

Code Overview:
The script captures monochromatic raw images after illuminating an area with different LEDs. For each LED, I capture 2 images. I’ve removed the hardware control portion of the code and replaced it with comments (TURN ON LED / TURN OFF LED) for simplicity.

Questions:

Are there best practices or recommended code patterns for achieving the fastest possible capture rate while synchronizing with external hardware (e.g., LEDs)?
Currently, I’m using a software trigger (as it’s easier than soldering a hardware trigger cable). Are there potential bottlenecks or optimizations I should consider?

Any guidance would be greatly appreciated!

`
import os
import cv2
import TIS
import time

#CustomData class to handle image callbacks
class CustomData:
def init(self):
self.newImageReceived = False
self.image = None
self.busy = False

#Callback function for new image
CD = CustomData()

def on_new_image(tis, userdata):
if userdata.busy is True:
return

userdata.busy = True
userdata.newImageReceived = True
userdata.image = tis.get_image()
userdata.busy = False

#LED groups and corresponding exposure times
led_groups = {
6: {"wavelength": 660, "exposure_time": 1700},
7: {"wavelength": 730, "exposure_time": 2300},
8: {"wavelength": 760, "exposure_time": 1800},
10: {"wavelength": 810, "exposure_time": 2000},
11: {"wavelength": 850, "exposure_time": 1500},
12: {"wavelength": 0, "exposure_time": 1700},
}

#Initialize and configure the camera
Tis = TIS.TIS()
Tis.open_device("05521093-v4l2", 1920, 1080, "90/1", TIS.SinkFormats.GRAY16_LE)

Initialize and configure the TreatmentPCB

try:
Tis.set_property("TriggerMode", "Off")
Tis.set_property("GainAuto", "Off")
Tis.set_property("Gain", 0)
Tis.set_property("ExposureAuto", "Off")
#Tis.set_property("BalanceWhiteAuto", "Off")

except Exception as error:
print(f"Error setting camera properties: {error}")

#Create a base folder to save the images
output_folder = "DMM"
os.makedirs(output_folder, exist_ok=True)

#Create subfolders for each wavelength
for group, settings in led_groups.items():
wavelength_folder = os.path.join(output_folder, str(settings["wavelength"]))
os.makedirs(wavelength_folder, exist_ok=True)

#Set up the callback and start the pipeline
Tis.set_image_callback(on_new_image, CD)
Tis.set_property("TriggerMode", "On")
Tis.start_pipeline()

#Capture 2 images for each LED group
num_images = 2
print("Starting image capture...")

try:
for group, settings in led_groups.items():
wavelength = settings["wavelength"]
exposure_time = settings["exposure_time"]
Tis.set_property("ExposureTime", exposure_time)

    wavelength_folder = os.path.join(output_folder, str(wavelength))

    for i in range(num_images):
        #TURN ON LED for this group
       
        Tis.execute_command("TriggerSoftware")  # Trigger the capture

        #Wait for a new image
        tries = 5
        while CD.newImageReceived is False and tries > 0:
            time.sleep(0.1)
            tries -= 1

        if CD.newImageReceived is True:
            CD.newImageReceived = False
            
            distance = 1
            print(f"Distance = {distance/10} cm")
            #Save the captured image
            
            filename_raw = os.path.join(wavelength_folder, f"capture_{i + 1}_{wavelength}_distance_{distance:.2f}.raw")

            CD.image[0].tofile(filename_raw)

            print(f"Image {i + 1} for group {group} ({wavelength} nm) saved in {wavelength_folder}")
        else:
            print(f"Failed to capture image {i + 1} for group {group} ({wavelength} nm)")

    #Turn off LEDs for this group

except KeyboardInterrupt:
print("Capture interrupted by user.")

#Turn off all LEDs and stop the pipeline
Tis.set_property("TriggerMode", "Off")
Tis.stop_pipeline()
cv2.destroyAllWindows()

print("Image capture complete.")
`

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions