Exemplo n.º 1
0
def capture_image(cam, gripper_height):
    camera_height = gripper_height + 70  # Camera is placed 70mm above gripper
    # TODO: Find a curve that correlates distance from subject and focus value
    if camera_height > 300:
        nRet = ueye.is_Focus(cam.handle(), ueye.FOC_CMD_SET_MANUAL_FOCUS,
                             config.focus_overview,
                             ueye.sizeof(config.focus_overview))
    else:
        nRet = ueye.is_Focus(cam.handle(), ueye.FOC_CMD_SET_MANUAL_FOCUS,
                             config.focus_closeup,
                             ueye.sizeof(config.focus_closeup))

    #nRet = ueye.is_Focus(cam.handle(), ueye.FOC_CMD_SET_ENABLE_AUTOFOCUS_ONCE, None, 0)

    # autofocus_status = ueye.INT(0)
    # ueye.is_Focus(cam.handle(), ueye.FOC_CMD_GET_AUTOFOCUS_STATUS, autofocus_status, ueye.sizeof(autofocus_status))
    img_buffer = ImageBuffer()  # Create image_tools buffer
    #ueye.is_Focus(cam.handle(), ueye.FOC_CMD_SET_ENABLE_AUTOFOCUS_ONCE, None, 0)
    time.sleep(0.5)
    # cam.freeze_video(True)  # Freeze video captures a single image_tools

    nRet = ueye.is_WaitForNextImage(cam.handle(), 1000, img_buffer.mem_ptr,
                                    img_buffer.mem_id)
    img_data = ImageData(cam.handle(), img_buffer)
    array = img_data.as_1d_image()
    img_data.unlock()

    return array
def calculate_focus(cam, working_distance):
    """This characteristic belongs to the IDS XS camera with serial code 4102885308.
    As stated by IDS themselves the characteristic is not robust and could vary between
    different cameras.
    The characteristic was made based on images up to a working distance of 620mm.
    """
    # TODO: Make characteristics for all IDS XS cameras at UiS.
    #  By checking the serial code through ueye.SENSORINFO, one would know which specific camera is in use

    if working_distance >= 357.5:
        focus_value = 204
    elif 237 <= working_distance < 357.5:
        focus_value = 192
    elif 169 <= working_distance < 237:
        focus_value = 180
    elif 131.5 <= working_distance < 169:
        focus_value = 168
    elif 101.5 <= working_distance < 131.5:
        focus_value = 156
    elif 86.5 <= working_distance < 101.5:
        focus_value = 144
    elif 72 <= working_distance < 86.5:
        focus_value = 128
    elif 42.5 <= working_distance < 72:
        focus_value = 112
    else:
        print("Too close to subject. Focus value not found. Default value: 204")
        focus_value = 204

    # Set the correct focus value
    focus_UINT = ueye.UINT(focus_value)
    ueye.is_Focus(cam.hCam, ueye.FOC_CMD_SET_MANUAL_FOCUS, focus_UINT, ueye.sizeof(focus_UINT))
    time.sleep(0.3)
Exemplo n.º 3
0
    def set_parameters(self, disable_exposure=True):
        # Change image format
        # formatID = ueye.UINT(5)  # Change image format to 2048x1536
        formatID = ueye.UINT(8)  # Change image format to 1280x960
        nRet = ueye.is_ImageFormat(self.hCam, ueye.IMGFRMT_CMD_SET_FORMAT,
                                   formatID, ueye.sizeof(formatID))

        if disable_exposure:
            # Disable auto exposure
            dblEnable = ueye.DOUBLE(0)
            dblDummy = ueye.DOUBLE(0)
            ueye.is_SetAutoParameter(
                self.hCam, ueye.IS_SET_ENABLE_AUTO_SENSOR_GAIN_SHUTTER,
                dblEnable, dblDummy)

            # Set new exposure value from .ini-file
            config = configparser.ConfigParser()
            config.read('image_tools/cam_adjustments.ini')
            exposure = float(config['EXPOSURE']['exposure'])
            newExposure = ueye.DOUBLE(exposure)
            ret = ueye.is_Exposure(self.hCam,
                                   ueye.IS_EXPOSURE_CMD_SET_EXPOSURE,
                                   newExposure, ueye.sizeof(newExposure))

        # Disable autofocus
        ueye.is_Focus(self.hCam, ueye.FOC_CMD_SET_DISABLE_AUTOFOCUS, None, 0)
Exemplo n.º 4
0
    def initialize_camera_settings(self):
        '''Sets pixel_clock, fps, exposure, autofocus, autogain based on self.config.'''
        # get max pixel clock
        pixel_clock_range = (ueye.c_uint * 3)()
        ret = ueye.is_PixelClock(self.input, ueye.IS_PIXELCLOCK_CMD_GET_RANGE, pixel_clock_range, 3 * ueye.sizeof(ueye.UINT()))
        log.info(f'pixel_clock max: {pixel_clock_range[0]}, pixel_clock min: {pixel_clock_range[1]}, ret val: {ret}')
        # set max pixel clock
        pixel_clock = ueye.c_int(pixel_clock_range[1])
        ret = ueye.is_PixelClock(self.input, ueye.IS_PIXELCLOCK_CMD_SET, pixel_clock, ueye.sizeof(pixel_clock))
        self.config['pixel_clock'] = pixel_clock.value
        log.info(f'Actual pixel clock: {pixel_clock}, ret val: {ret}')

        # max out frame rate
        target_frame_rate = ueye.double(self.config.get('fps'))
        actual_frame_rate = ueye.double(0.0)
        ret = ueye.is_SetFrameRate(self.input, target_frame_rate, actual_frame_rate)
        self.config['fps'] = actual_frame_rate.value
        log.info(f'Attempted to set frame rate to {target_frame_rate}, ret value: {ret}, actual frame rate: {actual_frame_rate}')

        # max out exposure
        target_exposure = ueye.double(self.config.get('exposure'))
        actual_exposure = ueye.double(0.0)
        ret = ueye.is_Exposure(self.input, ueye.IS_EXPOSURE_CMD_SET_EXPOSURE, target_exposure, ueye.sizeof(target_exposure))
        get_ret = ueye.is_Exposure(self.input, ueye.IS_EXPOSURE_CMD_GET_EXPOSURE, actual_exposure, ueye.sizeof(actual_exposure))
        self.config['exposure'] = actual_exposure.value
        log.info(f'Attempted to set exposure to {target_exposure}, ret value: {ret}, actual frame rate: {actual_exposure}')

        # set autofocus limits
        if self.config.get('focus_min') is not None and self.config.get('focus_max') is not None:
            limit = ueye.AUTOFOCUS_LIMIT()
            limit.sMin = ueye.c_int(self.config['focus_min'])
            limit.sMax = ueye.c_int(self.config['focus_max'])
            ret = ueye.is_Focus(self.input, ueye.FOC_CMD_SET_AUTOFOCUS_LIMIT, limit, ueye.sizeof(limit));
            if ret == ueye.IS_SUCCESS:
                log.info(f'Successfully set focus min: {self.config["focus_min"]}, focus max: {self.config["focus_max"]}')
            else:
                log.error('Failed to set focus min/max.')

        # enable autofocus
        if self.config.get('autofocus'):
            ret = ueye.is_Focus(self.input, ueye.FOC_CMD_SET_ENABLE_AUTOFOCUS, None, 0)
            if ret == ueye.IS_SUCCESS:
                log.info(f'Successfully set autofocus to {self.config.get("autofocus")}.')
            else:
                log.error('Failed to set autofocus.')

        # enable autogain
        if self.config.get('autogain'):
            ret = ueye.is_SetAutoParameter(self.input, ueye.IS_SET_ENABLE_AUTO_GAIN, ueye.double(1), ueye.double(0))
            if ret == ueye.IS_SUCCESS:
                log.info(f'Successfully set autogain to {self.config.get("autogain")}.')
            else:
                log.error('Failed to set autogain.')
Exemplo n.º 5
0
# Change the format to 1280x960
formatID = ueye.UINT(8)
nRet = ueye.is_ImageFormat(cam.handle(), ueye.IMGFRMT_CMD_SET_FORMAT, formatID, ueye.sizeof(formatID))

cam.alloc()  # Allocate image memory

# Disable auto exposure
dblEnable = ueye.DOUBLE(0)
dblDummy = ueye.DOUBLE(0)
ueye.is_SetAutoParameter(cam.handle(), ueye.IS_SET_ENABLE_AUTO_SENSOR_GAIN_SHUTTER, dblEnable, dblDummy)

newExposure = ueye.DOUBLE(30)
ret = ueye.is_Exposure(cam.handle(), ueye.IS_EXPOSURE_CMD_SET_EXPOSURE, newExposure, ueye.sizeof(newExposure))

ueye.is_Focus(cam.handle(), ueye.FOC_CMD_SET_DISABLE_AUTOFOCUS, None, 0)  # Disable autofocus

focus_overview = ueye.INT(205)  # Focus value for overview image (taken from 570mm above table)
focus_closeup = ueye.INT(144)  # Focus value for closeup image (taken from 190mm above table)


"""
exp_min = ueye.DOUBLE()
ret = ueye.is_Exposure(cam.handle(), ueye.IS_EXPOSURE_CMD_GET_EXPOSURE_RANGE_MIN, exp_min, ueye.sizeof(exp_min))
exp_max = ueye.DOUBLE()
ret = ueye.is_Exposure(cam.handle(), ueye.IS_EXPOSURE_CMD_GET_EXPOSURE_RANGE_MAX, exp_max, ueye.sizeof(exp_max))
print(exp_min, exp_max)
exp_def = ueye.DOUBLE()
ret = ueye.is_Exposure(cam.handle(), ueye.IS_EXPOSURE_CMD_GET_EXPOSURE_DEFAULT, exp_def, ueye.sizeof(exp_def))
print(exp_def)