def get_heatmap(self, type='raw'): """ Returns heatmap calculated from saliency map of image passed to SightSpotDetector constructor. Parameters ---------- type : 'raw' or 'precise' Specifies whenever segmentation will be used to improve saliency map (slower). Returns ------- out : PIL.Image Heatmap. """ if type == 'raw': if self._raw_heatmap == None: saliency_map = self._get_saliency_map(type) self._raw_heatmap = SightSpotUtil.eval_heatmap(saliency_map) return self._raw_heatmap.copy() if type == 'precise': if self._precise_heatmap == None: fusion_map = self._get_saliency_map(type) self._precise_heatmap = SightSpotUtil.eval_heatmap(fusion_map) return self._precise_heatmap.copy() raise Exception('Unknown argument value type = "' + str(type) + '"')
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) + '"')
def threshold(self, source, value='auto'): """ Returns black-and-white image - thresholded version of saliency map. Parameters ---------- source : 'raw' or 'precise' Which saliency map to use for thresholding. type : float or 'auto' Specifies thresholding value. Returns ------- out : PIL.Image Binary image. """ saliency_map = self._get_saliency_map(source) return Image.fromarray(255.0 * SightSpotUtil.threshold(saliency_map, value)).convert('RGB')
def get_foreground(self, source, value='auto'): """ Removes background pixels from input image and returns result. Parameters ---------- source : 'raw' or 'precise' Which saliency map to use for thresholding. type : float or 'auto' Specifies thresholding value. Returns ------- out : PIL.Image Image without background pixels. """ saliency_map = self._get_saliency_map(source) return Image.fromarray(SightSpotUtil.remove_background(self._rgb_image, saliency_map, value))
def get_foreground(self, source, value='auto'): """ Removes background pixels from input image and returns result. Parameters ---------- source : 'raw' or 'precise' Which saliency map to use for thresholding. type : float or 'auto' Specifies thresholding value. Returns ------- out : PIL.Image Image without background pixels. """ saliency_map = self._get_saliency_map(source) return Image.fromarray( SightSpotUtil.remove_background(self._rgb_image, saliency_map, value))
def threshold(self, source, value='auto'): """ Returns black-and-white image - thresholded version of saliency map. Parameters ---------- source : 'raw' or 'precise' Which saliency map to use for thresholding. type : float or 'auto' Specifies thresholding value. Returns ------- out : PIL.Image Binary image. """ saliency_map = self._get_saliency_map(source) return Image.fromarray( 255.0 * SightSpotUtil.threshold(saliency_map, value)).convert('RGB')
def cut_objects(self, source, value='auto'): """ Extract connected components from thresholded saliency map and cuts images. Parameters ---------- source : 'raw' or 'precise' Which saliency map to use for thresholding. type : float or 'auto' Specifies thresholding value. Returns ------- out : list of PIL.Images Detected objects. """ saliency_map = self._get_saliency_map(source) result = SightSpotUtil.detect_objects(self._rgb_image, saliency_map, value) windows = [] for item in result: windows.append(Image.fromarray(item)) return windows
cluster_idx = (segmentation_map == i - 1) cluster_size = numpy.sum(cluster_idx) if cluster_size == 0: 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')
print "RGB (0, 0, 1) is oRGB (%f, %f, %f) for BLUE " % _rgb2orgb( 0, 0, 1) # Blue. print "RGB (1, 1, 0) is oRGB (%f, %f, %f) for YELLOW " % _rgb2orgb( 1, 1, 0) # Yellow. print "RGB (0, 1, 1) is oRGB (%f, %f, %f) for SKY-BLUE" % _rgb2orgb( 0, 1, 1) # Sky-blue. print "RGB (1, 0, 1) is oRGB (%f, %f, %f) for MAGENTA " % _rgb2orgb( 1, 0, 1) # Magenta. print "RGB (1, 1, 1) is oRGB (%f, %f, %f) for WHITE " % _rgb2orgb( 1, 1, 1) # White. print 'Show channels of test image:' image = Image.open('flower.jpg') rgb_image = numpy.asarray(image, dtype='float32') start = time.clock() orgb_image = SightSpotUtil.eval_orgb_image(rgb_image) print 'ORGB image extracted in', time.clock() - start, 'sec.' print 'Colorizing...' lu_channel = orgb_image[:, :, 0] rg_channel = orgb_image[:, :, 1] yb_channel = orgb_image[:, :, 2] lu_image = Image.fromarray(lu_channel * 255 + 0.5) rg_image = _colorize_channel(rg_channel, (255, 0, 0), (0, 255, 0)) yb_image = _colorize_channel(yb_channel, (255, 255, 0), (0, 0, 255)) image.show('Source image') lu_image.show('Luminance channel') rg_image.show('Red-green channel') yb_image.show('Yellow-blue channel')
def _get_orgb_image(self): if self._orgb_image is None: self._orgb_image = SightSpotUtil.eval_orgb_image(self._rgb_image) return self._orgb_image
cluster_size = numpy.sum(cluster_idx) if cluster_size == 0: 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(
__author__ = 'Igor Ryabtsov aka Tinnulion' __copyright__ = 'Copyright (c) 2014' __license__ = 'Apache 2.0' __version__ = '1.0' import time import numpy from PIL import Image import SightSpotUtil if __name__ == '__main__': print 'Calculate saliency map for 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() saliency_map = SightSpotUtil.eval_saliency_map(orgb_image, 3.0, 60.0, 'auto') print 'Saliency map extracted in', time.clock() - start, 'sec.' start = time.clock() heatmap_image = SightSpotUtil.eval_heatmap(saliency_map) print 'Heatmap extracted in', time.clock() - start, 'sec.' saliency_image = Image.fromarray(saliency_map * 255) image.show('Source image') saliency_image.show('Saliency image') heatmap_image.show('Heatmap')
def _rgb2orgb(r, g, b): rgb_image = numpy.array([[[255.0 * r, 255.0 * g, 255.0 * b]]], dtype='float32') orgb = SightSpotUtil.eval_orgb_image(rgb_image)[0, 0] return tuple(orgb)
print 'Test some basic oRGB color conversions:' print "RGB (0, 0, 0) is oRGB (%f, %f, %f) for BLACK " % _rgb2orgb(0, 0, 0) # Black. print "RGB (1, 0, 0) is oRGB (%f, %f, %f) for RED " % _rgb2orgb(1, 0, 0) # Red. print "RGB (0, 1, 0) is oRGB (%f, %f, %f) for GREEN " % _rgb2orgb(0, 1, 0) # Green. print "RGB (0, 0, 1) is oRGB (%f, %f, %f) for BLUE " % _rgb2orgb(0, 0, 1) # Blue. print "RGB (1, 1, 0) is oRGB (%f, %f, %f) for YELLOW " % _rgb2orgb(1, 1, 0) # Yellow. print "RGB (0, 1, 1) is oRGB (%f, %f, %f) for SKY-BLUE" % _rgb2orgb(0, 1, 1) # Sky-blue. print "RGB (1, 0, 1) is oRGB (%f, %f, %f) for MAGENTA " % _rgb2orgb(1, 0, 1) # Magenta. print "RGB (1, 1, 1) is oRGB (%f, %f, %f) for WHITE " % _rgb2orgb(1, 1, 1) # White. print 'Show channels of test image:' image = Image.open('flower.jpg') rgb_image = numpy.asarray(image, dtype='float32') start = time.clock() orgb_image = SightSpotUtil.eval_orgb_image(rgb_image) print 'ORGB image extracted in', time.clock() - start, 'sec.' print 'Colorizing...' lu_channel = orgb_image[:,:,0] rg_channel = orgb_image[:,:,1] yb_channel = orgb_image[:,:,2] lu_image = Image.fromarray(lu_channel * 255 + 0.5) rg_image = _colorize_channel(rg_channel, (255, 0, 0), (0, 255, 0)) yb_image = _colorize_channel(yb_channel, (255, 255, 0), (0, 0, 255)) image.show('Source image') lu_image.show('Luminance channel') rg_image.show('Red-green channel') yb_image.show('Yellow-blue channel')
""" __author__ = 'Igor Ryabtsov aka Tinnulion' __copyright__ = 'Copyright (c) 2014' __license__ = 'Apache 2.0' __version__ = '1.0' import time import numpy from PIL import Image import SightSpotUtil if __name__ == '__main__': print 'Calculate saliency map for 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() saliency_map = SightSpotUtil.eval_saliency_map(orgb_image, 3.0, 60.0, 'auto') print 'Saliency map extracted in', time.clock() - start, 'sec.' start = time.clock() heatmap_image = SightSpotUtil.eval_heatmap(saliency_map) print 'Heatmap extracted in', time.clock() - start, 'sec.' saliency_image = Image.fromarray(saliency_map * 255) image.show('Source image') saliency_image.show('Saliency image') heatmap_image.show('Heatmap')