def check_status_of_stitching_button(self):
        """enable the button if all the target files have been selected at least once"""
        o_utilities = Utilities(parent=self)
        o_utilities.reset_all_status()

        nbr_row = self.ui.tableWidget.rowCount()
        list_target_file = set()
        for _row in np.arange(nbr_row):
            _reference_file = o_utilities.get_reference_file_selected_for_this_row(_row)
            _target_file = o_utilities.get_target_file_selected_for_this_row(_row)

            if _reference_file == _target_file:
                o_utilities.set_status_of_this_row_to_message(row=_row,
                                                              message='Reference and target MUST be different files!')
                continue

            if _target_file in list_target_file:
                o_utilities.set_status_of_this_row_to_message(row=_row,
                                                              message="Already used!")
            list_target_file.add(_target_file)

        if len(list_target_file) == len(self.list_target['files'])-1:
            enabled_button = True
            statusbar_message = ""
        else:
            enabled_button = False
            statusbar_message = "Make sure all files are used as reference"

        self.ui.statusbar.showMessage(statusbar_message)
        self.ui.run_stitching_button.setEnabled(enabled_button)
    def display_roi(self, data_type='reference'):
        o_utilities = Utilities(parent=self)
        _row = o_utilities.get_row_selected()
        file_dict = self.master_dict[_row]

        _roi_key = "{}_roi".format(data_type)
        x0 = file_dict[_roi_key]['x0']
        y0 = file_dict[_roi_key]['y0']
        width = file_dict[_roi_key]['width']
        height = file_dict[_roi_key]['height']

        ui = self.pyqtgraph_image_view[data_type]
        color = QtGui.QColor(62, 13, 244)
        _pen = QtGui.QPen()
        _pen.setColor(color)
        _pen.setWidth(0.02)
        _roi_id = pg.ROI([x0, y0], [width, height], pen=_pen, scaleSnap=True)
        if data_type == 'reference':
            _roi_id.addScaleHandle([1, 1], [0, 0])
            _roi_id.addScaleHandle([0, 0], [1, 1])
            method = self.reference_roi_changed
        else:
            method = self.target_roi_changed

        ui.addItem(_roi_id)
        _roi_id.sigRegionChanged.connect(method)
        self.live_rois_id[data_type] = _roi_id
    def table_widget_selection_changed(self):
        o_utilities = Utilities(parent=self)
        row_selected = o_utilities.get_reference_selected(key='index')

        # +1 because the target file starts at the second file
        target_file_index_selected = o_utilities.get_target_index_selected_from_row(row=row_selected)
        reference_file_index_selected = o_utilities.get_reference_index_selected_from_row(row=row_selected)

        reference_data = self.list_reference['data'][reference_file_index_selected]
        target_data = self.list_target['data'][target_file_index_selected]

        self.display_reference_data(data=reference_data)
        self.display_target_data(data=target_data)
Ejemplo n.º 4
0
    def run_fft(self):
        master_dict = self.parent.master_dict
        o_utilities = Utilities(parent=self.parent)

        for _row in master_dict.keys():
            _data_reference = o_utilities.get_image_for_this_row(
                data_type='reference', row=_row)
            _data_target = o_utilities.get_image_for_this_row(
                data_type='target', row=_row)

            reference_roi = master_dict[_row]['reference_roi']
            [ref_x0, ref_y0, ref_width, ref_height
             ] = Stitching.retrieve_roi_parameters(roi_dict=reference_roi)
            target_roi = master_dict[_row]['target_roi']
            [target_x0, target_y0, _,
             _] = Stitching.retrieve_roi_parameters(roi_dict=target_roi)

            _data_reference_roi = _data_reference[ref_y0:ref_y0 + ref_height,
                                                  ref_x0:ref_x0 + ref_width]
            _data_target_roi = _data_target[target_y0:target_y0 + ref_height,
                                            target_x0:target_x0 + ref_width]

            f_reference = np.fft.fft2(_data_reference_roi)
            f_target = np.fft.fft2(_data_target_roi)

            f_ref_target = f_target * np.conjugate(f_reference)
            co = np.abs(np.fft.ifft2(f_ref_target))

            pos = np.where(co == np.amax(co))
            optimum_x0 = pos[1][0]
            optimum_y0 = pos[0][0]

            displacement = {
                'x': target_x0 - ref_x0 + optimum_x0,
                'y': target_y0 - ref_y0 + optimum_y0
            }

            master_dict[_row]['displacement']['x'] = displacement['x']
            master_dict[_row]['displacement']['y'] = displacement['y']
    def save_roi_changed(self, data_type='reference'):
        roi_id = self.live_rois_id[data_type]
        o_utilities = Utilities(parent=self)
        view = o_utilities.get_view(data_type=data_type)
        image = o_utilities.get_image(data_type=data_type)
        row_selected = o_utilities.get_row_selected()

        region = roi_id.getArraySlice(np.transpose(image),
                                      view.imageItem)
        x0 = region[0][0].start
        x1 = region[0][0].stop
        y0 = region[0][1].start
        y1 = region[0][1].stop

        x0 = np.min([x0, x1])
        y0 = np.min([y0, y1])

        width = np.max([x0, x1]) - x0
        height = np.max([y0, y1]) - y0

        o_utilities.set_roi_to_master_dict(row=row_selected,
                                           data_type=data_type,
                                           x0=x0,
                                           y0=y0,
                                           width=width,
                                           height=height)

        # we need to make sure the target roi has the proper size
        if data_type == 'reference':
            o_utilities.set_roi_to_master_dict(row=row_selected,
                                               data_type='target',
                                               width=self.target_box_size_coefficient['x']*width,
                                               height=self.target_box_size_coefficient['y']*height)
            self.display_target_data(data=o_utilities.get_image(data_type='target'))

        self.check_status_of_stitching_button()
Ejemplo n.º 6
0
    def run(self):
        master_dict = self.parent.master_dict
        list_target_file = self.parent.list_target

        if DEBUG_JSON: roi_to_export = {}
        for _row in master_dict.keys():

            _data_reference = self.parent.list_reference['data'][_row]
            _target_file_index = master_dict[_row][
                'target_combobox_file_index']

            _target_file = list_target_file['files'][_target_file_index]
            _data_target = list_target_file['data'][_target_file_index]

            reference_roi = master_dict[_row]['reference_roi']
            [ref_x0, ref_y0, ref_width, ref_height
             ] = Stitching.retrieve_roi_parameters(roi_dict=reference_roi)

            target_roi = master_dict[_row]['target_roi']
            [starting_target_x0, starting_target_y0, target_width, target_height] = \
             Stitching.retrieve_roi_parameters(roi_dict=target_roi)

            _data_reference_of_roi = _data_reference[ref_y0:ref_y0 +
                                                     ref_height,
                                                     ref_x0:ref_x0 + ref_width]

            # where to start from
            moving_target_x0 = starting_target_x0
            moving_target_y0 = starting_target_y0

            moving_target_width = ref_width
            moving_target_height = ref_height

            # where to end
            final_target_x0 = starting_target_x0 + target_width - ref_width
            final_target_y0 = starting_target_y0 + target_height - ref_height

            moving_target_x1 = moving_target_x0 + moving_target_width
            moving_target_y1 = moving_target_y0 + moving_target_height

            print("Reference:")
            print("x0:{}, y0:{}, width:{}, height:{}".format(
                ref_x0, ref_y0, ref_width, ref_height))
            print("target:")
            print("x0:{}, y0:{}, width:{}, height:{}".format(
                starting_target_x0, starting_target_y0, target_width,
                target_height))

            if DEBUG_JSON:
                o_utilities = Utilities(parent=self.parent)
                _reference_file_index = o_utilities.get_reference_index_selected_from_row(
                    row=_row)

                roi_to_export[str(_row)] = {
                    'reference': {
                        'x0': str(ref_x0),
                        'y0': str(ref_y0),
                        'width': str(np.int(ref_width)),
                        'height': str(np.int(ref_height)),
                        'file_index': str(_target_file_index)
                    },
                    'target': {
                        'x0': str(starting_target_x0),
                        'y0': str(starting_target_y0),
                        'width': str(np.int(target_width)),
                        'height': str(np.int(target_height)),
                        'file_index': str(_reference_file_index)
                    }
                }

            counts_and_x0_position_dict = defaultdict(list)
            counts_and_y0_position_dict = defaultdict(list)

            counts_3d = np.zeros((final_target_y0 - moving_target_y0 + 1,
                                  final_target_x0 - moving_target_x0 + 1))

            x = 0
            y = 0
            while moving_target_y0 <= final_target_y0:

                _data_target_of_roi = _data_target[
                    moving_target_y0:moving_target_y0 + ref_height,
                    moving_target_x0:moving_target_x0 + ref_width]

                _diff_array = np.abs(_data_target_of_roi -
                                     _data_reference_of_roi)
                _sum_diff_array = np.sum(_diff_array)
                counts_and_x0_position_dict[_sum_diff_array].append(
                    moving_target_x0)
                counts_and_y0_position_dict[_sum_diff_array].append(
                    moving_target_y0)

                counts_3d[y, x] = _sum_diff_array

                moving_target_x0 += 1
                x += 1
                if moving_target_x0 > final_target_x0:
                    moving_target_x0 = starting_target_x0
                    x = 0

                    moving_target_y0 += 1
                    y += 1

            list_of_counts_x0 = np.array(
                list(counts_and_x0_position_dict.keys()))
            list_of_counts_y0 = np.array(
                list(counts_and_y0_position_dict.keys()))
            optimum_counts_for_x0 = list_of_counts_x0.min()
            optimum_counts_for_y0 = list_of_counts_y0.min()

            # self.parent.debug_list_of_counts_x0 = list_of_counts_x0
            # self.parent.debug_list_of_counts_y0 = list_of_counts_y0
            self.parent.debug_counts_3d = counts_3d

            optimum_x0 = counts_and_x0_position_dict[optimum_counts_for_x0][0]
            optimum_y0 = counts_and_y0_position_dict[optimum_counts_for_y0][0]

            print("optimum x0:{} and optimum y0:{}".format(
                optimum_x0, optimum_y0))

            self.parent.debug_big_array_roi_ref = _data_reference_of_roi
            self.parent.debug_big_array_roi_target = _data_target[
                optimum_y0:optimum_y0 + ref_height,
                optimum_x0:optimum_x0 + ref_width]

            if DEBUG_JSON:
                import json
                with open('/Users/j35/Desktop/roi.txt', 'w') as outfile:
                    json.dump(roi_to_export, outfile)
 def down_down_button_released(self):
     Utilities.button_released(ui=self.ui.down_down_button,
                               name='down_down')
 def down_button_pressed(self):
     Utilities.button_pressed(ui=self.ui.down_button,
                              name='down')
 def right_right_button_released(self):
     Utilities.button_released(ui=self.ui.right_right_button,
                               name='right_right')
 def left_left_button_released(self):
     Utilities.button_released(ui=self.ui.left_left_button,
                               name='left_left')
 def right_button_pressed(self):
     Utilities.button_pressed(ui=self.ui.right_button,
                              name='right')
 def left_button_pressed(self):
     Utilities.button_pressed(ui=self.ui.left_button,
                              name='left')
 def up_button_released(self):
     Utilities.button_released(ui=self.ui.up_button,
                               name='up')
 def up_up_button_pressed(self):
     Utilities.button_pressed(ui=self.ui.up_up_button,
                              name='up_up')