Exemple #1
0
class ProgramWidget(QWidget):
    def __init__(self, parent):
        super().__init__(parent)

        self.image_widget = ImageWidget(parent)
        self.hist_widget = HistogramWidget(parent)
        self.coord_label = QLabel("", self)
        self.pixel_rgb_label = QLabel('', self)
        self.pixel_hsv_label = QLabel('', self)
        self.pixel_lab_label = QLabel('', self)

        self.hsv_checkbox = QCheckBox("Shift HSV")
        self.hsv_checkbox.toggled.connect(self.slider_update)

        h_slider_box, self.h_slider = self._get_slider_box(
            "H:", -180, 180, 60, self.slider_update
        )

        s_slider_box, self.s_slider = self._get_slider_box(
            "S:", -100, 100, 10, self.slider_update
        )

        v_slider_box, self.v_slider = self._get_slider_box(
            "V:", -100, 100, 10, self.slider_update
        )

        self._set_default()

        self.image_widget.selection_update.connect(self.selection_upd)

        hbox = QHBoxLayout()
        hbox.addWidget(self.image_widget, 20)

        vbox = QVBoxLayout()
        vbox.addWidget(self.hist_widget)
        vbox.addWidget(self.coord_label)
        vbox.addWidget(self.pixel_rgb_label)
        vbox.addWidget(self.pixel_hsv_label)
        vbox.addWidget(self.pixel_lab_label)

        vbox.addWidget(self.hsv_checkbox)
        vbox.addLayout(h_slider_box)
        vbox.addLayout(s_slider_box)
        vbox.addLayout(v_slider_box)

        self._filter_buttons(vbox)

        hbox.addLayout(vbox, 1)

        self.setLayout(hbox)

    def _filter_buttons(self, vbox):
        self.filter_rbtn = QButtonGroup()
        hbox = QHBoxLayout()

        radio1 = QRadioButton("Отключить", self)
        self.filter_rbtn.addButton(radio1, 0)
        hbox.addWidget(radio1)
        radio1.setChecked(True)

        radio2 = QRadioButton("Гаусса", self)
        self.filter_rbtn.addButton(radio2, 1)
        hbox.addWidget(radio2)

        radio3 = QRadioButton("Собеля", self)
        self.filter_rbtn.addButton(radio3, 2)
        hbox.addWidget(radio3)

        radio4 = QRadioButton("Габора", self)
        self.filter_rbtn.addButton(radio4, 3)
        hbox.addWidget(radio4)

        vbox.addLayout(hbox)

        slider_box, self.sigma_slider = self._get_slider_box("σ", 0, 10, 0.5, self._filter_change)

        vbox.addLayout(slider_box)
        self.filter_rbtn.buttonClicked.connect(self._filter_change)

    def _filter_change(self):
        filter_id = self.filter_rbtn.checkedId()
        self._update_sigma_slider(filter_id)
        param1 = self.sigma_slider.value() / 10
        self.image_widget.set_filter(filter_id, param1)

    def _update_sigma_slider(self, filter_id):
        if filter_id == 1:
            self.sigma_slider.setEnabled(True)
            self.sigma_slider.setText("σ")
            self.sigma_slider.setMinimum(0)
            self.sigma_slider.setMaximum(100)
            self.sigma_slider.setTickInterval(1)
        elif filter_id == 3:
            self.sigma_slider.setEnabled(True)
            self.sigma_slider.setText("θ")
            # self.sigma_slider.setValue(1)
            self.sigma_slider.setMinimum(0)
            self.sigma_slider.setMaximum(3600)
            self.sigma_slider.setTickInterval(50)
        else:
            self.sigma_slider.setText("-")
            self.sigma_slider.setDisabled(True)

    def _get_slider_box(self, label_name, _min, _max, _interval, callback, layout=Qt.Horizontal) -> Tuple[QBoxLayout, QSlider]:
        slider = QSlider(layout, self)
        slider.setMinimum(_min)
        slider.setMaximum(_max)
        slider.setTickPosition(QSlider.TicksBelow)
        slider.setTickInterval(_interval)

        slider.sliderReleased.connect(callback)

        box = QHBoxLayout()
        label = QLabel(label_name, self)
        box.addWidget(label)
        box.addWidget(slider)
        slider._label = label

        def setText(text):
            slider._label.setText(text)

        slider.setText = setText

        return box, slider

    def _set_default(self):
        self.coord_label.setText("Select pixel")
        self.pixel_rgb_label.setText("Select pixel")
        self.pixel_hsv_label.setText("Select pixel")
        self.pixel_lab_label.setText("Select pixel")

        self.h_slider.setValue(0)

    def slider_update(self):
        if self.hsv_checkbox.isChecked():
            self.image_widget.shift_hsv = self.h_slider.value(), self.s_slider.value(), self.v_slider.value()
        else:
            self.image_widget.shift_hsv = [0, 0, 0]

    def selection_upd(self):
        img = self.image_widget.selected(self.hist_widget.speed)
        print(img.width(), img.height())

        coord = self.image_widget.selection_img
        self.coord_label.setText("{}, {} x {}, {}".format(
            coord.left(), coord.top(),
            coord.right(), coord.bottom()
        ))

        if 1 == img.width() == img.height():
            pixel = QColor(img.pixel(0, 0))

            self.pixel_rgb_label.setText(
                "R:{}, G:{}, B:{}".format(pixel.red(), pixel.green(), pixel.blue())
            )

            self.pixel_hsv_label.setText(
                "H:{}, S:{}, V:{}".format(pixel.hue(), pixel.saturation(), pixel.value())
            )

            self.pixel_lab_label.setText(
                "L:{:.1f}, A:{:.1f}, B:{:.1f}".format(*pixel.lab())
            )
        else:
            self.pixel_rgb_label.setText("Select one pixel")
            self.pixel_hsv_label.setText("Select one pixel")
            self.pixel_lab_label.setText("Select one pixel")

        self.hist_widget.calc_image(img)

    def set_image(self, image: QImage):
        self.image_widget.set_image(image)