我仍然是新手,我試圖編寫我的第一個類,它最終將包含一堆用於圖像處理的方法。如何將方法中的某個方法返回到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
請修復您的意願 –
PEP-8男人! :P對我們來說更容易C&P ;-) – YFP
你究竟是什麼意思?我不確定我是否得到你! – user1396713