Wendy
Wendy

Reputation: 43

Train skin pixels using Opencv CvNormalBayesClassifier

I'm very new to OpenCV. I am trying to use the CvNormalBayesClassifier to train my program to learn skin pixel colours.

Currently I have got around 20 human pictures (face/other body parts) under different light conditions and backgrounds. I have also got 20 corresponding responses in which the skin parts are marked red and everything else marked green.

I have problem understanding how to use the function

bool CvNormalBayesClassifier::train(const CvMat* _train_data, const CvMat* _response, const Cv*Mat _var_idx = 0, const CvMat* _sample_idx=0,, bool update=false);

How should I use the current two picture libraries I have got to prepare the values that can be passed in as _train_data and _responses?

Many thanks.

Upvotes: 4

Views: 3707

Answers (2)

remi
remi

Reputation: 3988

You need to put in train_data the pixel values from your training image, and in responses an index corresponding to the class of this pixel (e.g. 1 for class skin, 0 for class non-skin). var_idx and sample_idx can be left as is, they are used to mask out some of the descriptors or samples in your training set. Set update to true/false depending on wether you get all the descriptors (all the pixels of all your training images) at once in case you can let it to false, or you process your training images incrementally (which might be better for memory issues), in which case you need to update your model.

Let me clarify you with the code (not checked, and using the C++ interface to OpenCV which I strongly recommand instead of the old C)

int main(int argc, char **argv)
{

  CvNormalBaseClassifier classifier;
  for (int i = 0; i < argc; ++i) {
    cv::Mat image = // read in your training image, say cv::imread(argv[i]);
    // read your mask image
    cv::Mat mask = ...
    cv::Mat response = mask == CV_RGB(255,0,0); // little trick: you said red pixels in your mask correspond to skin, so pixels in responses are set to 1 if corresponding pixel in mask is red, 0 otherwise.
    cv::Mat responseInt;
    response.convertTo(responsesInt, CV_32S); // train expects a matrix of integers

    image = image.reshape(0, image.rows*image.cols); // little trick number 2 convert your width x height, N channel image into a witdth*height row matrix by N columns, as each pixel should be considere as a training sample.
    responsesInt = responsesInt.reshape(0, image.rows*image.cols); // the same, image and responses have the same number of rows (w*h).

    classifier.train(image, responsesInt, 0, 0, true);

}

Upvotes: 4

james
james

Reputation: 1155

I did a google search on this class but didn't find much information, and actually even the official opencv document does not provide direct explanation on the parameters. But I did notice one thing in opencv document

The method trains the Normal Bayes classifier. It follows the conventions of the generic CvStatModel::train() approach with the following limitations:

which direct me to CvStatModel class and from there I found something useful. And probably you can also take a look on the book from page 471 which gives you more details of this class. The book is free from google Books.

Upvotes: 0

Related Questions