简体   繁体   English

使用Opencv CvNormalBayesClassifier训练皮肤像素

[英]Train skin pixels using Opencv CvNormalBayesClassifier

I'm very new to OpenCV. 我对OpenCV很新。 I am trying to use the CvNormalBayesClassifier to train my program to learn skin pixel colours. 我正在尝试使用CvNormalBayesClassifier训练我的程序来学习皮肤像素颜色。

Currently I have got around 20 human pictures (face/other body parts) under different light conditions and backgrounds. 目前,我在不同的光线条件和背景下拍摄了大约20张人物照片(脸部/其他身体部位)。 I have also got 20 corresponding responses in which the skin parts are marked red and everything else marked green. 我还有20个相应的响应,其中皮肤部分标记为红色,其他所有标记为绿色。

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? 我应该如何使用当前的两个图片库来准备可以作为_train_data和_responses传入的值?

Many thanks. 非常感谢。

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 (eg 1 for class skin, 0 for class non-skin). 您需要在训练图像中输入train_data像素值,并在响应中输入与该像素类相对应的索引(例如,1表示类皮肤,0表示类非皮肤)。 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. var_idx和sample_idx可以保留原样,它们用于屏蔽训练集中的一些描述符或样本。 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. 将更新设置为true / false取决于您是否获得所有描述符(所有训练图像的所有像素),以防您可以将其设置为假,或者您可以逐步处理训练图像(这可能更好地解决内存问题) ),在这种情况下,您需要更新您的模型。

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) 让我用代码澄清你(没有检查,并使用OpenCV的C ++接口,我强烈建议而不是旧的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);

} }

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. 我在这个类上进行了谷歌搜索,但没有找到太多信息,实际上甚至官方的opencv文档都没有提供有关参数的直接解释。 But I did notice one thing in opencv document 但我确实在opencv文件中注意到了一件事

The method trains the Normal Bayes classifier. 该方法训练Normal Bayes分类器。 It follows the conventions of the generic CvStatModel::train() approach with the following limitations: 它遵循通用CvStatModel :: train()方法的约定,具有以下限制:

which direct me to CvStatModel class and from there I found something useful. 它指向我CvStatModel类,从那里我发现了一些有用的东西 And probably you can also take a look on the book from page 471 which gives you more details of this class. 也许你也可以看一下第471页的书,它为你提供了这个课程的更多细节。 The book is free from google Books. 这本书是免费的谷歌书籍。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM