Exemplo n.º 1
0
class Controller(object):
    def __init__(self):
        dispatcher.connect(self.handle_image_load_event, signal='load_image', sender=dispatcher.Any)
        dispatcher.connect(self.handle_filter_event, signal='apply_filter', sender=dispatcher.Any)
        self.image = None
        self.im_processor = ImageProcessor()
        self.user_interface = GUI()
        self.user_interface.run()

    def handle_filter_event(self, sender, args):
        filter_name, params = args

        if not self.image is None:
            if filter_name == 'Salt & pepper':
                self.image = self.im_processor.salt_and_pepper(self.image, params[0])
            elif filter_name == 'Median':
                self.image = self.im_processor.median(self.image, int(params[0]))
            elif filter_name == 'Average':
                self.image = self.im_processor.average(self.image, int(params[0]))
            elif filter_name == 'Binarization':
                self.image = self.im_processor.binarization(self.image)
            elif filter_name == 'Color detection':
                self.image = self.im_processor.color_detection(self.image, params[0], int(params[1]))
            elif filter_name == 'Complement':
                self.image = self.im_processor.complement(self.image)
            elif filter_name == 'Diagonal lines':
                self.image = self.im_processor.diagonal(self.image, params[0])
            elif filter_name == 'Difference':
                self.image = self.im_processor.difference(self.image, cv2.imread(params[0]))
            elif filter_name == 'High pass':
                self.image = self.im_processor.high_pass(self.image, int(params[0]))
            elif filter_name == 'Horizontal':
                self.image = self.im_processor.horizontal(self.image)
            elif filter_name == 'Hough':
                if params[0] == 'Line':
                    self.image = self.im_processor.hough_lines(self.image)
                else:
                    self.image = self.im_processor.hough_circles(self.image)
            elif filter_name == 'Intersection':
                self.image = self.im_processor.intersection(self.image, cv2.imread(params[0]))
            elif filter_name == 'Prewitt':
                self.image = self.im_processor.prewitt(self.image)
            elif filter_name == 'Roberts':
                self.image = self.im_processor.roberts(self.image)
            elif filter_name == 'Seam carving':
                self.image = self.im_processor.seam_carving(self.image, int(params[0]))
            elif filter_name == 'Sobel':
                self.image = self.im_processor.sobel(self.image)
            elif filter_name == 'Union':
                self.image = self.im_processor.union(self.image, cv2.imread(params[0]))

            colormap = self.im_processor.convert_bgr_to_rgb(self.image)

            self.user_interface.append_image(self.image, colormap, filter_name)
        else:
            print('There is no image set to apply this filter')

    def handle_image_load_event(self, sender, image_path):
        self.image = cv2.imread(image_path)
        print('the image was set')