Mentions légales du service

Skip to content
Snippets Groups Projects
Forked from DEBREUVE Eric / NutriMorph
169 commits behind the upstream repository.
image_verification.py 6.32 KiB
# Copyright CNRS/Inria/UNS
# Contributor(s): Eric Debreuve (since 2019), Morgane Nadal (2020)
#
# eric.debreuve@cnrs.fr
#
# This software is governed by the CeCILL  license under French law and
# abiding by the rules of distribution of free software.  You can  use,
# modify and/ or redistribute the software under the terms of the CeCILL
# license as circulated by CEA, CNRS and INRIA at the following URL
# "http://www.cecill.info".
#
# As a counterpart to the access to the source code and  rights to copy,
# modify and redistribute granted by the license, users are provided only
# with a limited warranty  and the software's author,  the holder of the
# economic rights,  and the successive licensors  have only  limited
# liability.
#
# In this respect, the user's attention is drawn to the risks associated
# with loading,  using,  modifying and/or developing or reproducing the
# software by the user in light of its specific status of free software,
# that may mean  that it is complicated to manipulate,  and  that  also
# therefore means  that it is reserved for developers  and  experienced
# professionals having in-depth computer knowledge. Users are therefore
# encouraged to load and test the software's suitability as regards their
# requirements in conditions enabling the security of their systems and/or
# data to be ensured and,  more generally, to use and operate it in the
# same conditions as regards security.
#
# The fact that you are presently reading this means that you have had
# knowledge of the CeCILL license and that you accept its terms.

import sys as sy_
from PySide2.QtWidgets import *
import skimage.io as io_
import matplotlib as mpl_
import matplotlib.pyplot as pl_

from brick.general.type import array_t

mpl_.use('TkAgg')

channel = 'G'
data_path = '../../data/DIO_6H_6_1.70bis_2.2_3.tif'
image = io_.imread(data_path)


class image_verification(QWidget):

    def __init__(self, image: array_t, channel: str, parent=None):
        super(image_verification, self).__init__(parent)
        # Create widgets
        # --Text
        self.text0 = QLabel('The image input must not be constant.')
        self.text1 = QLabel('The image has only one color channel.')
        self.text2 = QLabel(f"The image has only 3 dimensions. However, a value for the 'channel' parameter"
                            f"is specified: {channel}. Continue with this image?")
        self.text3 = QLabel(f"The image has multiple color channels. The channel {channel} is specified"
                            f" in the parameters. Continue with the resulting image?")
        self.text4 = QLabel('The image has multiple color channels. Error in the value of the parameter channel.')
        self.text5 = QLabel(f'The image dimensions are not correct: {image.ndim}, instead of 3 or 4.')
        # --Buttons
        self.button_quit = QPushButton("Quit and modify the parameters.")
        self.button_continue = QPushButton("Continue.")
        # Add button signal
        self.button_continue.clicked.connect(self.Continue)
        self.button_quit.clicked.connect(self.Quit)

        if image is not None:
            self.ImVerif(image, channel)

    def Continue(self):
        self.close()

    def Quit(self):
        sy_.exit(0)

    def ImVerif(self, image: array_t, channel: str) -> array_t:
        # The image must not be constant
        if image.max() == image.min():
            value_error = 'The image input must not be constant.'
            print(value_error)

            layout = QVBoxLayout()
            layout.addWidget(self.text0)
            layout.addWidget(self.button_quit)
            self.setLayout(layout)

        # Verification of the dimension of the image and its coherence with the parameters (channel)
        elif image.ndim == 3:
            print('The image has only one color channel.')
            pl_.imshow(image[10, :, :])
            pl_.show(block=True)

            layout = QVBoxLayout()
            layout.addWidget(self.text1)
            layout.addWidget(self.button_continue)
            self.setLayout(layout)

            if channel is not None:
                value_error = f'The image has only 3 dimensions. However, a value for the "channel" parameter is '
                f'specified : {channel}. Give the channel the value None?'
                print(value_error)

                layout = QVBoxLayout()
                layout.addWidget(self.text2)
                layout.addWidget(self.button_continue)
                layout.addWidget(self.button_quit)
                self.setLayout(layout)

        elif image.ndim == 4:
            if channel == 'R' or channel == 'G' or channel == 'B':
                # not changed into if --channel in 'RGB'-- because if channel='RG' => True.
                value_error = f'The image has multiple color channels. The channel {channel} is specified in the ' \
                              f'parameters. '
                print(value_error)

                image = image[:, :, :, 'RGB'.find(channel)]
                pl_.imshow(image[10, :, :])
                pl_.show(block=True)

                layout = QVBoxLayout()
                layout.addWidget(self.text3)
                layout.addWidget(self.button_continue)
                layout.addWidget(self.button_quit)
                self.setLayout(layout)

                # TODO: Viewing bug when channel = 'R' or 'B'
                # The obtained image must not be constant
                if image.max() == image.min():
                    raise ValueError('The image input must not be constant.')

                return image  # TODO: How to return the image ??

            else:
                print('The image has multiple color channels. Error in the value of the parameter channel.')

                layout = QVBoxLayout()
                layout.addWidget(self.text4)
                layout.addWidget(self.button_quit)
                self.setLayout(layout)

        elif image.ndim != 4 and image.ndim != 3:
            print(f'The image dimensions are not correct: {image.ndim}, instead of 3 or 4.')

            layout = QVBoxLayout()
            layout.addWidget(self.text5)
            layout.addWidget(self.button_quit)
            self.setLayout(layout)


if __name__ == '__main__':
    # Create the Qt Application
    app = QApplication(sy_.argv)
    # Create and show the form
    verif = image_verification(image, channel)
    verif.show()

    sy_.exit(app.exec_())