def run(image, mask=None, smoothing=False, show=False, show_now=True): if mask is None: mask = np.ones_like(image) im_orig = image.copy() else: image, mask = tools.crop_to_bbox(image, mask) im_orig = image.copy() mean_v = int(image[np.nonzero(mask)].mean()) image = np.where(mask, image, mean_v) mask = mask.astype(np.uint8) if smoothing: image = tools.smoothing(image) # defaultne hleda svetle oblasti image = np.invert(image) rgb_image = cv2.cvtColor(image, cv2.COLOR_BAYER_GR2RGB).astype(np.float32) # print 'Calculate saliency map for test image:' orgb_image = SightSpotUtil.eval_orgb_image(rgb_image) # start = time.clock() saliency_map = SightSpotUtil.eval_saliency_map(orgb_image, 1.0, 40.0, 'auto') # print 'Saliency map extracted in', time.clock() - start, 'sec.' # start = time.clock() # heatmap_image = SightSpotUtil.eval_heatmap(saliency_map) # heatmap_image = np.asarray(heatmap_image) # heatmap_image.setflags(write=True) # print 'Heatmap extracted in', time.clock() - start, 'sec.' saliency_map *= mask # heatmap_image *= np.dstack((mask, mask, mask)) im_orig *= mask saliency_map = skiexp.rescale_intensity(saliency_map, out_range=(0, 1)) if show: if smoothing: plt.subplot(131), plt.imshow(im_orig, 'gray', interpolation='nearest'), plt.title('input') plt.subplot(132), plt.imshow(image, 'gray', interpolation='nearest'), plt.title('smoothed') plt.subplot(133), plt.imshow(saliency_map, 'gray', interpolation='nearest'), plt.title('saliency') else: plt.subplot(121), plt.imshow(im_orig, 'gray', interpolation='nearest'), plt.title('input') plt.subplot(122), plt.imshow(saliency_map, 'gray', interpolation='nearest'), plt.title('saliency') if show_now: plt.show() return im_orig, image, saliency_map
def _get_saliency_map(self, type): orgb_image = self._get_orgb_image() if type == 'raw': if self._saliency_map is None: small_sigma = self._small_sigma large_sigma = self._large_sigma self._saliency_map = SightSpotUtil.eval_saliency_map(orgb_image, small_sigma, large_sigma, 'auto') return self._saliency_map if type == 'precise': if self._fusion_map is None: saliency_map = self._get_saliency_map(type='raw') cell_size = self._cell_size alpha = self._SLIC_ALPHA iterations = self._ITERATION_NUMBER segmentation_map = SightSpotUtil.eval_slic_map(orgb_image, cell_size, alpha, iterations) self._fusion_map = SightSpotUtil.combine_saliency_and_segmentation(saliency_map, segmentation_map) return self._fusion_map raise Exception('Unknown argument value type = "' + str(type) + '"')
def _get_saliency_map(self, type): orgb_image = self._get_orgb_image() if type == 'raw': if self._saliency_map is None: small_sigma = self._small_sigma large_sigma = self._large_sigma self._saliency_map = SightSpotUtil.eval_saliency_map( orgb_image, small_sigma, large_sigma, 'auto') return self._saliency_map if type == 'precise': if self._fusion_map is None: saliency_map = self._get_saliency_map(type='raw') cell_size = self._cell_size alpha = self._SLIC_ALPHA iterations = self._ITERATION_NUMBER segmentation_map = SightSpotUtil.eval_slic_map( orgb_image, cell_size, alpha, iterations) self._fusion_map = SightSpotUtil.combine_saliency_and_segmentation( saliency_map, segmentation_map) return self._fusion_map raise Exception('Unknown argument value type = "' + str(type) + '"')
def _get_saliency_map(self, type): orgb_image = self._get_orgb_image() if type == 'raw': if self._saliency_map is None: small_sigma = self._small_sigma large_sigma = self._large_sigma self._saliency_map = SightSpotUtil.eval_saliency_map(orgb_image, small_sigma, large_sigma, 'auto') return self._saliency_map if type == 'precise': if self._fusion_map is None: saliency_map = self._get_saliency_map(type='raw') cell_size = self._cell_size alpha = self._SLIC_ALPHA iterations = self._ITERATION_NUMBER self._segmentation_map = SightSpotUtil.eval_slic_map(orgb_image, cell_size, alpha, iterations) self._fusion_map = SightSpotUtil.combine_saliency_and_segmentation(saliency_map, self._segmentation_map) # _visualize_clusters(self._segmentation_map).show('Clusters in random colors') # _visualize_contours(self._rgb_image, self._segmentation_map, (255, 255, 0)).show('Cluster contours') # _visualize_averaging(self._rgb_image, self._segmentation_map).show('Averaging') return self._fusion_map raise Exception('Unknown argument value type = "' + str(type) + '"')
avg = numpy.sum(rgb_image[cluster_idx], axis=0) / cluster_size palette_items.append(tuple(avg)) palette = numpy.array(palette_items, dtype='uint8') visualization = palette[segmentation_map + 1] return Image.fromarray(visualization) if __name__ == '__main__': print 'Segmentation of test image:' image = Image.open('flower.jpg') rgb_image = numpy.asarray(image, dtype='float32') orgb_image = SightSpotUtil.eval_orgb_image(rgb_image) start = time.clock() segmentation_map = SightSpotUtil.eval_slic_map(orgb_image, 32.0, 0.25, 4) print 'Segmentation map extracted in', time.clock() - start, 'sec.' print 'Segment number:', numpy.max(segmentation_map) + 1 _visualize_clusters(segmentation_map).show('Clusters in random colors') _visualize_contours(rgb_image, segmentation_map, (255, 255, 0)).show('Cluster contours') _visualize_averaging(rgb_image, segmentation_map).show('Averaging') print 'Use segmentation to improve clustering...' saliency_map = SightSpotUtil.eval_saliency_map(orgb_image, 3.0, 60.0, 'auto') precise_saliency_map = SightSpotUtil.combine_saliency_and_segmentation( saliency_map, segmentation_map) precise_saliency_image = Image.fromarray(precise_saliency_map * 255) precise_saliency_image.show('Pixel-precise saliency image')
palette_items.append((0, 0, 0)) else: avg = numpy.sum(rgb_image[cluster_idx], axis=0) / cluster_size palette_items.append(tuple(avg)) palette = numpy.array(palette_items, dtype='uint8') visualization = palette[segmentation_map + 1] return Image.fromarray(visualization) if __name__ == '__main__': print 'Segmentation of test image:' image = Image.open('flower.jpg') rgb_image = numpy.asarray(image, dtype='float32') orgb_image = SightSpotUtil.eval_orgb_image(rgb_image) start = time.clock() segmentation_map = SightSpotUtil.eval_slic_map(orgb_image, 32.0, 0.25, 4) print 'Segmentation map extracted in', time.clock() - start, 'sec.' print 'Segment number:', numpy.max(segmentation_map) + 1 _visualize_clusters(segmentation_map).show('Clusters in random colors') _visualize_contours(rgb_image, segmentation_map, (255, 255, 0)).show('Cluster contours') _visualize_averaging(rgb_image, segmentation_map).show('Averaging') print 'Use segmentation to improve clustering...' saliency_map = SightSpotUtil.eval_saliency_map(orgb_image, 3.0, 60.0, 'auto') precise_saliency_map = SightSpotUtil.combine_saliency_and_segmentation(saliency_map, segmentation_map) precise_saliency_image = Image.fromarray(precise_saliency_map * 255) precise_saliency_image.show('Pixel-precise saliency image')