【问题标题】:how to call this function which requires self parameter如何调用这个需要self参数的函数
【发布时间】:2019-05-10 07:49:08
【问题描述】:

我正在尝试测试一个返回 CLD 功能的函数.. 它只需要使用 numpy 库。我正在测试它,但它总是说需要一个 self 参数。我不知道为什么会这样,因为该函数只接收一张我用 opencv 加载的图像。

这是我尝试使用的类: colorlayoutdescriptor.py

import numpy as np

class ColorLayoutDescriptor:
    def __init__(self):
        self.rows = 8
        self.cols = 8
        self.prefix = "CLD"

    def compute(self, img):
        averages = np.zeros((self.rows,self.cols,3))

我希望将一张图像发送到名为compute 的方法并获得一个特征向量,现在我遇到了这个问题::

image = cv2.imread("test.jpg")
vector = ColorLayoutDescriptor.compute(image)

TypeError:compute() 缺少 1 个必需的位置参数:'img'

非常感谢。

【问题讨论】:

  • 你想要ColorLayoutDescriptor().compute(image)
  • 谢谢人..我可以用这种方式访问​​类的方法..但现在我收到这条消息: slice = img[imgH/self.rows * row: imgH/self .rows * (row+1), imgW/self.cols*col : imgW/self.cols*(col+1)] TypeError: slice indices must be integers or None or have an index method
  • 请改为编辑您的问题。 cmets中的代码很难阅读。
  • @Vincent 如果您还有其他问题,请发布另一个问题。
  • 没关系,亲爱的..

标签: python function parameters self


【解决方案1】:

ColorLayoutDescriptor 是一个类,所以你首先要创建一个类的实例:

cld = ColorLayoutDescriptor()
cld.compute(image)

【讨论】:

  • 效果很好.. 但现在我收到了这条消息: slice = img[imgH/self.rows * row: imgH/self.rows * (row+1), imgW/self. cols*col : imgW/self.cols*(col+1)] TypeError: slice indices must be integers or None or have an index method 原来的类是它:github.com/scferrada/imgpedia/blob/master/IMGdescriptors/Python/…我删除了这个参数ColorLayoutComputer(DescriptorComputer): 因为是不必要的.. 是因为那个错误?
  • 看来@fluxens 已经满意地回答了你的问题。如果您还有其他问题,请随时作为单独的问题提出。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-11-19
  • 1970-01-01
相关资源
最近更新 更多