Polarization#

from itala import itala

system = itala.create_system()
devices_info = system.enumerate_devices(500)
if(len(devices_info) == 0):
    print("No devices found. Example canceled.")
    exit(1)
if(devices_info[0].access_status != itala.DeviceAccessStatus_AvailableReadWrite):
    print("Target device is unaccessible in RW mode. Example cancelled.")
    exit(1)
device = system.create_device(devices_info[0])
print("First device initialized.")
nodemap = device.node_map

pixel_format_node = nodemap.PixelFormat
if(not itala.is_writable(pixel_format_node)):
    print("Unable to configure the pixel format. Aborting.")
    exit(1)
original_pixel_format = pixel_format_node.get_int_value()
pixel_format_node.from_string("Polarized00Mono8")
print("PixelFormat set to Polarized00Mono8")

device.start_acquisition()
print("Acquisition started")

image = device.get_next_image(1000)

# Clone the grabbed image for convenience so that it can be returned to the library and the
# acquisition can be stopped.
polar_image = image.clone()
image.dispose()
device.stop_acquisition()

# Extract an image for each component (or angle) of the polarizer filter. Each resulting image
# represents the captured light at 0, 45, 90 and 135 degrees respectively. Depending on the
# demosaicing algorithm used, the pixel format of the resulting images can be of different types.
# NearestNeighbour on Polarized00Mono8 produces Mono8 components.
print("Extracting all polarization components..")
polarization_components = itala.extract_all_polar_components(polar_image, itala.PolarDemosaicingAlgorithm_NearestNeighbour)
print(polarization_components)
print("P45 format is: "+ str(itala.get_pixel_format_description(polarization_components.P45.pixel_format)))

# display or process polarization components

# Compute all the Stokes vectors to determine the polarization state of the light given the
# intensities of the different polarizers on the sensor. Since we're dealing with linear
# polarization data, only the first three (s0, s1 and s2) components of the vector are available.
# Each resulting image represents the values of that particular component of the Stokes vector
# across the whole sensor. For instance, the S0 image contains the s0 values of each pixel.
print("Computing all stokes vectors..")
stokes_vectors = itala.compute_all_stokes(polarization_components)
print("S0 format is: "+ str(itala.get_pixel_format_description(stokes_vectors.S0.pixel_format)))

# display or process the Stokes vectors

# Given the stokes vectors, compute the linear polarization images.
# The DoLP (Degree of Linear Polarization) image indicates the ratio of the intensity of the
# polarized part of the light to the intensity of the unpolarized one. When the light is totally
# polarized, this corresponds to the total intensity of the light. When the light is unpolarized,
# this corresponds to zero.
# The AoLP (Angle of Linear Polarization) image indicates the polarization direction of the
# incoming light.
# The intensity image simply indicate the total intensity of the incoming light.
print("\nComputing linear polarization images..")
dolp_image = itala.compute_do_lp(stokes_vectors)
aolp_image = itala.compute_ao_lp(stokes_vectors)
intensity_image = itala.compute_intensity(stokes_vectors, polar_image.pixel_format)
print("DoLP format is: "+ str(itala.get_pixel_format_description(dolp_image.pixel_format)))
print("AoLP format is: "+ str(itala.get_pixel_format_description(aolp_image.pixel_format)))
print("Intensity format is: "+ str(itala.get_pixel_format_description(intensity_image.pixel_format)))

# display or process the images

# From the source image, compute an image of the same size divided in four quadrants containing
# the four polarization components.
print("\nComputing quadrants image..")
quadrants_image = itala.compute_polar_quadrants_image(polar_image)
print("Quadrants image format is: "+ str(itala.get_pixel_format_description(quadrants_image.pixel_format)))

# Display or process the quadrants image

# If intermediate data like polar components or stokes vectors is not needed, monochrome AoLP,
# DoLP and intensity can be directly computed with the dedicated functions given the polarized
# image. For instance, DoLP:
print("\nCompute DoLP without intermediate steps..")
direct_dolp_image = itala.compute_do_lp(polar_image, itala.PolarDemosaicingAlgorithm_NearestNeighbour)

# Display or process DoLP

# Restore the original pixel format value
pixel_format_node.set_int_value(original_pixel_format)

# Each image MUST be disposed so that its memory is released. In real scenarios it's a good idea
# to release the images as long as they're no longer required to keep the memory footprint low.
polar_image.dispose()
polarization_components.P0.dispose()
polarization_components.P45.dispose()
polarization_components.P90.dispose()
polarization_components.P135.dispose()
stokes_vectors.S0.dispose()
stokes_vectors.S1.dispose()
stokes_vectors.S2.dispose()
dolp_image.dispose()
aolp_image.dispose()
intensity_image.dispose()
direct_dolp_image.dispose()
print("Image instances disposed.")

device.dispose()
print("Device instance disposed.")

system.dispose()
print("System instance disposed.")