Skins stream using Opencv CvNormalBayesClassifier

I am very new to OpenCV. I am trying to use CvNormalBayesClassifier to train my program to find out the colors of pixels in pixels.

Currently, I have about 20 human pictures (faces / other parts of the body) in different conditions of light and background. I also have 20 corresponding answers in which parts of the skin are marked in red and everything else is marked in green.

I have a 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 do I use the current two image libraries that I have to prepare values ​​that can be passed as _train_data and _responses?

Thank you very much.

+4
source share
2 answers

You need to put the pixel values ​​from your training image in train_data, and in the answers you need to put the index corresponding to the class of this pixel (for example, 1 for the skin class, 0 for the non-skin class). var_idx and sample_idx can be left as is, they are used to mask some descriptors or samples in your training set. Set the update to true / false, depending on whether you get all the descriptors (all pixels of all training images) at once if you can allow it to be mistakenly or gradually process training images (which may be better for memory problems), and in In this case, you need to update your model.

Let me explain the code to you (not tested and use the C ++ interface for OpenCV, which I highly recommend 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); 

}

+4
source

I did a Google search in this class, but did not find much information, and in fact even the official opencv document does not provide a direct explanation of the parameters. But I noticed one in the opencv document

The method trains the Normal Bayes classifier. This follows from the convention of the general CvStatModel :: train () approach with the following restrictions:

which direct me to the CvStatModel class, and from there I found something useful. You can probably also take a look at the book from page 471, which gives you more details about this class. There are no Google books in the book .

0
source

Source: https://habr.com/ru/post/1444391/


All Articles