ArthurN
ArthurN

Reputation: 179

OpenCV SVM prediction inconsistent?

This question has been asked here, but still no answer/solution. My problem is this: I trained an SVM (with RBF kernel) for smoke detection, using the RGB histogram distribution (in 8 bins - so M = 24) of the smoke:

cv::Mat labelsMat = cv::Mat(N, 1, CV_32SC1);
for (int i = 0; i < N; i++)
{
    labelsMat.at<int>(i, 0) = labels[i];
}
cv::Mat trainingDataMat = cv::Mat(N, M, CV_32FC1);
for (int i = 0; i < N; i++)
{
    for (int j = 0; j < M; j++)
    {
        trainingDataMat.at<float>(i, j) = histogramData[i][j];
    }
}

// Create the SVM
cv::Ptr<ml::SVM> svm = ml::SVM::create();
svm->setType(ml::SVM::C_SVC);
svm->setKernel(ml::SVM::RBF);
svm->setTermCriteria(cv::TermCriteria(cv::TermCriteria::MAX_ITER, 1000, 1e-8));

// Train the SVM
svm->trainAuto(trainingDataMat, ml::ROW_SAMPLE, labelsMat);
svm->save(SVMFileName);

Then I saved the SVM model in a file. For the detection, after loading the SVM model:

svm = cv::ml::SVM::load(SVMFile);

I proceeded with the smoke detection; in this case to decide for each detected blob in a frame whether it's smoke or not:

for (int i = 0; i < 8; i++)
    histogramData.at<float>(0, i) = Rhist[i];
for (int i = 8; i < 16; i++)
    histogramData.at<float>(0, i) = Ghist[i];
for (int i = 16; i < 24; i++)
    histogramData.at<float>(0, i) = Bhist[i];

float response = svm->predict(histogramData);

The frames where detection (true/false positive) occurs are saved, with the frame no. When I run this on the same video several times, each time different results (frame no.) will be produced (the blob detection always produces the same blobs). Regarding the detection, sometimes (most of the time) the smoke will be detected, but there are some cases where the same smoke will not be detected (the same video).

Anybody has any idea how to resolve this? Or is this still a known problem in OpenCV SVM?

Upvotes: 1

Views: 63

Answers (1)

ArthurN
ArthurN

Reputation: 179

Just realized my stupid error in the code: the indexing of Ghist & Bhist to form the data for prediction is totally incorrect, hence the inconsistencies!

Upvotes: 1

Related Questions