你需要把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);
}