2012-11-06 68 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從谷歌圖書免費。