【问题标题】:Extending numpy arrays扩展 numpy 数组
【发布时间】:2014-04-16 16:36:43
【问题描述】:

我是 Python/numpy 的新手。

我正在尝试扩展 numpy.array 以赋予它一些使其更适合表示图像的功能(例如转换为灰度等)。

import numpy as np
import cv2
from support import *
import matplotlib.pyplot as plt

class Frame(np.array):
    def __init__(self):
        print "new frame"


f = Frame()

目前这给了我:

  File "o.py", line 6, in <module>
    class Frame(np.array):
TypeError: Error when calling the metaclass bases
    cannot create 'builtin_function_or_method' instances

我不明白为什么这是 Python 的问题?

【问题讨论】:

  • 作为旁注,您应该查看:PIL Python Imaging Library pythonware.com/products/pil/#pil117。它具有这些功能,并且与 ndarray 之间的转换非常简单
  • 如果你打算使用 PIL,你应该试试它的 fork pillow,这是一个更新版本,也兼容 Python 3。

标签: python arrays opencv numpy


【解决方案1】:

您希望继承np.ndarray,而不是np.array,但这比在您的示例中换掉另一个要复杂一些。可能值得看一下文档: http://docs.scipy.org/doc/numpy/user/basics.subclassing.html

【讨论】:

  • 谢谢。最后,我“围绕”它进行分类,而不是扩展它,看起来是更安全的选择。
猜你喜欢
  • 2011-01-22
  • 1970-01-01
  • 1970-01-01
  • 2017-12-15
  • 1970-01-01
  • 1970-01-01
  • 2020-04-15
  • 2019-08-31
  • 1970-01-01
相关资源
最近更新 更多