def __itti_saliency_map(self, image_src): self.__set_timer() image = io.imread(image_src) print "itti saliency started..." s_map = SaliencyMap(image) self.__print_timer() print "itti saliency done." return s_map.map
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) rgb_image = cv2.cvtColor(image, cv2.COLOR_BAYER_GR2BGR).astype(np.float32) saliency = SaliencyMap(rgb_image) saliency_map = saliency.map saliency_map *= 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 buildSM(self, gazepath, lblpath): ''' build saliency map from gaze labels ''' ''' # convert black pixels in lbl to white with open(lblpath, 'r+b') as f: with PIL.Image.open(f) as image: for width in xrange(image.size[0]): for length in xrange(image.size[1]): pixel = image.getpixel((width, length)) if pixel == (0, 0, 0): image.putpixel((width, length), (255, 255, 255)) ''' oi = OpencvIo() gaze = oi.imread(lblpath) sm = SaliencyMap(gaze) oi.imwrite(sm.map, gazepath) # threshold SM with open(gazepath, 'r+b') as f: with PIL.Image.open(f).convert('RGB') as image: for length in xrange(image.size[0]): for width in xrange(image.size[1]): pixel = image.getpixel((length, width)) # putpixel() is slow, learn to use paste for single pixel placement if possible if pixel[0] < 50: image.putpixel((length, width), (0, 0, 0)) elif 50 <= pixel[0] < 100: image.putpixel((length, width), (100, 100, 100)) elif 100 <= pixel[0] < 150: image.putpixel((length, width), (175, 175, 175)) else: image.putpixel((length, width), (255, 255, 255)) image.save(gazepath, image.format)
from saliency_map import SaliencyMap from utils import * from os import listdir from os.path import isfile, join import numpy as np onlyfiles = [f for f in sorted(listdir('stimuli')) if isfile(join('stimuli', f))] oi = OpencvIo() u = Util() ## option 1: using saliency_map.py for f in onlyfiles: maps = 0 print('Doing saliency map of file {}'.format(f)) src = oi.imread('stimuli/'+f) sm = SaliencyMap(src) # np.savetxt('results/'+f+'.txt',sm.map) cv.imwrite( 'results/sm'+f, np.uint8(u.normalize_range(sm.map))) cv.imwrite( 'results/cmcolor'+f, np.uint8(u.normalize_range(sm.cm.maps['color']))) cv.imwrite( 'results/cmintensity'+f, np.uint8(u.normalize_range(sm.cm.maps['intensity']))) cv.imwrite( 'results/cmorientation'+f, np.uint8(u.normalize_range(sm.cm.maps['orientation']))) maps+=4 for i in sm.fm.maps['intensity']: maps+=1 cv.imwrite( 'results/fmintensity'+ str(maps) + f, np.uint8(u.normalize_range(i.astype('float64')))) for i in sm.fm.maps['colors']['bg']: maps+=1 cv.imwrite( 'results/fmcolorsbg'+ str(maps) + f, np.uint8(u.normalize_range(i.astype('float64')))) for i in sm.fm.maps['colors']['ry']: maps+=1 cv.imwrite( 'results/fmcolorsry'+ str(maps) + f, np.uint8(u.normalize_range(i.astype('float64'))))