Ejemplo n.º 1
0
    def _impurities_init(self, filename):
        # Load sample quantized in two colors by maximum coverage algorithm and
        # determine (and store) where the impurities are located
        _, bitonal, _ = io.load_and_quantize(filename, num_colors=2,
                                             method=io.MAX_COVERAGE)

        # Assuming count of the impurity pixels ("foreground") is less than
        # count of the clean paper pixels ("background")
        num_zeros, num_ones = (bitonal == 0).sum(), (bitonal == 1).sum()
        impurities_index = 0 if num_zeros < num_ones else 1

        # Create impurity map
        impurities_map = np.zeros_like(self._sample_indexed, dtype=np.bool)
        impurities_map[bitonal == impurities_index] = True

        # Dilate a little bit to better encompass the impurities
        impurities_map = skimage.morphology.binary_dilation(impurities_map)
        self._impurities_map_ravel = impurities_map.ravel()

        # Store foreground and background indices
        self._fg_indices = np.where(impurities_map.ravel())[0]
        self._bg_indices = np.where(~impurities_map.ravel())[0]

        # Save impurities map
        filename = TextureSynth._add_to_filename(filename, 'impurities')
        io.save_image(filename, 255 * impurities_map)

        logging.info('Map of impurities computed.')
Ejemplo n.º 2
0
 def _create_ind2rgb(self, filename):
     h, w = self._sample.shape[0:2]
     r, g = np.meshgrid(np.linspace(0, 1, w), np.linspace(0, 1, h))
     b = np.zeros_like(r)
     rgb_face = np.dstack((r, g, b))
     self._sample_ind2rgb = rgb_face.reshape(h * w, 3)
     filename = Synthesizer._add_to_filename(filename, 'rgb_map')
     io.save_image(filename, rgb_face)
Ejemplo n.º 3
0
    def _load_sample(self, filename):
        # Load and quantize sample image
        num_colors = Synthesizer._quantized_colors
        sample, indexed, labels = io.load_and_quantize(filename, num_colors)
        self._sample = sample
        self._sample_indexed = indexed
        self._sample_indexed_ravel = indexed.ravel()
        self._index_labels = labels
        logging.info('Sample image loaded.')

        # Save quantized sample image for visual comparison
        quantized_sample = np.take(labels, indexed, axis=0)
        colors = 'indexed_{0}_colors'.format(Synthesizer._quantized_colors)
        filename_quantized = Synthesizer._add_to_filename(filename, colors)
        io.save_image(filename_quantized, quantized_sample)

        # Color cube part
        self._create_ind2rgb(filename)
        logging.info('Sample indices to RGB image created.')
Ejemplo n.º 4
0
    def _prepare_equivalence_map(self, filename):
        logging.debug('Loading image for the equivalence map.')
        _, equiv, l = io.load_and_quantize(filename,
                                           num_colors=TextureSynth._sim_colors,
                                           method=io.MAX_COVERAGE)

        # Save equivalence image for visual comparison
        equiv_image = np.take(l, equiv, axis=0)
        equiv_text = 'equivalence_{0}_colors'.format(TextureSynth._sim_colors)
        filename_equiv = Synthesizer._add_to_filename(filename, equiv_text)
        io.save_image(filename_equiv, equiv_image)

        equiv = equiv.ravel()
        equiv_map = []

        logging.info('Computing equivalence map.')
        for i in range(TextureSynth._sim_colors):
            indices = np.where(equiv == i)[0]
            equiv_map.append(indices)

        self._equiv_map = equiv_map
        self._sample_equiv = equiv

        logging.info('Equivalence map computed.')
Ejemplo n.º 5
0
 def _save_results(self, out_origins, filename):
     texture, indices = self._transform_origins(out_origins)
     io.save_image(filename, texture)
     indices_filename = Synthesizer._add_to_filename(filename, 'origins')
     io.save_image(indices_filename, indices)