Ask Your Question
0

I want to classify a object as positive or negative by using SVM, I wrote the following codes. Is the code is right or any suggestions ?

asked 2018-08-14 08:23:24 -0600

Samjith888 gravatar image

updated 2018-08-16 07:02:18 -0600

I have 37 images in my database for each postive and negative for training. I have extracted HOG features of each images and labelled as 'Positive' and 'negative'. its saved into 'Hogfeat' Matrix. Is this code need to be improved or any errors? Now i need to load this features into SVM .(I'm just the learning SVM).I have gone through many links , but its not useful for my codes.Please help me to solve this.

int main()

{

HOGDescriptor hog;
vector<Point> locs;





for (size_t i = 1; i <= 37; ++i)
{

    ostringstream os;
    os << "C:/Users/Sam/Desktop/Images/" << "Content\\" << setw(2) << setfill('0') << i << ".JPG";
    cout << os.str();
    Mat img = imread(os.str(), IMREAD_GRAYSCALE);

    if (!img.data)
    {
        break;
    }
    else
    {

        // obtain feature vector:
        vector<float> featureVector;
        hog.compute(img, featureVector, Size(32, 32), Size(0, 0));


        //HOG features computed for img are stored in featureVector vector to make it into a matrix

        Mat Hogfeat(featureVector.size(), 1, CV_32FC1); //label 1

        for (int i = 0; i < featureVector.size(); i++)
        {
            Hogfeat.at<float>(i, 0) = featureVector.at(i);
        }



        //HOG features are stored in Hogfeat matrix
        cout << Hogfeat;
        cout << "Postive Images";
        system("PAUSE");
    }
}
for (size_t i = 1; i <= 37; ++i)
{

    ostringstream os;
    os << "C:/Users/Sam/Desktop/Images/" << "No humans\\" << setw(2) << setfill('0') << i << ".JPG";
    cout << os.str();
    Mat img = imread(os.str(), IMREAD_GRAYSCALE);

    if (!img.data)
    {
        break;
    }
    else
    {

        // obtain feature vector:
        vector<float> featureVector;
        hog.compute(img, featureVector, Size(32, 32), Size(0, 0));


        //HOG features computed for img are stored in featureVector vector to make it into a matrix

        Mat Hogfeat(featureVector.size(), -1, CV_32FC1); //label -1

        for (int i = 0; i < featureVector.size(); i++)
        {
            Hogfeat.at<float>(i, 0) = featureVector.at(i);
        }



        //HOG features are stored in Hogfeat matrix
        cout << Hogfeat;
        cout << "Negative Images";
    }

}





return 0;

}

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
2

answered 2018-08-14 09:23:33 -0600

berak gravatar image

no, won't work this way. your trainData needs each hog vector stacked as one horizontal row , and you need a seperate labels mat with 1 entry per hog feature. so:

Mat trainData, trainLabels; // initially empty.
for (...) {
    ...
    // obtain feature vector:
    vector<float> featureVector;
    hog.compute(img, featureVector, Size(32, 32), Size(0, 0));
    Mat feature = Mat(featureVector).reshape(1,1); // from col to row
    trainData.push_back(feature);
    trainLabels.push_back(1); // or -1 for the negs.
    ...
}
edit flag offensive delete link more

Comments

@berak: can i have to create two feature vector (for positive and negative)?( In matlab i have created positive.mat for positive images and negative.mat for negative images.)

Samjith888 gravatar imageSamjith888 ( 2018-08-16 01:03:38 -0600 )edit

well, for opencv's SVM, you will need a single trainData Mat containing both positive and negative samples.

berak gravatar imageberak ( 2018-08-16 01:33:57 -0600 )edit

I put 'vector<float> featureVector;' above 'int main()'. then i created for (...) { for 'positive' image feature extraction and labelled as 1} then i did the same for 'negative' images in the next for loop. Is this right way?

Samjith888 gravatar imageSamjith888 ( 2018-08-16 02:04:54 -0600 )edit

sorry, not enough context. "Is this right way" -- doing what ?

and for a classification, you need integer labels, not float.

berak gravatar imageberak ( 2018-08-16 02:06:58 -0600 )edit

@berak: I meant the features are extracted and saved in the 'featureVector' for both positive and negative training images after the two for loop .Is it?

Samjith888 gravatar imageSamjith888 ( 2018-08-16 02:11:58 -0600 )edit

you need a cv::Mat with N(numsamples) rows and M(sizeof_sample) cols, CV_32F. and another labels Mat with N(umsamples) rows, and a single col, an integer label for the resp. sample.

berak gravatar imageberak ( 2018-08-16 02:14:36 -0600 )edit

@berak: Did you meant about the following code? where can i put this code ? Mat Hogfeat(featureVector.size(), 1, CV_32FC1); for(int i=0;i<featureVector.size();i++) Hogfeat.at<float>(i,0)=featureVector.at(i);

Samjith888 gravatar imageSamjith888 ( 2018-08-16 02:22:50 -0600 )edit

that code is simply wrong.. (you're also overwriting the 1st column over and over)

berak gravatar imageberak ( 2018-08-16 02:33:43 -0600 )edit

@berak: Can you correct the above code ?

Hogfeat(N=37;M=featureVector.size();CV_32F) { }

Samjith888 gravatar imageSamjith888 ( 2018-08-16 02:39:10 -0600 )edit

i've already done that in the answer.

berak gravatar imageberak ( 2018-08-16 02:42:16 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2018-08-14 08:23:24 -0600

Seen: 348 times

Last updated: Aug 16 '18