您需要将训练图像中的像素值放入 train_data 中,并在响应中输入与该像素的类别相对应的索引(例如,1 表示皮肤类,0 表示非皮肤类)。var_idx 和 sample_idx 可以保持原样,它们用于屏蔽训练集中的一些描述符或样本。根据您是否一次获得所有描述符(所有训练图像的所有像素)将更新设置为真/假,以防您可以让它为假,或者您逐步处理训练图像(这可能更好地解决内存问题),在这种情况下,您需要更新模型。
让我用代码澄清一下(未检查,并使用我强烈推荐的 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);
}