|
@@ -1,5 +1,4 @@
|
|
|
from pylab import imshow, imread, show
|
|
from pylab import imshow, imread, show
|
|
|
-from scipy.misc import imresize
|
|
|
|
|
from matplotlib.pyplot import hist
|
|
from matplotlib.pyplot import hist
|
|
|
from scipy.misc import imresize, imsave
|
|
from scipy.misc import imresize, imsave
|
|
|
|
|
|
|
@@ -8,21 +7,21 @@ class GrayscaleImage:
|
|
|
def __init__(self, image_path = None, data = None):
|
|
def __init__(self, image_path = None, data = None):
|
|
|
if image_path != None:
|
|
if image_path != None:
|
|
|
self.data = imread(image_path)
|
|
self.data = imread(image_path)
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
extension = image_path.split('.',3)[-1]
|
|
extension = image_path.split('.',3)[-1]
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
if extension == "jpg":
|
|
if extension == "jpg":
|
|
|
self.data = self.data[::-1]
|
|
self.data = self.data[::-1]
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
self.convert_to_grayscale()
|
|
self.convert_to_grayscale()
|
|
|
elif data != None:
|
|
elif data != None:
|
|
|
self.data = data
|
|
self.data = data
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
def __iter__(self):
|
|
def __iter__(self):
|
|
|
self.__i_x = -1
|
|
self.__i_x = -1
|
|
|
self.__i_y = 0
|
|
self.__i_y = 0
|
|
|
return self
|
|
return self
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
def next(self):
|
|
def next(self):
|
|
|
self.__i_x += 1
|
|
self.__i_x += 1
|
|
|
if self.__i_x == self.width:
|
|
if self.__i_x == self.width:
|
|
@@ -30,47 +29,47 @@ class GrayscaleImage:
|
|
|
self.__i_y += 1
|
|
self.__i_y += 1
|
|
|
if self.__i_y == self.height:
|
|
if self.__i_y == self.height:
|
|
|
raise StopIteration
|
|
raise StopIteration
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
return self.__i_y, self.__i_x, self[self.__i_y, self.__i_x]
|
|
return self.__i_y, self.__i_x, self[self.__i_y, self.__i_x]
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
def __getitem__(self, position):
|
|
def __getitem__(self, position):
|
|
|
return self.data[position]
|
|
return self.data[position]
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
def convert_to_grayscale(self):
|
|
def convert_to_grayscale(self):
|
|
|
if len(self.data.shape) > 2:
|
|
if len(self.data.shape) > 2:
|
|
|
self.data = self.data[:,:,:3].sum(axis=2) / 3
|
|
self.data = self.data[:,:,:3].sum(axis=2) / 3
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
def crop(self, rectangle):
|
|
def crop(self, rectangle):
|
|
|
- self.data = self.data[rectangle.y : rectangle.y + rectangle.height,
|
|
|
|
|
|
|
+ self.data = self.data[rectangle.y : rectangle.y + rectangle.height,
|
|
|
rectangle.x : rectangle.x + rectangle.width]
|
|
rectangle.x : rectangle.x + rectangle.width]
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
def show(self):
|
|
def show(self):
|
|
|
imshow(self.data, cmap="gray")
|
|
imshow(self.data, cmap="gray")
|
|
|
show()
|
|
show()
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
def make_histogram(self):
|
|
def make_histogram(self):
|
|
|
return hist(self.data)
|
|
return hist(self.data)
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
def resize(self, size): # size is of type float
|
|
def resize(self, size): # size is of type float
|
|
|
self.data = imresize(self.data, size)
|
|
self.data = imresize(self.data, size)
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
def get_shape(self):
|
|
def get_shape(self):
|
|
|
return self.data.shape
|
|
return self.data.shape
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
shape = property(get_shape)
|
|
shape = property(get_shape)
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
def get_width(self):
|
|
def get_width(self):
|
|
|
return self.get_shape()[1]
|
|
return self.get_shape()[1]
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
width = property(get_width)
|
|
width = property(get_width)
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
def get_height(self):
|
|
def get_height(self):
|
|
|
return self.get_shape()[0]
|
|
return self.get_shape()[0]
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
height = property(get_height)
|
|
height = property(get_height)
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
def in_bounds(self, y, x):
|
|
def in_bounds(self, y, x):
|
|
|
return x >= 0 and x < self.width and y >= 0 and y < self.height
|
|
return x >= 0 and x < self.width and y >= 0 and y < self.height
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
def save(self, path):
|
|
def save(self, path):
|
|
|
imsave(path, self.data)
|
|
imsave(path, self.data)
|