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