Reputation: 545
I am still novice in python and I am trying to write my first class that will eventually contain a bunch of methods for image processing.
My problem is that even though my method computes something I find it hard "return" this outcome to the class. So depending where I place the "return" command - either I get an error that method is unused either it does not get the reference to the returned variable (unresolved reference).
The code looks like this (version with unresolved reference) :
from sklearn.feature_extraction import image
import numpy as np
class ImagePreprocessing():
def __init__(self, X=None, Y=None,
tile_size=None, label_computation_method = "majority"):
self.X = X
self.Y = Y
self.tile_size = tile_size
self.label_computation_method = label_computation_method
def image_tilling_odd(X,Y,tile_size,label_computation_method):
"""
This method takes a set of image data and their respectice tiles and constructs a
tilebased vector image - with each tile per row - so the overall size is
(size[0] x size[1] x number of bands)
"""
# asser if tile dimension is the same is all directions
assert tile_size[0] == tile_size[1]
# test if tiles are even or odd
assert tile_size[0] % 2 != 0
assert tile_size[1] % 2 != 0
# tile images and labels
image_tiles=image.extract_patches_2d(X,tile_size)
label_tiles=image.extract_patches_2d(Y,tile_size)
# construct column vector of zeros to store label
Y=np.zeros(label_tiles.size[0])
if label_computation_method == "central_value":
# index of middle element
idx=tile_size[0]/2 +1
# detect middle element and store labels --- central element labeling
for i,rr in enumerate(label_tiles):
# construct vector with middle labels
Y[i]= rr[idx]
COM=np.append(image_tiles,Y,1)
return COM
Upvotes: 0
Views: 213
Reputation: 6320
My guess is that your indentation is wrong or you don't need to return anything and just set a class attribute value.
class MyClass(object):
def __init__(self, x=0, y=0):
super().__init__()
self.x = x
self.y = y
self.result = None
# end Constructor
def method_return(self):
return self.x + self.y
# end method_return
def method_for_attribute(self):
self.result = self.x + self.y
# end method_for_attribute
# end class MyClass
if __name__ == "__main__":
my_instance = MyClass(1, 1)
result = my_instance.method_return() # Returns a value to result
print(result)
print(my_instance.result)
my_instance.method_for_attribute() # Sets the value for my_instance.result
print(my_instance.result)
Upvotes: 1
Reputation: 545
Ok I think I found it - solution below !
from sklearn.feature_extraction import image
import numpy as np
class ImagePreprocessing():
def __init__(self, X=None, Y=None,
tile_size=None, label_computation_method = "majority"):
self.X = X
self.Y = Y
self.tile_size = tile_size
self.label_computation_method = label_computation_method
def image_tilling_odd(X,Y,tile_size,label_computation_method):
"""
This method takes a set of image data and their respectice tiles and constructs a
tilebased vector image - with each tile per row - so the overall size is
(size[0] x size[1] x number of bands)
"""
# asser if tile dimension is the same is all directions
assert tile_size[0] == tile_size[1]
# test if tiles are even or odd
assert tile_size[0] % 2 != 0
assert tile_size[1] % 2 != 0
# tile images and labels
image_tiles=image.extract_patches_2d(X,tile_size)
label_tiles=image.extract_patches_2d(Y,tile_size)
# construct column vector of zeros to store label
Y=np.zeros(label_tiles.size[0])
if label_computation_method == "central_value":
# index of middle element
idx=tile_size[0]/2 +1
# detect middle element and store labels --- central element labeling
for i,rr in enumerate(label_tiles):
# construct vector with middle labels
Y[i]= rr[idx]
COM=np.append(image_tiles,Y,1)
return COM
Upvotes: 0