【发布时间】:2014-08-30 10:00:31
【问题描述】:
我还是 python 新手,我正在尝试编写我的第一个类,它最终将包含一堆图像处理方法。
我的问题是,即使我的方法计算了一些东西,我发现很难将这个结果“返回”给课堂。因此,取决于我放置“return”命令的位置 - 要么我得到一个错误,即方法未使用要么它没有获得对返回变量的引用(未解析的引用)。
代码看起来像这样(未解决参考的版本):
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 ;-)
-
你到底是什么意思?我不确定我是否得到你!
-
也附上错误信息。
-
@user1396713:您的
image_tilling_odd缩进就像它是__init__方法的一部分一样。正确的缩进至关重要。
标签: python class methods return