OpenCV 3.0 Python SVM示例

时间:2016-01-28 17:21:11

标签: python opencv machine-learning svm opencv3.0

https://github.com/Itseez/opencv/blob/master/samples/python/digits.py是opencv中的SVM示例python 3.0是否有人可以解释在循环内的process_hog()函数内部做了什么。提前谢谢!

def preprocess_hog(digits):
    samples = []
    for img in digits:
        gx = cv2.Sobel(img, cv2.CV_32F, 1, 0)
        gy = cv2.Sobel(img, cv2.CV_32F, 0, 1)
        mag, ang = cv2.cartToPolar(gx, gy)
        bin_n = 16
        bin = np.int32(bin_n*ang/(2*np.pi))
        bin_cells = bin[:10,:10], bin[10:,:10], bin[:10,10:], bin[10:,10:]
        mag_cells = mag[:10,:10], mag[10:,:10], mag[:10,10:], mag[10:,10:]
        hists = [np.bincount(b.ravel(), m.ravel(), bin_n) for b, m in zip(bin_cells, mag_cells)]
        hist = np.hstack(hists)

        # transform to Hellinger kernel
        eps = 1e-7
        hist /= hist.sum() + eps
        hist = np.sqrt(hist)
        hist /= norm(hist) + eps

        samples.append(hist)
    return np.float32(samples)

修改

如果我使用hog.compute()而不是

def preprocess_hog(digits):
    samples = []
    hog=cv2.HOGDescriptor()
    idx=0
    for img in digits:
        print idx
        idx+=1
        samples.append(hog.compute(img))
    return np.float32(samples)

它给出了这个错误

Traceback (most recent call last):
  File "handGT.py", line 125, in <module>
    samples = preprocess_hog(digits)
  File "handGT.py", line 110, in preprocess_hog
    return np.float32(samples)
ValueError: setting an array element with a sequence.

0 个答案:

没有答案