-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsobel_energy_calculator.py
65 lines (57 loc) · 2.13 KB
/
sobel_energy_calculator.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
# sobel_energy_calculator - Implementation of EnergyCalculator using
# the Sobel matrix
#
# This file is part of "scarving", a Python implementation of the
# seam carving image resize algorithm
#
# Copyright (C) 2007 Nicolas Trangez <[email protected]>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; version 2 of the License (and no other).
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
#
# EOL
from energy_calculator import EnergyCalculator
from scipy.ndimage.filters import generic_gradient_magnitude, sobel
import sys
from PIL import Image
class SobelEnergyCalculator(EnergyCalculator):
def __init__(self, image):
EnergyCalculator.__init__(self, image)
def calculate_per_pixel(self):
return False
def _calculate_full_energy(self):
return generic_gradient_magnitude(self.get_image_matrix(), derivative = sobel)
def usage(pname):
print "Usage: %s filename.jpg" % pname
def main():
image = None
try:
image = Image.open(sys.argv[-1])
except:
usage(sys.argv[0])
return
ec = SobelEnergyCalculator(image)
energy = ec.get_energy_image()
energy.show()
if __name__ == "__main__":
try:
try:
import cProfile as profile
except ImportError:
import profile
except ImportError:
print "Not profiling"
main()
sys.exit()
print "Profiling using %s" % profile.__name__
profile.run("main()")