2014-07-09 18 views
0

我仍然是新手,我試圖編寫我的第一個類,它最終將包含一堆用於圖像處理的方法。如何將方法中的某個方法返回到Python中的類

我的問題是即使我的方法計算了一些東西,我發現很難將這個結果「返回」到類中。因此,根據我放置「返回」命令的位置 - 我得到的錯誤是該方法未使用,或者它沒有獲得對返回變量的引用(未解析的引用)。

的代碼看起來是這樣的(未解決的參考版本):

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 
+2

請修復您的意願 –

+2

PEP-8男人! :P對我們來說更容易C&P ;-) – YFP

+0

你究竟是什麼意思?我不確定我是否得到你! – user1396713

回答

1

我的猜測是,你的縮進是錯誤的,或者您不需要返回任何東西,只是設置一個類的屬性值。

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) 
0

好吧我想我找到了 - 解決方案在下面!

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 
+0

問題是返回函數的位置(對齊)錯誤! – user1396713

相關問題