-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsaliency.py
66 lines (51 loc) · 2 KB
/
saliency.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# read, display, and save the images
import matplotlib.image as mpimg
import matplotlib.pyplot as plt
# for color space conversion
from skimage.color import rgb2lab
# for some simple profiling
import time
from imutils import paths
import cv2
import numpy as np
# the actual functions
from saliencyMap import getSaliencyMapNumpy
import os
from matplotlib import interactive
import morphsnakes
path = "D:\\Sanjeev\\Saliency Object Detection\\SaliencyMap\\MSRA-B\\images\\"
outPath = "D:\\Sanjeev\\Saliency Object Detection\\SaliencyMap\\a"
def rgb2gray(img):
"""Convert a RGB image to gray scale."""
return 0.2989*img[:, :, 0] + 0.587*img[:, :, 1] + 0.114*img[:, :, 2]
def circle_levelset(shape, center, sqradius, scalerow=1.0):
"""Build a binary function with a circle as the 0.5-levelset."""
grid = np.mgrid[list(map(slice, shape))].T - center
phi = sqradius - np.sqrt(np.sum(grid.T**2, 0))
u = np.float_(phi > 0)
return u
def main():
for image_path in os.listdir(path):
input_path = os.path.join(path, image_path)
# read image
rgbImage = mpimg.imread(input_path)
# convert to lab
labImage = rgb2lab(rgbImage)
# TODO: Matlab scales/shifts values, so we do the same in order to compare results
labImage[:, :, 0] = labImage[:, :, 0] * 2.55
labImage[:, :, 1] = labImage[:, :, 1] + 128
labImage[:, :, 2] = labImage[:, :, 2] + 128
start = time.clock()
# calculate saliency map
sm3 = getSaliencyMapNumpy(labImage)
end = time.clock()
print ("getSaliencyMapNumpy() took", (end - start), " seconds")
output = sm3
# save output
fullpath = os.path.join(outPath, 'crop'+image_path)
mpimg.imsave(fullpath, output)
print('Calculating Active Contour from Saliency Map')
# os.system("python tests.py")
# os.system("python spectral.py")
if __name__ == "__main__":
main()