Ask Your Question

Image classification using SVM

asked 2016-05-17 09:03:48 -0500

sara1691362 gravatar image

i'm using opencv svm and i want to know if it is possible to know which image is closer to our input image instead of the class which belongs our image.

edit retag flag offensive close merge delete



this might not be possible.

an SVM does not keep the train data around, all it keeps are the support vectors.

berak gravatar imageberak ( 2016-05-17 10:42:01 -0500 )edit

thank you for your answer. can you please tell me what method can i use to solve this problem?

sara1691362 gravatar imagesara1691362 ( 2016-05-17 12:50:26 -0500 )edit

it's not an answer (so far)

an SVM does classification , but your question hints, that you want something slightly different, like finding the 'closest neighbour' ? a simple 'nearest neighbour search' (e.g. comparing all of your train samples to the test one using norm(a,b), and keeping the one with lowest score) would do that, also there's flann::Index

berak gravatar imageberak ( 2016-05-17 13:01:13 -0500 )edit

is there a possible way to get the closest neighbour + the class which belongs the image (using the nearest neighbour ) ?

sara1691362 gravatar imagesara1691362 ( 2016-05-17 16:33:09 -0500 )edit

sidenote: do you really need the closest image ? or would any image from the predicted class do?

berak gravatar imageberak ( 2016-05-18 02:14:37 -0500 )edit

i need the closest image + which class it belongs

sara1691362 gravatar imagesara1691362 ( 2016-05-18 04:56:16 -0500 )edit

1 answer

Sort by ยป oldest newest most voted

answered 2016-05-18 02:11:05 -0500

berak gravatar image

updated 2016-05-18 02:12:22 -0500

so, here is a simple nearest neighbour classification.

(you'll find though, that it is less accurate, than an SVM or ANN_MLP):

void nearest(const cv::Mat &testData, const vector<cv::Mat> &trainData, int &best, double &mind)
    best = -1;
    for (size_t r=0; r<trainData.size(); r++)
        double d = norm(testData, trainData[r]);
        if (d < mind)
            mind = d;
            best = r;

vector<Mat> trainData = ... 
vector<int> trainLabels = ...

Mat testData = ...
int best;
double dist;

nearest(testData, trainData, best, dist);

int predicted = trainLabels[best];
Mat closest = trainData[best];
edit flag offensive delete link more


I am currently working on a project where I have to extract the facial expression from a video. I've extracted the landmarks, now i want to classify the emotions + get the closest image's landmarks. So is there a possibility to do the classification based on the facial landmarks?

sara1691362 gravatar imagesara1691362 ( 2016-05-18 05:53:07 -0500 )edit

sure, just put your landmarks into a Mat

berak gravatar imageberak ( 2016-05-18 06:43:47 -0500 )edit

can you please tell me how can i do that?

sara1691362 gravatar imagesara1691362 ( 2016-05-18 06:44:39 -0500 )edit
Login/Signup to Answer

Question Tools

1 follower


Asked: 2016-05-17 09:03:48 -0500

Seen: 566 times

Last updated: May 18 '16