Bayes classifier not working.
int main() { cv::Ptr<cv::ml::normalbayesclassifier> classifier = cv::ml::NormalBayesClassifier::create();
ifstream myfile("positive.txt");
// new lines will be skipped unless we stop it from happening:
myfile.unsetf(std::ios_base::skipws);
// count the newlines with an algorithm specialized for counting:
unsigned line_count = std::count(
std::istream_iterator<char>(myfile),
std::istream_iterator<char>(),
'\n');
myfile.open("positive.txt");
cout << line_count;
string line;
string img_location;
Mat trainData;
Mat labels;
myfile >> line;
int num = line_count;
for (int i = 0; i < num; i++)
{
cout << line << endl;
Mat src = imread(line, 1);
if (!src.empty())
{
src = src.reshape(1, 1);
//cvtColor(src, src, CV_32S);
src.convertTo(src, CV_32FC1);
trainData.push_back(src);
labels.push_back(1);
myfile >> line;
}
}
trainData.convertTo(trainData, CV_32FC1);
//cout << labels. << endl;
labels.convertTo(labels, CV_32FC1);
classifier->train(trainData, cv::ml::ROW_SAMPLE,labels);
classifier->save("bayes.xml");
cout << "Done" << endl;
return 0;
}
" But i am getting an error invalid null pointer." -- where exactly do you get that error ?
also imho, it should be
src = src.reshape(1, 1);
, notsrc = src.reshape(1, src.rows*src.cols);
strcat("Training Images/SKIN/35/positive.txt",line)
-- this is probably not a filenameI edited the code above. Now I am getting another error. I posted a screenshot above.
now where do you get that error ?
(please learn to use the vs debugger, it's actually pretty good !)
classifier->train(trainData, cv::ml::ROW_SAMPLE,labels);
your trainData / labels is empty() ?
The third parameter in classifier->train() represents what. I thought its just for labelling the trainData.
3rd param is a labels array, one for each row/feature.
you get one of those labels back, when you do the prediction later
btw, your loop above does not read a new line , if the previous image was invalid. ;(