DefectivePixelCorrection#

DefectivePixelCorrection.py#
from itala import itala


def correct_detected_pixels(defective_pixels: itala.DefectivePixelList, node_map: itala.INodeMap):
    defective_pixel_count_node = node_map.get_node("oeDefectivePixelCount")
    if(not itala.is_writable(defective_pixel_count_node)):
        print("Unable to configure eDefectivePixelCount. Aborting,")
        exit(1)
    defective_pixel_count_node.value = len(defective_pixels)
    print(f"Correction procedure started with {len(defective_pixels)} pixels")

    defective_pixel_selector_node = node_map.get_node("oeDefectivePixelSelector")
    if(not itala.is_writable(defective_pixel_selector_node)):
        print("Unable to configure oeDefectivePixelSelector. Aborting,")
        exit(1)

    defective_pixel_coordinate_x_node = node_map.get_node("oeDefectivePixelXCoordinate")
    if(not itala.is_writable(defective_pixel_coordinate_x_node)):
        print("Unable to configure oeDefectivePixelXCoordinate. Aborting,")
        exit(1)
    defective_pixel_coordinate_y_node = node_map.get_node("oeDefectivePixelYCoordinate")
    if(not itala.is_writable(defective_pixel_coordinate_y_node)):
        print("Unable to configure oeDefectivePixelYCoordinate. Aborting,")
        exit(1)

    for i in range(len(defective_pixels)):
        defective_pixel_selector_node.value = i
        defective_pixel_coordinate_x_node.value = defective_pixels[i].x
        defective_pixel_coordinate_y_node.value = defective_pixels[i].y

        print(f"Pixel [{defective_pixels[i].x},{defective_pixels[i].y}] registered for correction. Was {defective_pixels[i].type_string}.")

    defective_pixel_write_map_node = node_map.get_node("oeDefectivePixelWriteMap")
    if(not itala.is_writable(defective_pixel_write_map_node)):
        print("Unable to configure oeDefectivePixelWriteMap. Aborting,")
        exit(1)
    defective_pixel_write_map_node.execute()

    print("Correction command sent, procedure completed.")


def defective_pixel_correction():
    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

    # Start by setting a full resolution Mono8 image to perform the
    # correction on the whole sensor. Keep the original values to 
    # restore the initial camera state at the end of the program.
    width_node = nodemap.Width
    if(not itala.is_writable(width_node)):
        print("Unable to configure the image width. Aborting.")
        exit(1)
    width_original = width_node.value
    width_node.value = width_node.max
    print("Image width set to max.")

    height_node = nodemap.Height
    if(not itala.is_writable(height_node)):
        print("Unable to configure the image height. Aborting,")
        exit(1)
    height_original = height_node.value
    height_node.value = height_node.max
    print("Image height set to max.")

    pixel_format_node = nodemap.PixelFormat
    if(not itala.is_writable(pixel_format_node)):
        print("Unable to configure the image PixelFormat. Aborting,")
        exit(1)
    pixel_format_original = pixel_format_node.get_int_value()
    mono8_entry = pixel_format_node.get_entry_by_name("Mono8")
    mono8_format = mono8_entry.value
    pixel_format_node.set_int_value(mono8_format)
    print("Pixel format set to Mono8")

    mono8_detection = itala.create_defect_detection(width_node.max, height_node.max, itala.PixelDepth_d8Bit)

    device.start_acquisition()
    print("Acquisition started")
    for i in range(10):
        dark_image = device.get_next_image(500)
        buffer = dark_image.get_data()
        buff = itala.buffer.frompointer(buffer)
        mono8_detection.accumulate_dark(buff.cast())
        print(f"Accumulating dark: image {i+1} added.", end="\r")
        dark_image.dispose()

    for i in range(10):
        dark_image = device.get_next_image(500)
        buffer = dark_image.get_data()
        buff = itala.buffer.frompointer(buffer)
        mono8_detection.accumulate_gray(buff.cast())
        print(f"Accumulating grey: image {i+1} added.", end="\r")
        dark_image.dispose()

    device.stop_acquisition()
    print("Acquisition stopped")

    leaky_pixel = mono8_detection.find_leaky(1024, 5)
    hot_n_cold_pixel = mono8_detection.find_hot_ncold(1024, 5)
    print(leaky_pixel)
    print(f"{leaky_pixel.size()} leaky pixels found.")
    print(f"{hot_n_cold_pixel.size()} hot/cold pixels found.")

    if leaky_pixel.size()>0:
        correct_detected_pixels(leaky_pixel, nodemap)
    if hot_n_cold_pixel.size()>0:
        correct_detected_pixels(hot_n_cold_pixel, nodemap)

    mono8_detection.dispose()
    print("Mono8 detection instance disposed")
    width_node.value = width_original
    height_node.value = height_original
    pixel_format_node.set_int_value(pixel_format_original)

    device.dispose()
    system.dispose()


if __name__ == "__main__":
    try:
        defective_pixel_correction()
    except Exception as e:
        print(e)
        exit(1)
    exit(0)