Ask Your Question

Bayes classifier not working.

asked 2016-02-06 17:35:17 -0500

bob409 gravatar image

updated 2016-02-07 02:57:22 -0500

image description

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:    

// count the newlines with an algorithm specialized for counting:
unsigned line_count = std::count(
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);



        myfile >> line;


trainData.convertTo(trainData, CV_32FC1);
//cout << labels. << endl;
labels.convertTo(labels, CV_32FC1);
classifier->train(trainData, cv::ml::ROW_SAMPLE,labels);

cout << "Done" << endl;

return 0;


edit retag flag offensive close merge delete


" 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); , not src = src.reshape(1, src.rows*src.cols);

berak gravatar imageberak ( 2016-02-06 23:02:53 -0500 )edit

strcat("Training Images/SKIN/35/positive.txt",line) -- this is probably not a filename

berak gravatar imageberak ( 2016-02-07 00:12:47 -0500 )edit

I edited the code above. Now I am getting another error. I posted a screenshot above.

bob409 gravatar imagebob409 ( 2016-02-07 02:55:35 -0500 )edit

now where do you get that error ?

(please learn to use the vs debugger, it's actually pretty good !)

berak gravatar imageberak ( 2016-02-07 03:08:46 -0500 )edit

classifier->train(trainData, cv::ml::ROW_SAMPLE,labels);

bob409 gravatar imagebob409 ( 2016-02-07 03:27:32 -0500 )edit

your trainData / labels is empty() ?

berak gravatar imageberak ( 2016-02-07 04:01:38 -0500 )edit

The third parameter in classifier->train() represents what. I thought its just for labelling the trainData.

bob409 gravatar imagebob409 ( 2016-02-07 04:19:32 -0500 )edit

3rd param is a labels array, one for each row/feature.

you get one of those labels back, when you do the prediction later

berak gravatar imageberak ( 2016-02-07 04:31:15 -0500 )edit

btw, your loop above does not read a new line , if the previous image was invalid. ;(

berak gravatar imageberak ( 2016-02-07 04:35:40 -0500 )edit

1 answer

Sort by ยป oldest newest most voted

answered 2016-02-07 04:27:56 -0500

berak gravatar image

i'd recommend, that you get rid of your text files, and instead put your positive images into one folder, and the negatives into another.

then, use cv::glob to read in the directories, and get the data, like this:

void readData(String folderPath, Mat &data, Mat &labels, int label)
    vector<String> fn;

    for (size_t i=0; i<fn.size(); i++)
        Mat img = imread(fn[i], 1);
        if (img.empty()) 
            cerr << "invalid image: " << fn[i] << endl;
        img.convertTo(img, CV_32FC1);

int main()
    Mat trainData;
    Mat trainLabels;
    readData("c:/path/to/positives/*.png", trainData, trainLabels, 1);
    readData("c:/path/to/negatives/*.png", trainData, trainLabels, 0);

    cv::Ptr<cv::ml::NormalBayesClassifier> classifier = cv::ml::NormalBayesClassifier::create();
    classifier->train(trainData, cv::ml::ROW_SAMPLE, trainLabels);
    return 0;
edit flag offensive delete link more


Thanks I will try it. Do I have to train non-skin pixels data as well. I am using bayesian classifier to detect skin regions in a video.

bob409 gravatar imagebob409 ( 2016-02-07 04:33:40 -0500 )edit

yes, you need negatives.

and i'm having doubts, that your approach will work properly. (far too naive, imho)

so, that's whole video images above ? since you need a lot of samples, you probably will have to resize them, else you'll run out of memory fast.

berak gravatar imageberak ( 2016-02-07 04:45:11 -0500 )edit

So what other approaches do you suggest me to use which can be used to detect skin region in a video?

bob409 gravatar imagebob409 ( 2016-02-07 04:51:28 -0500 )edit

idk, maybe do not classify whole images but either single pixels (in various colorspaces), or histograms (again , colorspaces)

whole images will always have a 'locality' problem

berak gravatar imageberak ( 2016-02-07 05:44:31 -0500 )edit
Login/Signup to Answer

Question Tools

1 follower


Asked: 2016-02-06 17:35:17 -0500

Seen: 156 times

Last updated: Feb 07 '16