Пример #1
0
    def __init__(self,
                 cam_num=0,
                 rotate=None,
                 crop=None,
                 exposure=None,
                 format='Y800 (720x540)'):
        '''
        Params
        ------
        cam_num = int; camera number (int)
            default = 0
        crop = dict; contains ints named top, left, height, width for cropping
            default = None, uses default parameters specific to camera
        '''

        self.ic_ic = IC_ImagingControl()
        self.ic_ic.init_library()

        self.cam_num = cam_num
        self.rotate = rotate if rotate is not None else cam_details[str(
            self.cam_num)]['rotate']
        self.crop = crop if crop is not None else cam_details[str(
            self.cam_num)]['crop']
        self.exposure = exposure if exposure is not None else cam_details[str(
            self.cam_num)]['exposure']

        self.cam = self.ic_ic.get_device(
            self.ic_ic.get_unique_device_names()[self.cam_num])
        self.cam.open()
        self.cam.set_video_format(format)
        self.add_filters()
    def __init__(self, cameraNo, exposure, gain, brightness):
        super().__init__()

        self.properties['subarray_vpos'] = 0
        self.properties['subarray_hpos'] = 0
        self.properties['exposure_time'] = 0.03
        self.properties['subarray_vsize'] = 1024
        self.properties['subarray_hsize'] = 1280

        from pyicic import IC_ImagingControl
        ic_ic = IC_ImagingControl.IC_ImagingControl()
        ic_ic.init_library()
        cam_names = ic_ic.get_unique_device_names()
        print(cam_names)
        self.cam = ic_ic.get_device(cam_names[cameraNo])
        # print(self.cam.list_property_names())

        self.cam.open()

        self.cam.colorenable = 0
        self.cam.gain.auto = False
        self.cam.exposure.auto = False
        if cameraNo == 1:
            self.cam.exposure = exposure  # exposure in ms
            self.cam.gain = gain  # gain in dB
            self.cam.brightness = brightness  # brightness in arbitrary units
            self.properties['subarray_vsize'] = 2048
            self.properties['subarray_hsize'] = 2448
        self.cam.enable_continuous_mode(True)  # image in continuous mode
        self.cam.start_live(show_display=False)  # start imaging
        # self.cam.enable_trigger(True)  # camera will wait for trigger
        # self.cam.send_trigger()
        if not self.cam.callback_registered:
            self.cam.register_frame_ready_callback(
            )  # needed to wait for frame ready callback
Пример #3
0
    def getFrame(self, t_exp=None, bitdepth=None):
        t_exp = t_exp or self.t_exp
        bitdepth = bitdepth or self.bitdepth

        # open lib
        ic_ic = IC_ImagingControl()
        ic_ic.init_library()

        cam_names = ic_ic.get_unique_device_names()
        cam = ic_ic.get_device(cam_names[0])
        cam.open()

        # change camera properties
        cam.gain.auto = False  # enable auto gain
        cam.gain.value = cam.gain.min
        t_exp_reg = int(
            np.round(np.log2(t_exp))
        )  # convert exposure time into register value (nearest power of 2)

        if t_exp_reg in range(cam.exposure.min, cam.exposure.max + 1):
            cam.exposure.value = int(np.round(np.log2(t_exp)))
        else:
            cam.exposure.value = int(cam.exposure.max + cam.exposure.min) / 2
            print('Exposure out of range. Setting to half of exposure range')

        cam.formats = cam.list_video_formats()

        cam.sensor_height = 1080
        cam.sensor_width = 1920
        cam.set_video_format(
            b'Y800 (1920x1080)')  # use first available video format
        cam.enable_continuous_mode(True)  # image in continuous mode
        cam.start_live(show_display=False)  # start imaging

        cam.enable_trigger(True)  # camera will wait for trigger
        if not cam.callback_registered:
            cam.register_frame_ready_callback(
            )  # needed to wait for frame ready callback

        cam.reset_frame_ready()  # reset frame ready flag

        # send hardware trigger OR call cam.send_trigger() here
        cam.send_trigger()
        # get image data...

        cam.wait_til_frame_ready(10)  # wait for frame ready due to trigger
        im = cam.get_image_data()

        img = np.ndarray(buffer=im[0],
                         dtype=np.uint8,
                         shape=(cam.sensor_height, cam.sensor_width, 3))

        cam.stop_live()
        cam.close()

        ic_ic.close_library()
Пример #4
0
    def __init__(self, cameraNo):
        super().__init__()

        ic_ic = IC_ImagingControl.IC_ImagingControl()
        ic_ic.init_library()
        cam_names = ic_ic.get_unique_device_names()
        self.model = cam_names[cameraNo]
        self.cam = ic_ic.get_device(cam_names[cameraNo])

        self.cam.open()

        self.shape = (0, 0)
        self.cam.colorenable = 0

        self.cam.enable_continuous_mode(True)  # image in continuous mode
        self.cam.enable_trigger(False)  # camera will wait for trigger

        self.roi_filter = self.cam.create_frame_filter('ROI'.encode('utf-8'))
        self.cam.add_frame_filter_to_device(self.roi_filter)
Пример #5
0
import time

import numpy as np

from pyicic.IC_ImagingControl import *
from pyicic.IC_Camera import C_FRAME_READY_CALLBACK

ic = IC_ImagingControl()
ic.init_library()

cam_names = ic.get_unique_device_names()
print cam_names
device_name = cam_names[0]

print device_name

cam = ic.get_device(device_name)
cam.open()
cam.reset_properties()

formats = cam.list_video_formats()
print formats
cam.set_video_format(formats[2])

print 'get_available_frame_filter_count:', cam.get_available_frame_filter_count()
print 'get_available_frame_filters:', cam.get_available_frame_filters(cam.get_available_frame_filter_count())

h = cam.create_frame_filter('Rotate Flip')
h2 = cam.create_frame_filter('ROI')
cam.add_frame_filter_to_device(h)
cam.add_frame_filter_to_device(h2)
class DAQ_2DViewer_TIS(DAQ_Viewer_base):
    """
        =============== ==================
        **Attributes**   **Type**
        *params*         dictionnary list
        *x_axis*         1D numpy array
        *y_axis*         1D numpy array
        =============== ==================

        See Also
        --------
        utility_classes.DAQ_Viewer_base
    """
    from pyicic import IC_ImagingControl

    ic = IC_ImagingControl.IC_ImagingControl()
    ic.init_library()
    cameras = [cam.decode() for cam in ic.get_unique_device_names()]

    params = comon_parameters + \
             [{'title': 'Cam. names:', 'name': 'cam_name', 'type': 'list', 'limits': cameras},
              {'title': 'Video Formats:', 'name': 'video_formats', 'type': 'list'},
              {'title': 'Gray scale:', 'name': 'gray_scale', 'type': 'bool', 'value': False},
              {'title': 'Cam. Prop.:', 'name': 'cam_settings', 'type': 'group', 'children': [
                  {'title': 'Brightness:', 'name': 'brightness', 'type': 'int'},
                  {'title': 'Contrast:', 'name': 'contrast', 'type': 'int'},
                  {'title': 'Hue:', 'name': 'hue', 'type': 'int'},
                  {'title': 'Saturation:', 'name': 'saturation', 'type': 'int'},
                  {'title': 'Sharpness:', 'name': 'sharpness', 'type': 'int'},
                  {'title': 'Gamma:', 'name': 'gamma', 'type': 'int'},
                  {'title': 'Color?:', 'name': 'colorenable', 'type': 'bool'},
                  {'title': 'Whitebalance:', 'name': 'whitebalance', 'type': 'int'},
                  {'title': 'Black light compensation:', 'name': 'blacklightcompensation', 'type': 'int'},
                  {'title': 'Gain:', 'name': 'gain', 'type': 'int'},
                  {'title': 'Pan:', 'name': 'pan', 'type': 'int'},
                  {'title': 'Tilt:', 'name': 'tilt', 'type': 'int'},
                  {'title': 'Roll:', 'name': 'roll', 'type': 'int'},
                  {'title': 'Zoom:', 'name': 'zoom', 'type': 'int'},
                  {'title': 'Exposure:', 'name': 'exposure', 'type': 'int'},
                  {'title': 'Iris:', 'name': 'iris', 'type': 'int'},
                  {'title': 'Focus:', 'name': 'focus', 'type': 'int'},

              ]},

              ]

    def __init__(
        self,
        parent=None,
        params_state=None
    ):  # init_params is a list of tuple where each tuple contains info on a 1D channel (Ntps,amplitude, width, position and noise)
        super(DAQ_2DViewer_TIS, self).__init__(parent, params_state)
        self.x_axis = None
        self.y_axis = None
        self.live = False

        from pyicic import IC_Structures
        GrabberHandlePtr = ctypes.POINTER(IC_Structures.GrabberHandle)
        # c function type for frame callback
        # outside of class so it can be called by unbound function
        callback = ctypes.WINFUNCTYPE(None, GrabberHandlePtr,
                                      ctypes.POINTER(ctypes.c_ubyte),
                                      ctypes.c_ulong, ctypes.c_void_p)
        self.__data_ready = callback(self._data_ready)

    def _data_ready(self, handle_ptr, p_data, frame_num, data):
        dat = self.controller.get_image_data()
        data = np.array(dat[0][:], dtype=np.uint8)
        data = data.reshape((dat[2], dat[1], 3))
        self.data_grabed_signal.emit([
            DataFromPlugins(name='TIS ',
                            data=[data[:, :, 0], data[:, :, 1], data[:, :, 2]],
                            dim='Data2D'),
        ])

    def commit_settings(self, param):
        """
            Activate parameters changes on the hardware.

            =============== ================================ ===========================
            **Parameters**   **Type**                          **Description**
            *param*          instance of pyqtgraph Parameter   the parameter to activate
            =============== ================================ ===========================

            See Also
            --------
            set_Mock_data
        """
        try:
            if param.parent().name() == 'cam_settings':
                getattr(self.controller, param.name()).value = param.value()
                param.setValue(getattr(self.controller, param.name()).value)
            elif param.name() == 'video_formats':
                self.controller.stop_live()
                self.controller.set_video_format(param.value().encode())
                # if 'Y' in param.value():
                #     self.controller.set_format(0)
                # else:
                #     self.controller.set_format(1)
                self.controller.start_live()
        except Exception as e:
            self.emit_status(
                ThreadCommand('Update_Status',
                              [getLineInfo() + str(e), 'log']))

    def ini_detector(self, controller=None):
        """
            Initialisation procedure of the detector initializing the status dictionnary.

            See Also
            --------
            daq_utils.ThreadCommand, get_xaxis, get_yaxis
        """
        self.status.update(
            edict(initialized=False,
                  info="",
                  x_axis=None,
                  y_axis=None,
                  controller=None))
        try:

            if self.settings.child(('controller_status')).value() == "Slave":
                if controller is None:
                    raise Exception(
                        'no controller has been defined externally while this detector is a slave one'
                    )
                else:
                    self.controller = controller
            else:

                self.controller = self.ic.get_device(
                    self.settings.child(('cam_name')).value().encode())
                self.controller.open()

            properties = self.controller.list_property_names()
            for prop in properties:
                if prop in [
                        child.name() for child in self.settings.child((
                            'cam_settings')).children()
                ]:
                    if getattr(self.controller, prop).available:
                        param = self.settings.child('cam_settings', prop)
                        if param.opts['type'] == 'int' or param.opts[
                                'type'] == 'float':
                            range = getattr(self.controller, prop).range
                            param.setOpts(limits=range)
                        try:
                            getattr(self.controller, prop).auto = False
                        except:
                            pass
                        value = getattr(self.controller, prop).value
                        param.setValue(value)
                    else:
                        self.settings.child('cam_settings', prop).hide()

            formats = [
                form.decode() for form in self.controller.list_video_formats()
            ]  # if 'RGB'.encode() in form]
            self.settings.child(('video_formats')).setOpts(limits=formats)
            self.settings.child(('video_formats')).setValue(formats[8])
            self.controller.set_video_format(
                formats[8].encode())  # use first available video format

            self.controller.enable_continuous_mode(
                True)  # image in continuous mode
            self.controller.start_live(show_display=False)  # start imaging
            self.controller.enable_trigger(
                True)  # camera will wait for trigger

            if not self.controller.callback_registered:
                self.controller.register_frame_ready_callback(
                    self.__data_ready
                )  # needed to wait for frame ready callback

            self.controller.send_trigger()

            self.x_axis = self.get_xaxis()
            self.y_axis = self.get_yaxis()

            # initialize viewers with the future type of data
            self.data_grabed_signal_temp.emit([
                DataFromPlugins(
                    name='TIS',
                    data=[np.zeros((len(self.y_axis), len(self.x_axis)))],
                    dim='Data2D'),
            ])

            self.status.x_axis = self.x_axis
            self.status.y_axis = self.y_axis
            self.status.initialized = True
            self.status.controller = self.controller
            return self.status

        except Exception as e:
            self.emit_status(
                ThreadCommand('Update_Status',
                              [getLineInfo() + str(e), 'log']))
            self.status.info = getLineInfo() + str(e)
            self.status.initialized = False
            return self.status

    def close(self):
        """
            not implemented.
        """
        self.controller.stop_live()
        self.controller.close()

        self.ic.close_library()

    def get_xaxis(self):
        """
            Get the current x_axis from the Mock data setting.

            Returns
            -------
            1D numpy array
                the current x_axis.

            See Also
            --------
            set_Mock_data
        """
        Nx = self.controller.get_video_format_width()
        self.x_axis = np.linspace(0, Nx - 1, Nx, dtype=np.int32)
        return self.x_axis

    def get_yaxis(self):
        """
            Get the current y_axis from the Mock data setting.

            Returns
            -------
            1D numpy array
                the current y_axis.

            See Also
            --------
            set_Mock_data
        """
        Ny = self.controller.get_video_format_height()
        self.y_axis = np.linspace(0, Ny - 1, Ny, dtype=np.int32)
        return self.y_axis

    def grab_data(self, Naverage=1, **kwargs):
        """
            | For each integer step of naverage range set mock data.
            | Construct the data matrix and send the data_grabed_signal once done.

            =============== ======== ===============================================
            **Parameters**  **Type**  **Description**
            *Naverage*      int       The number of images to average.
                                      specify the threshold of the mean calculation
            =============== ======== ===============================================

            See Also
            --------
            set_Mock_data
        """
        # self.controller.reset_frame_ready()
        if not self.controller.is_live():
            self.controller.enable_continuous_mode(
                True)  # image in continuous mode
            self.controller.start_live(show_display=False)  # start imaging
            self.controller.enable_trigger(True)

        self.controller.send_trigger()
        # self.controller.wait_til_frame_ready(1000)
        # self.controller.snap_image()

    def stop(self):
        """
            not implemented.
        """
        self.controller.stop_live()
        return ""
Пример #7
0
    def __init__(self, device_string, fom_num):
        """
        Initialize the Imaging Source camera.

        Set all of the parameters for the chosen Imaging Source camera so that it is ready to 
        take an image.

        Parameters
        ----------
        device_string : str
            A string naming which daq device to use.
        fom_num : int
            An int denoting which figure of merit this daq device will be using.

        """
        super().__init__(device_string,
                         fom_num)  # call the daq_device __init__ function

        self.ic_ic = IC_ImagingControl(
        )  # initialize the imaging control grabber
        self.ic_ic.init_library(
        )  # Use the grabber to initialze the library of IC functions we can use

        # Determine the Imaging Source cameras connected to the computer
        cam_names = self.ic_ic.get_unique_device_names()
        if (len(cam_names) == 0):  # no IC camera is connected
            print("Error: No IC cameras connected to the computer.")
            exit()
        print("\nThese are the available cameras:")
        print(cam_names)
        print(
            "Please select an IC camera to use by inputting the index of the camera."
        )
        print("The indices go from 0 to ", len(cam_names) - 1)

        # Iterate through an infinite loop until the user defines which camera they want to use
        while True:
            index = int(input())
            if ((index <= len(cam_names) - 1) and (index >= 0)):
                self.cam = self.ic_ic.get_device(cam_names[index])
                break
            else:
                print("You didn't enter a correct index.")

        self.cam.open()  # open the camera they chose

        # Go through an infinite loop with user to decide to initialize with the .ini file or by setting all of the values
        print(
            "\nWould you like to set all of the camera initialization values yourself, or use the IC properties.ini file?"
        )
        print(
            'Enter either "set" for setting all of the values or "ini" for the .ini file'
        )
        while True:
            init = input()
            if (init == "set"):
                set_all = True
                break
            elif (init == "ini"):
                set_all = False
                break
            else:
                print("You didn't enter 'set' or 'ini'. Try again.")

        # reset all properties before setting them
        self.cam.reset_properties()

        # Go through each property available and set its value
        cam_properties = self.cam.list_property_names()
        print(
            "Note: this only goes through the camera properties available for this specific camera."
        )
        for attribute_index in range(
                len(cam_properties
                    )):  # for loop through each of the camera properties
            if (getattr(self.cam, cam_properties[attribute_index]).available ==
                    True):  # if the attribute can be set
                if (set_all == True):  # if the user wants to set everything
                    print("You are setting the",
                          cam_properties[attribute_index])
                    print(
                        "Its current value is ",
                        getattr(self.cam,
                                cam_properties[attribute_index]).value)
                    print(
                        "The range of values you can set this to is ",
                        getattr(self.cam,
                                cam_properties[attribute_index]).range)
                    print("What would you like to set this property to?")
                    while True:
                        change_value = input()
                        print("You entered", change_value,
                              "\nIs this okay? (enter 'y' or 'n')")
                        input_is_good = input()
                        if (input_is_good == 'y'):
                            break
                        elif (input_is_good == 'n'):
                            print(
                                "Type in what you'd like to change this property to instead"
                            )
                        else:
                            print(
                                "You didn't enter a y or an n. Enter what value you'd like to change the property to again."
                            )
                else:  # if the user is using the .ini file
                    if (self.initialize_array[attribute_index] == "auto"
                        ):  # if the .ini file has "auto" for this property
                        if (getattr(
                                self.cam,
                                cam_properties[attribute_index]).auto_available
                                == True):
                            getattr(
                                self.cam,
                                cam_properties[attribute_index]).auto = True
                            print("Set the camera",
                                  cam_properties[attribute_index], "to auto")
                        else:
                            print("Auto setting unavailable for",
                                  cam_properties[attribute_index])
                            print("Did not set",
                                  cam_properties[attribute_index])
                    elif (self.initialize_array[attribute_index] == "none"
                          ):  # if the .ini file has "none" for this property
                        print("Did not set", cam_properties[attribute_index])
                    else:  # if the .ini file has a value for its setting
                        if (type(
                                getattr(self.cam,
                                        cam_properties[attribute_index]).value)
                                == int):
                            getattr(
                                self.cam,
                                cam_properties[attribute_index]).value = int(
                                    self.initialize_array[attribute_index])
                        if (type(
                                getattr(self.cam,
                                        cam_properties[attribute_index]).value)
                                == float):
                            getattr(
                                self.cam,
                                cam_properties[attribute_index]).value = float(
                                    self.initialize_array[attribute_index])
                        print(
                            "Set the camera", cam_properties[attribute_index],
                            "to",
                            getattr(self.cam,
                                    cam_properties[attribute_index]).value,
                            "within the range",
                            getattr(self.cam,
                                    cam_properties[attribute_index]).range)

        # the last property in the .ini file is whether the person wants the trigger
        self.software_trigger = (
            self.initialize_array[len(cam_properties)]
        ) == "True"  # set the software trigger to True or False

        # Determine the video format the user would like to use
        formats = self.cam.list_video_formats()
        print("\nThese are the available video formats:")
        print(formats)
        print(
            "Please select video format to use by inputting the index of the format."
        )
        print("The indices go from 0 to ", len(formats) - 1)

        # Iterate through an infinite loop until the user defines which video format they want to use
        while True:
            self.video_index = int(input())
            if ((self.video_index <= len(formats) - 1)
                    and (self.video_index >= 0)):
                self.cam.set_video_format(formats[self.video_index])
                break
            else:
                print("You didn't enter a correct index.")
        current_video_format = self.cam.get_video_format(
            self.video_index)  # set the video format

        # if the video format stores the pixels left to right and top to bottom, set flip_image to True
        if any(string in str(current_video_format)
               for string in PIXEL_FORMAT_TOP_DOWN):
            self.flip_image = True
        else:
            self.flip_image = False

        self.cam.enable_continuous_mode(True)  # image in continuous mode
        self.cam.start_live(show_display=False)  # start imaging

        self.cam.enable_trigger(
            self.software_trigger)  # camera will wait for trigger
        if not self.cam.callback_registered:
            self.cam.register_frame_ready_callback(
            )  # needed to wait for frame ready callback

        # determine the image dimensions
        self.width, self.height, depth, color_format = self.cam.get_image_description(
        )
        self.depth = depth // 8  # I have no idea why the open source pyicic library does this

        # take an image because for some cameras, the first image doesn't work correctly and then the rest work
        self.__acquire()
Пример #8
0
    def _previewCam(self, t_exp, bitdepth, displaythresh):
        t_exp = t_exp or self.t_exp
        bitdepth = bitdepth or self.bitdepth

        # open lib
        ic_ic = IC_ImagingControl()
        ic_ic.init_library()

        cam_names = ic_ic.get_unique_device_names()
        cam = ic_ic.get_device(cam_names[0])
        cam.open()

        # change camera properties
        cam.gain.auto = False  # enable auto gain
        cam.gain.value = cam.gain.min

        t_exp_reg = int(
            np.round(np.log2(t_exp))
        )  # convert exposure time into register value (nearest power of 2)

        if t_exp_reg in range(cam.exposure.min, cam.exposure.max + 1):
            cam.exposure.value = int(np.round(np.log2(t_exp)))
        else:
            cam.exposure.value = int(cam.exposure.max + cam.exposure.min) / 2
            print('Exposure out of range. Setting to half of exposure range')
        cam.formats = cam.list_video_formats()

        cam.sensor_height = 1080
        cam.sensor_width = 1920
        cam.set_video_format(
            b'Y800 (1920x1080)')  # use first available video format
        cam.enable_continuous_mode(True)  # image in continuous mode
        cam.start_live(show_display=False)  # start imaging

        cam.enable_trigger(True)  # camera will wait for trigger
        if not cam.callback_registered:
            cam.register_frame_ready_callback(
            )  # needed to wait for frame ready callback

        window_width = 800
        resize_scale = window_width / cam.sensor_width
        rescaled_size = (window_width, int(cam.sensor_height * resize_scale))

        cv2.namedWindow('Camera Preview',
                        flags=cv2.WINDOW_KEEPRATIO | cv2.WINDOW_NORMAL)
        cv2.resizeWindow('Camera Preview', rescaled_size[0], rescaled_size[1])

        class MouseControl:
            def __init__(self):
                self.xy = [0, 0]
                self.im_val = 0
                self.stopflag = 0
                self.markers = 0

            def process_events(self, event, x, y, flags, param):
                if event == cv2.EVENT_LBUTTONDOWN:
                    self.stopflag = not (self.stopflag)
                elif event == cv2.EVENT_MOUSEMOVE:
                    x_val = int(x)
                    y_val = int(y)
                    self.xy = [x_val, y_val]
                elif event == cv2.EVENT_RBUTTONDOWN:
                    self.markers = (self.markers + 1) % 3

        #instantiate class
        mouse = MouseControl()

        cv2.setMouseCallback('Camera Preview', mouse.process_events)

        while (cv2.getWindowProperty('Camera Preview', 0) >= 0):
            try:
                if not (mouse.stopflag):
                    t = time.time()
                    cam.reset_frame_ready()  # reset frame ready flag

                    # send hardware trigger OR call cam.send_trigger() here
                    cam.send_trigger()
                    # get image data...

                    cam.wait_til_frame_ready(
                        10)  # wait for frame ready due to trigger
                    im = cam.get_image_data()

                    img = np.ndarray(buffer=im[0],
                                     dtype=np.uint8,
                                     shape=(cam.sensor_height,
                                            cam.sensor_width, 3))

                    cv2.imshow('Camera Preview', img)
                    k = cv2.waitKey(1)

                    if k == 0x1b:
                        cv2.destroyAllWindows()
                        break

                    fps = 1 / (time.time() - t)
                else:

                    cv2.imshow('Camera Preview', img)

                    k = cv2.waitKey(1)

                    if k == 0x1b:
                        cv2.destroyAllWindows()
                        break
            except:
                print('There was an error')
                cam.stop_live()
                cam.close()
                ic_ic.close_library()
                raise

        cam.stop_live()
        cam.close()

        ic_ic.close_library()
def ic():

    ic_ic = IC_ImagingControl()
    ic_ic.init_library()

    # open first available camera device
    cam_names = ic_ic.get_unique_device_names()
    # print(cam_names)
    cam = ic_ic.get_device(cam_names[0])
    cam.open()
    cam.reset_properties()

    # change camera properties
    # print(cam.list_property_names())         # ['gain', 'exposure', 'hue', etc...]
    cam.gain.auto = False  # enable auto gain

    cam.exposure.value = -5

    # change camera settings
    formats = cam.list_video_formats()
    # print formats
    cam.set_video_format(formats[0])  # use first available video format
    cam.enable_continuous_mode(True)  # image in continuous mode
    cam.start_live(show_display=False)  # start imaging

    cam.enable_trigger(True)  # camera will wait for trigger
    if not cam.callback_registered:
        cam.register_frame_ready_callback(
        )  # needed to wait for frame ready callback

    cam.reset_frame_ready()

    cam.send_trigger()

    cam.wait_til_frame_ready(1000)  # wait for frame ready due to trigger

    data, width, height, depth = cam.get_image_data()
    frame = np.ndarray(buffer=data,
                       dtype=np.uint8,
                       shape=(height, width, depth))
    frameout = copy.deepcopy(frame).astype(float)

    del frame
    # print(frameout.max())

    cam.stop_live()
    cam.close()

    ic_ic.close_library()

    imgray = rgb2gray(frameout)  # convert rgb image into grayscale

    satu = imgray[imgray > 254].shape[0]
    if satu > 0:
        print('Image saturated with %d pixels' % satu)
        return 0
    else:

        # FOM1
        # I = abs(imgray)**2
        # x = np.arange(imgray.shape[1]).astype(float)
        # y = np.arange(imgray.shape[0]).astype(float)
        # mu0 = np.trapz(np.trapz(I, x ),y)
        # mean_x = np.trapz(np.trapz(I * x, x), y)/mu0
        # mean_y = np.trapz(np.trapz(I, x)*y, y)/mu0
        # r0 = 50
        # X, Y= np.meshgrid(x,y)
        # r = (Y - mean_y)**2 + (X - mean_x)**2
        # fom = (1-np.sum(imgray[r>=r0**2]) / np.sum(imgray) ) * np.sum(imgray[r<r0**2])
        # y_peak, x_peak = np.unravel_index(imgray.argmax(), imgray.shape) # find the target position for FOM calculation, here the maximum point is the target position

        #FOM2 (Image Moment)
        # x_peak = 520
        # y_peak = 554
        # xx = np.arange(imgray.shape[1]).astype(float)
        # yy = np.arange(imgray.shape[0]).astype(float)
        # X, Y= np.meshgrid(xx,yy)
        # d1 = (Y - y_peak)**2
        # d2 = (X - x_peak)**2
        # d = (d1+d2)**4
        # d[y_peak,x_peak]=1
        # fom = imgray / d
        # fom[y_peak,x_peak]=0
        # fom = np.sum(fom)

        #FOM3
        # fom = np.sum(imgray**2);

        #FOM4
        fom = np.sum(imgray)

        print(frameout.max(), fom)
        return fom
Пример #10
0
def ic():

    ic_ic = IC_ImagingControl()
    ic_ic.init_library()

    cam_names = ic_ic.get_unique_device_names()
    print("These are the available cameras:")
    print(cam_names)
    print(
        "Please select an IC camera to use by inputting the index of the camera."
    )
    print("The indices go from 0 to ", len(cam_names) - 1)
    while True:
        index = int(input())
        if ((index <= len(cam_names) - 1) and (index >= 0)):
            cam = ic_ic.get_device(cam_names[index])
            break
        else:
            print("You didn't enter a correct index.")

    cam.open()
    cam.reset_properties()

    # change camera properties
    print(cam.list_property_names())  # ['gain', 'exposure', 'hue', etc...]
    cam.gain.auto = False  # enable auto gain

    cam.exposure.value = -5

    # change camera settings
    formats = cam.list_video_formats()
    # print formats
    print(formats)
    cam.set_video_format(formats[0])  # use first available video format
    cam.enable_continuous_mode(True)  # image in continuous mode
    cam.start_live(show_display=False)  # start imaging

    cam.enable_trigger(True)  # camera will wait for trigger
    if not cam.callback_registered:
        cam.register_frame_ready_callback(
        )  # needed to wait for frame ready callback

    cam.reset_frame_ready()

    cam.send_trigger()

    cam.wait_til_frame_ready(1000)  # wait for frame ready due to trigger

    data, width, height, depth = cam.get_image_data()
    frame = np.ndarray(buffer=data,
                       dtype=np.uint8,
                       shape=(height, width, depth))
    frameout = copy.deepcopy(frame).astype(float)
    plt.imsave('figure_of_merit.png', frameout, cmap=cm.gray)
    del frame
    # print(frameout.max())

    cam.stop_live()  # stop capturing video from the camera
    cam.close()  # shut down the camera

    ic_ic.close_library()  # stop accessing the IC dll

    return frameout
Пример #11
0
    def __initialize_IC(self, initialize_array):
        """TODO comments"""
        self.ic_ic = IC_ImagingControl()
        self.ic_ic.init_library()

        cam_names = self.ic_ic.get_unique_device_names()
        if (len(cam_names) == 0):
            print("Error: No IC cameras connected to the computer.")
            exit()
        print("\nThese are the available cameras:")
        print(cam_names)
        print(
            "Please select an IC camera to use by inputting the index of the camera."
        )
        print("The indices go from 0 to ", len(cam_names) - 1)
        while True:
            index = int(input())
            if ((index <= len(cam_names) - 1) and (index >= 0)):
                self.cam = self.ic_ic.get_device(cam_names[index])
                break
            else:
                print("You didn't enter a correct index.")

        self.cam.open()

        print(
            "\nWould you like to set all of the camera initialization values yourself, or use the IC properties.ini file?"
        )
        print(
            'Enter either "set" for setting all of the values or "ini" for the .ini file'
        )
        while True:
            init = input()
            if (init == "set"):
                set_all = True
                break
            elif (init == "ini"):
                set_all = False
                break
            else:
                print("You didn't enter 'set' or 'ini'. Try again.")

        self.cam.reset_properties()
        cam_properties = self.cam.list_property_names()
        print(
            "Note: this only goes through the camera properties available for this specific camera."
        )

        for attribute_index in range(len(cam_properties)):
            if (getattr(self.cam,
                        cam_properties[attribute_index]).available == True):
                if (set_all == True):  # if they want to go through everything
                    print("You are setting the",
                          cam_properties[attribute_index])
                    print(
                        "Its current value is ",
                        getattr(self.cam,
                                cam_properties[attribute_index]).value)
                    print(
                        "The range of values you can set this to is ",
                        getattr(self.cam,
                                cam_properties[attribute_index]).range)
                    print("What would you like to set this property to?")
                    while True:
                        change_value = input()
                        print("You entered", change_value,
                              "\nIs this okay? (enter 'y' or 'n')")
                        input_is_good = input()
                        if (input_is_good == 'y'):
                            break
                        elif (input_is_good == 'n'):
                            print(
                                "Type in what you'd like to change this property to instead"
                            )
                        else:
                            print(
                                "You didn't enter a y or an n. Enter what value you'd like to change the property to again."
                            )
                else:
                    if (initialize_array[attribute_index] == "auto"):
                        if (getattr(
                                self.cam,
                                cam_properties[attribute_index]).auto_available
                                == True):
                            getattr(
                                self.cam,
                                cam_properties[attribute_index]).auto = True
                            print("Set the camera",
                                  cam_properties[attribute_index], "to auto")
                        else:
                            print("Auto setting unavailable for",
                                  cam_properties[attribute_index])
                            print("Did not set",
                                  cam_properties[attribute_index])
                    elif (initialize_array[attribute_index] == "none"):
                        print("Did not set", cam_properties[attribute_index])
                    else:
                        if (type(
                                getattr(self.cam,
                                        cam_properties[attribute_index]).value)
                                == int):
                            getattr(
                                self.cam,
                                cam_properties[attribute_index]).value = int(
                                    initialize_array[attribute_index])
                        if (type(
                                getattr(self.cam,
                                        cam_properties[attribute_index]).value)
                                == float):
                            getattr(
                                self.cam,
                                cam_properties[attribute_index]).value = float(
                                    initialize_array[attribute_index])
                        print(
                            "Set the camera", cam_properties[attribute_index],
                            "to",
                            getattr(self.cam,
                                    cam_properties[attribute_index]).value,
                            "within the range",
                            getattr(self.cam,
                                    cam_properties[attribute_index]).range)

        formats = self.cam.list_video_formats()
        print("\nThese are the available video formats:")
        print(formats)
        print(
            "Please select video format to use by inputting the index of the format."
        )
        print("The indices go from 0 to ", len(formats) - 1)
        while True:
            index = int(input())
            if ((index <= len(formats) - 1) and (index >= 0)):
                self.cam.set_video_format(formats[index])
                break
            else:
                print("You didn't enter a correct index.")

        self.cam.enable_continuous_mode(True)  # image in continuous mode
        self.cam.start_live(show_display=False)  # start imaging

        self.cam.enable_trigger(True)  # camera will wait for trigger
        if not self.cam.callback_registered:
            self.cam.register_frame_ready_callback(
            )  # needed to wait for frame ready callback

        self.width, self.height, depth, color_format = self.cam.get_image_description(
        )
        self.depth = depth // 8  # I have no idea why the library does this
        self.acquire()