Hough变换SimpleCV特征提取器

时间:2015-01-07 23:39:40

标签: machine-learning simplecv orange

我正在尝试为openCV的Hough Circle Transform构建一个新的SimpleCV FeatureExtractor,但是在我的机器学习脚本的训练阶段我遇到了错误。

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

橙/源/橙色/ 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

1 个答案:

答案 0 :(得分: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]

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