cerebrou
cerebrou

Reputation: 5540

How to train OpenCV SVM classifier on images

The purpose of the task is to classify images by means of SVM. The variable 'images' is supposed to contain the image information and correspondingly labels contains image labels. How can I build (what format and dimensions) should the images and labels have? I tried unsuccesfully images to be a Python array (appending flattened images) and then, in another attempt, Numpy arrays:

images=np.zeros((number_of_images, image_size))
labels=np.zeros((number_of_images, 1))

svm=cv2.SVM()
svm.train(images, labels)

Is it a right approach to the problem and if so, what is the correct way for training the classifier?

Upvotes: 1

Views: 2267

Answers (1)

CaptainTrunky
CaptainTrunky

Reputation: 1707

I don't think that you can use raw image data to train SVM model. Ok-ok, you can, but it won't be very fruitful.

The basic approach is to extract some features from each image and to use these features for training your model. A set of features forms a dictionary of words, each of which describes your image. Due to the fact that you are using the same set of words to describe each image, you can compare features corresponding to different images. This link introduces more details, check it.

Whats next?

  1. Choose a feature extractor for your algo - HOG, SURF, SIFT (link)
  2. Extract features from each image. You'll get an array of the same length as images array.
  3. Initialize bag-of-words (BoG) model
  4. Train SVM with BoG

Useful links:

  1. C++ vey detailed example
  2. Documentation for existing BOG classifier

Upvotes: 2

Related Questions