Ask Your Question

bob409's profile - activity

2018-02-16 03:10:48 -0600 received badge  Popular Question (source)
2016-06-14 06:18:43 -0600 marked best answer Train images with SVM

I have used HOGDescriptors to compute the feature vector of an image, now I have to use SVM to create training set for a class. I have to create multiclass SVM as i have 6 classes each with 10 images. I do not know how to convert vector into Mat and how to label the class to create an xml file?

hog.compute(drawing, ders, Size(1, 1), Size(0, 0));



Mat Hogfeat;
Hogfeat.create(ders.size(), 1, CV_32FC1);

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


}

int labels = {1};
Mat labelsMat(1, 1, CV_32SC1, labels);

Ptr<ml::SVM> svm = ml::SVM::create();
svm->setType(ml::SVM::C_SVC);
svm->setKernel(ml::SVM::LINEAR);
svm->setGamma(3);
svm->setTermCriteria(TermCriteria(TermCriteria::MAX_ITER, 100, 1e-6));

Ptr<ml::TrainData> tData = ml::TrainData::create(Hogfeat, ml::SampleTypes::ROW_SAMPLE, labelsMat);
svm->train(tData);

svm->save("testing.xml");
2016-04-10 04:09:51 -0600 asked a question Cannot retrieve matrix from an xml file using FileStorage

I am storing features in form of a matrix in an xml file using FileStorage

FileStorage f("trial.xml",FileStorage::WRITE);
Mat feature;
f<<"feature"<<feature;

By executing the above code, a trial.xml file is created

<?xml version="1.0"?>
<opencv_storage>
<features type_id="opencv-matrix">
  <rows>1</rows>
  <cols>8</cols>
  <dt>f</dt>
  <data>
   3.35365832e-001 1.21951215e-002 9.14634094e-002 6.30081296e-002
    3.31300795e-001 1.01626012e-002 9.95934904e-002 5.69105670e-002</data></features>
 </opencv_storage>

However, I cannot retrive the matrix from the xml file.

FileStorage fs("trial.xml",FileStorage::READ);
Mat data;
fs["data"]>>data;
cout<<data<<endl;

The Mat data is empty: []

2016-04-07 12:29:46 -0600 commented question How to interpret predict parameters obtained from KNN?

I have not thrown some random numbers. I am working with KNN and I got this problem that is why I am asking. I do not understand how to get the correct label. Thanks for the link. It is the sample which which I have trained the data so knn should output label 1

2016-04-07 12:07:57 -0600 commented question How to interpret predict parameters obtained from KNN?

I have alreday read that. What I am asking is that is class 1 dist is 0 why am i getting response as class 0 instead of class 1

2016-04-07 08:31:36 -0600 asked a question How to interpret predict parameters obtained from KNN?

I have trained KNN to recognise obj A as 1

When I am doinh knn->findKnearest(data,m,response,neighbor,dist);

I get response as 0; Then neighbor [1,0,...] dist[0,0.23333]

What does this mean? Do I have to use euclidean distance to do matching?

2016-04-05 16:41:03 -0600 asked a question How to train data for Neural Network?

How to input labels for classes in NN. Are they like SVM give different labels to each class. I have not really understood how to create the training class for neural network? I do not know how to use it for training I have tried the following way:

void mlp(cv::Mat& trainingData, vector<int>& index)
{
    int input_neurons = 8;
    int hidden_neurons = 100;
    int output_neurons = 12;
    Mat layerSizes = Mat(3, 1, CV_32SC1);
    layerSizes.row(0) = Scalar(input_neurons);
    layerSizes.row(1) = Scalar(hidden_neurons);
    layerSizes.row(2) = Scalar(output_neurons);

    Ptr<ml::ANN_MLP> mlp = ml::ANN_MLP::create();
    mlp->setLayerSizes(layerSizes);
    mlp->setTrainMethod(ml::ANN_MLP::SIGMOID_SYM);
    mlp->setTermCriteria(TermCriteria(CV_TERMCRIT_ITER | CV_TERMCRIT_EPS, 1000, 0.00001f));
    mlp->setTrainMethod(ml::ANN_MLP::BACKPROP,0.1f,0.1f);
    mlp->setActivationFunction(ml::ANN_MLP::SIGMOID_SYM, 1, 1);

    Mat trainClasses;

    cout << "Poker" << endl;
    trainClasses.create(trainingData.rows, 12, CV_32FC1);
    for (int i = 0; i < trainClasses.rows; i++)
    {
        trainClasses.at<float>(i, index[i]) = 1.f;
    }

    cout << "Row of trainClass: " << trainClasses.cols << endl;
    cout << "Row of trainData: " << trainingData.cols << endl;
    cout << "Koker" << endl;
    Ptr<ml::TrainData> td = ml::TrainData::create(trainingData, ml::ROW_SAMPLE, trainClasses);
    mlp->train(td);
    cout << "Training Done" << endl;

    mlp->save("neural_network.xml");

}

When I am predicting, I am getting the following response:

[-4.4227011e+0008,-404295997e+008,-4.4214237e+008,-4.3398125e+008,.......]

What is wrong and how to interpret the response from an ANN?

2016-04-05 12:02:24 -0600 asked a question I cannot train data using knn

I am getting this error:

OpenCV Error: Assertation failed <samples.type<>CV_32F || samples.type<>CV_32S> in cv::ml::TrainDataImpl::setData

I have extracted feature and placed it in a String;

//for loop

String features = "352111653512146120635735005";

Mat m;
    m.push_back(features);
    m.reshape(1, 1);

    freeman.push_back(m);
             labelMat.push_back(label);

             Ptr<ml::KNearest> knn = ml::KNearest::create();
        Ptr<ml::TrainData> td = ml::TrainData::create(feature, ml::ROW_SAMPLE, label);  <-----I am getting error here
2016-04-05 10:39:02 -0600 commented question Fusion of two feature vectors

What is the problem in doing that if I am getting the result? I know only c-Api to extract chain code.

2016-04-04 11:06:52 -0600 asked a question Fusion of two feature vectors

I have extracted two feature vectors from an image:

  1. chain code histogram
  2. fourier descriptors

How to fuse them and train them using svm?

I have tried hconcat but it is not working. When i am concatenatenating: hconcat(fourierdescriptor,mergeMatrix)

mergeMatrix is Null

 Mat fd;

vector<vector<Point> > contours;
vector<Vec4i> hierarchy;
findContours(inputImage, contours, hierarchy, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE, Point(0, 0));

Mat drawing = Mat::zeros(inputImage.size(), CV_8UC1);

int s = findBiggestContour(contours);

drawContours(drawing, contours, s, Scalar(255), 1, 8, hierarchy, 0, Point());

imshow("draw", drawing);

vector<int> ctrSelec;
ctrSelec.push_back(s);

vector<Point2d> z;
vector<Point2d> Z;

z.resize(ctrSelec.size());
Z.resize(ctrSelec.size());


vector<Point2d> c = ReSampleContour(contours[ctrSelec[0]], 30);

Mat mc = Mat::zeros(drawing.size(), CV_8UC3);


    for (int j = 0; j < c.size(); j++)
    {
        z.push_back(c[(j * 10) % c.size()]);

        dft(z, Z, DFT_SCALE | DFT_REAL_OUTPUT);
    }


    fd.push_back(Z);

    for (int i = 0; i < Z.size(); i++)
    {
        cout << Z[i].x << "," << Z[i].y << endl;
    }
//medianBlur(drawing, drawing, 3);

CvChain* chain;
CvMemStorage* storage = 0;
storage = cvCreateMemStorage();
cvFindContours(&IplImage(drawing), storage, (CvSeq**)(&chain), sizeof(*chain), CV_RETR_EXTERNAL, CV_CHAIN_CODE);



int total = chain->total;

cv::Mat hist(1, 8, CV_32F, Scalar(0));

int totalCount = 0;
for (; chain != NULL; chain = (CvChain*)chain->h_next)
{

    int numChain = 0;

    CvSeqReader reader;
    int i, total = chain->total;

    cvStartReadSeq((CvSeq*)chain, &reader, 0);


    for (i = 0; i < total; i++)
    {
        char code;
        CV_READ_SEQ_ELEM(code, reader);
        int Fchain = (int)code;


        hist.at<float>(0, Fchain)++;

        totalCount++;
    }
}


    Mat prob = hist / totalCount;


    cch = prob.clone();

    Mat feature;
           hconcat(fd,cch,feature)
2016-04-04 06:05:38 -0600 commented question DFT and IDFT of contour, Fourier descriptors

So was it good?

2016-04-04 02:37:51 -0600 commented answer How to find the rotated angle of object

I dnt nid fourier descriptor which are rotational invariant they just need to be translation and scale invariant. It means that u have 1024 fourier descriptors

2016-04-04 02:20:15 -0600 commented answer How to find the rotated angle of object

What should be taken into consideration while choosing a value? Why have u chosen 1024, was it derived by experimental result? And where do u get the fourier descriptors in the code above? Have u made them scale and starting point invariant? If yes where?

2016-04-04 01:04:32 -0600 commented answer How to find the rotated angle of object

What does the following line perform? vector<point2d> c = ReSampleContour(contours[ctrSelec[i]],1024); Why 1024? Has anyone understood it?

2016-04-02 10:46:18 -0600 asked a question Which distance metric does SVM->predict uses

When using binary svm for classification, it returns a value when using a value when using predict? What does the value mean? It is the distance between the test data and the predicted data. If someone knows the answer please help.

2016-04-02 10:42:42 -0600 commented question how to install

What is not working> What error are you getting?

2016-04-02 04:48:20 -0600 commented question Setting parameters to compute HoG

How to normalize hog feature?

2016-04-02 00:24:47 -0600 commented question how to install

You have to set environmental variables.Then you have to change some setting in ur project->properties. This video will help u https://www.youtube.com/watch?v=JrIpA.... Just make the necessary changes as in the video the person is using vs13 and opencv 3.0 n u vs10 with opencv 3.1.0. Try it

2016-04-01 19:56:48 -0600 commented question Setting parameters to compute HoG

Thnks. Should I normalize the feature vector afterwards or it is already normalized?

2016-04-01 11:44:03 -0600 asked a question Setting parameters to compute HoG

My image is of size 128x128. Should I use a win_Size of 128x128 as well? And how to select values for the other parameters as well: HOGDescriptor(Size win_size=Size(128, 128), Size block_size=Size(16, 16), Size block_stride=Size(8, 8), Size cell_size=Size(8, 8), int nbins=9, double win_sigma=DEFAULT_WIN_SIGMA, double threshold_L2hys=0.2, bool gamma_correction=true, int nlevels=DEFAULT_NLEVELS)

I have seen the explanation at http://docs.opencv.org/2.4/modules/gp... but it is not clear enough on how to set the parameters properly.

2016-03-27 02:11:15 -0600 commented answer How to combine several features of a hand for machine learning

Thanks a lot.

2016-03-26 13:46:14 -0600 asked a question How to combine several features of a hand for machine learning

I have extracted the following features of hand: compactness, aspect ration and orientation and stored them in a .csv file. Now I want to train them using KNN. KNN takes a matrix as input, how should I combines these features into a single Mat and train them.

2016-03-26 03:36:16 -0600 commented answer Chain Code Histogram

I did not understand by what you want to say by improving it.

2016-03-26 02:54:35 -0600 commented answer Chain Code Histogram

12 different classes and each class has 10 instances.

2016-03-26 02:13:27 -0600 commented answer Chain Code Histogram

Thanks. Which classification method is more appropriate for chain code histogram feature?

2016-03-26 02:12:12 -0600 marked best answer Chain Code Histogram

I have already extracted chain code of the contour, calculated the number of occurence of each code. Then calculated the frequency of each code. Now I have to plot it in a histogram, I do not know how to create a histogram for chain code the no. of bins should be 8 thats all I know. But how to create it in opencv 3.0 using c++?

int main()
{
int count0 = 0;
int count1 = 0;
int count2 = 0;
int count3 = 0;
int count4 = 0;
int count5 = 0;
int count6 = 0;
int count7 = 0;

double totalCount = 0;

Mat image = imread("rectangle.jpg", 0);

Canny(image, image, 100, 100 * 2, 3, false);

CvChain* chain;
CvMemStorage* storage = 0;
storage = cvCreateMemStorage();

cvFindContours(&IplImage(image), storage, (CvSeq**)(&chain), sizeof(*chain), CV_RETR_EXTERNAL, CV_CHAIN_CODE);

int total = chain->total;


for (; chain != NULL; chain = (CvChain*)chain->h_next)
{

    int numChain = 0;

    CvSeqReader reader;
    int i, total = chain->total;

    cvStartReadSeq((CvSeq*)chain, &reader, 0);


    for (i = 0; i < total; i++)
    {
        char code;
        CV_READ_SEQ_ELEM(code, reader);
        int Fchain = (int)code;

        if (Fchain == 0)
        {
            count0++;
        }

        else if (Fchain == 1)
        {
            count1++;
        }

        else if (Fchain == 2)
        {
            count2++;
        }

        else if (Fchain == 3)
        {
            count3++;
        }

        else if (Fchain == 4)
        {
            count4++;
        }

        else if (Fchain == 5)
        {
            count5++;
        }

        else if (Fchain == 6)
        {
            count6++;
        }

        else if (Fchain == 7)
        {
            count7++;
        }

        totalCount++;
    }
}

cout << "0: " << count0 << endl;
cout << "1: " << count1 << endl;
cout << "2: " << count2 << endl;
cout << "3: " << count3 << endl;
cout << "4: " << count4 << endl;
cout << "5: " << count5 << endl;
cout << "6: " << count6 << endl;
cout << "7: " << count7 << endl;

cout << "Total: " << totalCount << endl;

double prob0 = (count0 / totalCount);
double prob1 = (count1 / totalCount);
double prob2 = (count2 / totalCount);
double prob3 = (count3 / totalCount);
double prob4 = (count4 / totalCount);
double prob5 = (count5 / totalCount);
double prob6 = (count6 / totalCount);
double prob7 = (count7 / totalCount);


cout << "Proababilty 0: " << prob0 << endl;
cout << "Proababilty 1: " << prob1 << endl;
cout << "Proababilty 2: " << prob2 << endl;
cout << "Proababilty 3: " << prob3 << endl;
cout << "Proababilty 4: " << prob4 << endl;
cout << "Proababilty 5: " << prob5 << endl;
cout << "Proababilty 6: " << prob6 << endl;
cout << "Proababilty 7: " << prob7 << endl;


waitKey(0);
return 0;

}