Ask Your Question

Svm error unsupported format or combination of formats ..

asked 2013-09-18 07:12:44 -0600

Tomas gravatar image

updated 2013-09-18 07:21:14 -0600

berak gravatar image

I'm trying to do object detection, training the SVM. The part of training is ok, he read the 2 images and create a file with the learned information. But when the code execute the predict function, he crash and below i post the error message (that i don't understand).

Do you have any idea why this happen?

#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/ml/ml.hpp>

#include <io.h>

using namespace cv;

int main()

    // Set up training data
    int width = 1920, height = 1080;

    int num_files=2;
    std::string files[2]={"tonno1.jpg",

    int img_area=width*height;
    Mat training_mat(num_files,img_area,CV_32FC1);

    for(int z=0;z<num_files;z++){
        Mat img_mat = imread(files[z],CV_32FC1);
        int ii = 0; // Current column in training_mat
        for (int i = 0; i<img_mat.rows; i++) {
            for (int j = 0; j < img_mat.cols; j++) {
      <float>(z,ii++) =<uchar>(i,j);

    Mat labels(num_files,1,CV_32FC1);<float>(0,0)=1.0;<float>(1,0)=0.0;

    // Set up SVM's parameters
    CvSVMParams params;
    params.svm_type    = CvSVM::C_SVC;
    params.kernel_type = CvSVM::POLY;
    params.gamma = 3;
    params.term_crit   = cvTermCriteria(CV_TERMCRIT_ITER, 100, 1e-6);;

    // Train the SVM
    CvSVM svm;
    svm.train(training_mat, labels, Mat(), Mat(), params);"learned.svm");

    return 1;


    CvSVM svm;

    Mat img;
    img=imread("Sequenza 01.Immagine001.jpg",CV_32FC1);

    float f=svm.predict(img);



Here is the image:

image description

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted

answered 2013-09-18 07:19:04 -0600

berak gravatar image

updated 2013-09-18 07:27:53 -0600

you already found out, that your img must be float, but this:

img=imread("Sequenza 01.Immagine001.jpg",CV_32FC1);

won't work. try this instead:

img=imread("Sequenza 01.Immagine001.jpg", 0 ); // load grayscale
Mat im_f;
img.convertTo(im_f, CV_32F);

float f = svm.predict( im_f.reshape(1,1) );    // flattened to a row

also correct the flag when loading the train images (0 for grayscale instead of CV_32F)!

(sidenote: i hope, in the end you're using more than 2 pics for training, and that those are small . )

edit flag offensive delete link more


problem solved, thank you very much! But I have another question, how I can map (get a rectangle that contain the object) the object/objects found in the big image?

Tomas gravatar imageTomas ( 2013-09-18 08:29:19 -0600 )edit

pardon ? the svm just compares images or feature vecs. it does not know about objects.

i don't know enough about your setup there

berak gravatar imageberak ( 2013-09-18 09:24:00 -0600 )edit

I thought it did..ops, I'm very confused because the are a lot oh technique and I'm new of this world. Ok so, if i have to do an object detection I can use something like haar cascades(that work well with a lot of images learned) or something like SIFT,SURF and the others techniques that extract key points from images?

Tomas gravatar imageTomas ( 2013-09-19 11:58:05 -0600 )edit

sorry, mate, i don't understand what you're saying ..

berak gravatar imageberak ( 2013-09-19 12:17:10 -0600 )edit

ok no problem :)

Tomas gravatar imageTomas ( 2013-09-20 03:54:07 -0600 )edit

Question Tools


Asked: 2013-09-18 07:12:44 -0600

Seen: 1,494 times

Last updated: Sep 18 '13