Ask Your Question
0

Bayes classifier not working.

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

bob409 gravatar image

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

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:    
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;

}

edit retag flag offensive close merge delete

Comments

" 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 -0600 )edit

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

berak gravatar imageberak ( 2016-02-07 00:12:47 -0600 )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 -0600 )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 -0600 )edit

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

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

your trainData / labels is empty() ?

berak gravatar imageberak ( 2016-02-07 04:01:38 -0600 )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 -0600 )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 -0600 )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 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
0

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

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;
    glob(folderPath,fn,false);

    for (size_t i=0; i<fn.size(); i++)
    {
        Mat img = imread(fn[i], 1);
        if (img.empty()) 
        {
            cerr << "invalid image: " << fn[i] << endl;
            continue;
        }
        img.convertTo(img, CV_32FC1);
        data.push_back(img.reshape(1,1));
        labels.push_back(label);
    }
}

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);
    classifier->save("bayes.xml");
    return 0;
}
edit flag offensive delete link more

Comments

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 -0600 )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 -0600 )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 -0600 )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 -0600 )edit

Question Tools

1 follower

Stats

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

Seen: 327 times

Last updated: Feb 07 '16