Ask Your Question
0

Training color histogram values to svm.

asked 2016-04-14 05:17:46 -0600

mrlibby gravatar image

updated 2016-04-14 07:45:19 -0600

Complete newbie here. I am trying to make a classifier using OpenCV 3.0.0's SVM and Color histogram to classify a banana to unripe(more green than yellow peel), ripe(more yellow than green or brown peel), and rotten(more brown than yellow peel) based on the color of the peel. I am already trying to make my own using this code which i'm sure will not work:

int labels[510]; //510 is not the number of images but the combined coordinates in the histograms for red and green. I labeled each coordinate.
if (label.compare("raw")){
    for (int i = 0; i < 509; i++){
        labels[i] = 1;
    }
}
else if (label.compare("ripe")){
    for (int i = 0; i < 509; i++){
        labels[i] = 2;
    }
}
else if (label.compare("rotten")){
    for (int i = 0; i < 509; i++){
        labels[i] = 3;
    }
}    
for (int i = 0; i < 254; i++){
        trainingData[i][1] = r_hist.at<float>(i - 1);
        trainingData[i][2] = i;
    }
int j = 0;
for (int i = 255; i < 509; i++){
    trainingData[i][1] = g_hist.at<float>(j - 1);
    trainingData[i][2] = i;
    j++;
}

I know that there is a more effective way to train the SVM. Can anybody help me with this?

The size of the histogram is 255

edit retag flag offensive close merge delete

Comments

1

r_hist.at<float>(i - 1); will obviously crash for i=0.

what is the size of your histograms ? (r_hist.size())

berak gravatar imageberak ( 2016-04-14 06:15:22 -0600 )edit

The size is 255.

mrlibby gravatar imagemrlibby ( 2016-04-14 06:30:49 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
0

answered 2016-04-14 06:23:18 -0600

berak gravatar image

updated 2016-04-14 23:53:59 -0600

indeed, there are more effective ways to setup your train data.

* use cv::Mat consistently, not integer arrays
* use builtin functions, not loops


Mat responses(510, 1, CV_32S, Scalar(1)); 
// i'm making up the numbers, but i hope, you get the idea,
// initialize the whole labels Mat with your 1st label, then
// set the other parts to the resp. label:
responses(Range(100,200), Range::all()) = 2;
responses(Range(200,510), Range::all()) = 3;


Mat data;
// this assumes, you have a r_hist and a g_hist 
// for each of your 510 input images
for each pair of histograms:
   // combine the histograms into a single, flat column:
   Mat feature;
   // you can skip the reshape, if your hists are already "horizontal" (single column)
   hconcat(r_hist.reshape(1,1), g_hist.reshape(1,1), feature);
   data.push_back(feature);

// then:
svm->train(data, ml::ROW_DATA, responses);

// for prediction later, process your image in the very same way:
// get histograms, r_hist, g_hist, and combine them:
Mat feature;
hconcat(r_hist.reshape(1,1), g_hist.reshape(1,1), feature);

// prediction will be one of the labels, you fed in for training:
int prediction = (int) svm->predict(feature);
edit flag offensive delete link more

Comments

Thank you very much. This is the one I am looking for. Now how do i do a classification using svm->predict?

mrlibby gravatar imagemrlibby ( 2016-04-14 23:47:18 -0600 )edit
1

^^ updated. was it helpful ?

berak gravatar imageberak ( 2016-04-14 23:54:59 -0600 )edit

Yes. One (probably) last thing, how do i show the training data in an image?

mrlibby gravatar imagemrlibby ( 2016-04-15 00:22:00 -0600 )edit

maybe you can use the histogram drawing code from here

berak gravatar imageberak ( 2016-04-15 00:49:21 -0600 )edit

i mean for the svm like this: http://docs.opencv.org/2.4/_images/re...

mrlibby gravatar imagemrlibby ( 2016-04-15 00:51:26 -0600 )edit

hehe, easy, just draw a 512 dimensional image ;)

no, fun apart, you would need something like TSNE (google that !) to project high dimensional data to 2d space

berak gravatar imageberak ( 2016-04-15 01:06:33 -0600 )edit
1

You could also use PCA and keep only the 2 first eigen vector, TSNE is not (yet?) implemented in OpenCV.

Mathieu Barnachon gravatar imageMathieu Barnachon ( 2016-04-15 02:28:42 -0600 )edit

PCA will be for sure easier to do, than tsne

berak gravatar imageberak ( 2016-04-15 02:38:27 -0600 )edit

Nice! Any pull request commng soon? :-)

Mathieu Barnachon gravatar imageMathieu Barnachon ( 2016-04-15 03:25:49 -0600 )edit

it's just a demo / toy, and i'm not even sure, if it's correct

berak gravatar imageberak ( 2016-04-15 03:33:34 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2016-04-14 05:17:46 -0600

Seen: 1,429 times

Last updated: Apr 14 '16