Steven Lee Steven Lee - 1 year ago 202
Python Question

Convert KNN train from Opencv 3 to 2

I am reading a tutorial for training KNN using Opencv. The code is written for Opencv 3 but I need to use it in Opencv 2. The original training is:,, npaClassifications)

I tried using this:

cv2.KNearest().train(npaFlattenedImages, cv2.CV_ROW_SAMPLE, npaClassifications)

but the error is:

Unsupported index array data type (it should be 8uC1, 8sC1 or 32sC1) in function cvPreprocessIndexArray

The full code is here:

Answer Source

You just need to change some lines of code:

# line 60
kNearest = cv2.KNearest()

# line 62
kNearest.train(npaFlattenedImages, npaClassifications)

# line 85
npaContours, npaHierarchy = cv2.findContours(imgThreshCopy,

# line 125
retval, npaResults, neigh_resp, dists = kNearest.find_nearest(npaROIResized, k = 1)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download