Like Silk: Script: saliencymap

python関連のメモ

画像のSaliencyMapを推定する

code

saliencymap.py←右クリックで保存できます.
#!/usr/bin/env python
#coding: utf-8

##########################################################################################
# It is simple implimentation of the paper:
#
# R. Achanta, S. Hemami, F. Estrada and S. Susstrunk, 
# Frequency-tuned Salient Region Detection, 
# IEEE International Conference on Computer Vision and Pattern Recognition (CVPR), 2009.
#
# for more information
# http://ivrg.epfl.ch/supplementary_material/RK_CVPR09/index.html
# 
# I use the YCrCb color space instead of the LAB color space for the simple implimentation
#
##########################################################################################

from PIL import Image
from PIL import ImageFilter
import numpy as np

def saliencymap(img):
	gfrgb = img.filter(ImageFilter.GaussianBlur(radius=3))
	ycc = gfrgb.convert('YCbCr')
	imgArray = np.float32(np.asarray(ycc))
	m = imgArray.mean(axis=(0,1))
	imwidth, imheight = img.size
	smap = np.zeros((imheight, imwidth))
	for i in xrange(3):
		d = imgArray[:,:,i] - m[i]
		smap = smap + d*d
	return smap

if( __name__ == '__main__' ):
	img = Image.open('a.png')
	smap = saliencymap(img)
	smap = smap / smap.max() * 255
	smapimg = Image.fromarray(np.uint8(smap))
	smapimg.save('smap.png')

参考URL


Like Silk