2012-11-06 48 views
4

我对OpenCV非常陌生。我正在尝试使用CvNormalBayesClassifier来训练我的程序以学习皮肤像素颜色。使用Opencv训练皮肤像素CvNormalBayesClassifier

目前在不同的光照条件和背景下,我已经有大约20张人体图片(脸部/其他身体部位)。我还得到了20个相应的响应,其中皮肤部分标记为红色,其他标记为绿色。

我有问题,了解如何使用功能

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

我应该如何使用我已经拿到了目前的两个图片库编写一个可以传递作为_train_data和_responses值是多少?

非常感谢。

回答

4

你需要把train_data像素值从训练图像,并在答复对应的类此像素的指数(例如1类皮肤,0类非皮肤)。 var_idx和sample_idx可以保持不变,它们用于掩盖训练集中的一些描述符或样本。设置更新到真/假取决于无论您得到的所有描述符(所有的训练图像的所有像素)的情况下,一旦你可以把它让到假的,或者您逐步处理你的训练图像(这可能是内存问题更好),在这种情况下,您需要更新模型。

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

}

0

我做了这个类谷歌搜索,但没有找到太多的信息,居然连官方OpenCV的文件没有提供有关参数直接的解释。但是,我也注意到OpenCV的文档中的一件事

的方法训练的普通贝叶斯分类器。它遵循与 以下限制通用CvStatModel ::列车()方法的 约定:

这直接我CvStatModel类,并从那里我发现something有用。也许你也可以看一下第471页的书,它会给你更多关于这堂课的细节。 The book从谷歌图书免费。