0

我正在尝试为 openCV 的霍夫圆变换构建一个新的 SimpleCV FeatureExtractor,但在机器学习脚本的训练阶段遇到了错误。

我在下面提供了错误。它是由 Orange 机器学习库self.mDataSetOrange在 SimpleCV 的 TreeClassifier.py 中创建变量时提出的。由于某种原因,数据集的大小与 Orange 的预期不符。我查看了 Orange 的源代码,发现这里抛出了错误:

橙色/源/橙色/cls_example.cpp

int const nvars = dom->variables->size() + dom->classVars->size();
if (Py_ssize_t(nvars) != PyList_Size(lst)) {
    PyErr_Format(PyExc_IndexError, "invalid list size (got %i, expected %i items)",
        PyList_Size(lst), nvars);
    return false;
}

显然,我的特征提取器没有提取 Orange 要求的内容,但我无法确定问题可能是什么。我对 SimpleCV 和 Orange 还很陌生,所以如果有人能指出我所犯的任何错误,我将不胜感激。

错误:

Traceback (most recent call last):
  File "MyClassifier.py", line 113, in <module>
    MyClassifier.run(MyClassifier.TRAIN_RUN_TYPE, trainingPaths)
  File "MyClassifier.py", line 39, in run
    self.decisionTree.train(imgPaths, MyClassifier.CLASSES, verbose=True)
  File "/usr/local/lib/python2.7/dist-packages/SimpleCV-1.3-py2.7.egg/SimpleCV/MachineLearning/TreeClassifier.py", line 282, in train
    self.mDataSetOrange = orange.ExampleTable(self.mOrangeDomain,self.mDataSetRaw)
IndexError: invalid list size (got 266, expected 263 items) (at example 2)

HoughTransformFeatureExtractor.py

class HoughTransformFeatureExtractor(FeatureExtractorBase):

    def extract(self, img):
        bitmap = img.getBitmap()
        cvMat = cv.GetMat(bitmap)
        cvImage = numpy.asarray(cvMat)

        height, width = cvImage.shape[:2]
        gray = cv2.cvtColor(cvImage, cv2.COLOR_BGR2GRAY)

        circles = cv2.HoughCircles(gray, cv2.cv.CV_HOUGH_GRADIENT, 2.0, width / 2)
        self.featuresLen = 0

        if circles is not None:
            circleFeatures = circles.ravel().tolist()
            self.featuresLen = len(circleFeatures)

            return circleFeatures
        else:
            return None

    def getFieldNames(self):
        retVal = []
        for i in range(self.featuresLen):
            name = "Hough"+str(i)
            retVal.append(name)
        return retVal

    def getNumFields(self):
        return self.featuresLen
4

1 回答 1

0

所以,我想出了我的问题。基本上,问题在于该extract方法返回的列表的大小。每个处理过的图像的列表大小各不相同,这就是导致此错误的原因。因此,以下是该方法返回的列表类型的一些示例extract

3 -> [74.0, 46.0, 14.866068840026855]
3 -> [118.0, 20.0, 7.071067810058594]
6 -> [68.0, 8.0, 8.5440034866333, 116.0, 76.0, 13.03840446472168]
3 -> [72.0, 44.0, 8.602325439453125]
9 -> [106.0, 48.0, 15.81138801574707, 20.0, 52.0, 23.409399032592773, 90.0, 122.0, 18.0]

一旦我确定列表的大小是一致的,无论图像如何,错误都会消失。希望这将有助于将来遇到类似问题的任何人。

于 2015-01-08T01:10:37.190 回答